Sign in to follow this  

3.7.5 Calculator Program

This topic is 3594 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Image and video hosting by TinyPic that is the exercise I'm doing currently and this is the code I have currently.
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
	bool quit = true;
	float x = 0;
	float y = 0;
	// Menu
	while (!quit)
	{
	cout << "1) cos(x), 2) sin(x), 3) tan(x), 4) atan2(y, x), 5) sqrt(x), 6) x^y, 7) ln(x), 8) e^x, 9) |x|, 10) floor(x), 11) ceil(x), 12) Exit." << endl;
	int selection;
	cin >> selection;

	switch(selection)
	{	
		case 1: 
		float cosf(float x);
		cin >> x;
		case 2:
		float sinf(float x);
		cin >> x;
		case 3:
		float tanf(float x);
		cin >> x;
		break;
		case 4:
		float acosf(float x);
		cin >> x;
		break;
		case 5:
		float asinf(float x);
		cin >> x;
		break;
		case 6:
		float atanf(float x);
		cin >> x;
		break;
		case 7:
		float sqrtf(float x);
		cin >> x;
		break;
		case 8:
		float logf(float x);
		cin >> x;
		break;
		case 9:
		float expf(float x);
		cin >> x;
		break;
		case 10:
		float powf(float x, float y);
		cin >> x;
		cin >> y;
		break;
		case 11:
		float fabsf(float x);
		break;
		case 12: 
			quit = false;

	}
	}

}
As you might be able to see I wasn't even able to include all the math functions and even with those math functions my code seem's to be all fudged up. If anyone can fill me in on how and what I should be doing with this program and how I can correct it, that would be greatly appreciated. Thanks in advance!

Share this post


Link to post
Share on other sites
The image... it's too biiiiig. Width of 480 is about maximum that's commonly acceptable for forums.... This one barely fits on 1600 resolution screen. Also, there's [ source ] tags for posting code.

Quote:
As you might be able to see I wasn't even able to include all the math functions


Why not? Which problems did you encounter? What were the errors you received?

Quote:
and even with those math functions my code seem's to be all fudged up.


Fudged up is not a good problem description. Neither is borked, fubared or messed up.

What were the problems you encountered? Did the compiler report any error messages? If so, which.

Quote:
If anyone can fill me in on how and what I should be doing with this program


What needs to be done is very clearly presented in the task description. Your code seems somewhat on the mark.

However, two things stand out.
- Check the quit variable and how it interacts with while() loop.
- Check the syntax of switch/case statement. After you process a condition, you need to add break; at the end of it to stop testing for more conditions.

You also need to check the syntax for declaring variables. The way you're declaring them now inside switch statement is incorrect.

Share this post


Link to post
Share on other sites
Here are the errors I received and NOTE: the reason I wasn't able to include all functions is that the exercise only wants to do 12 diffrent math functions the math library of functions has more then 12. Thus resulting in more cases but the exercise only wants me to make 12 diffrent functions. Here are my errors:

------ Build started: Project: Calculator Program, Configuration: Debug Win32 ------

Compiling...
main.cpp
c:\Users\Administrator\Documents\Visual Studio Projects\Calculator Program\main.cpp(20) : error C2883: 'cosf' : function declaration conflicts with 'cosf' introduced by using-declaration
c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\math.h(443) : see declaration of 'cosf'
c:\Users\Administrator\Documents\Visual Studio Projects\Calculator Program\main.cpp(23) : error C2883: 'sinf' : function declaration conflicts with 'sinf' introduced by using-declaration
c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\math.h(465) : see declaration of 'sinf'
c:\Users\Administrator\Documents\Visual Studio Projects\Calculator Program\main.cpp(26) : error C2883: 'tanf' : function declaration conflicts with 'tanf' introduced by using-declaration
c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\math.h(471) : see declaration of 'tanf'
c:\Users\Administrator\Documents\Visual Studio Projects\Calculator Program\main.cpp(30) : error C2883: 'acosf' : function declaration conflicts with 'acosf' introduced by using-declaration
c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\math.h(433) : see declaration of 'acosf'
c:\Users\Administrator\Documents\Visual Studio Projects\Calculator Program\main.cpp(34) : error C2883: 'asinf' : function declaration conflicts with 'asinf' introduced by using-declaration
c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\math.h(435) : see declaration of 'asinf'
c:\Users\Administrator\Documents\Visual Studio Projects\Calculator Program\main.cpp(38) : error C2883: 'atanf' : function declaration conflicts with 'atanf' introduced by using-declaration
c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\math.h(437) : see declaration of 'atanf'
c:\Users\Administrator\Documents\Visual Studio Projects\Calculator Program\main.cpp(42) : error C2883: 'sqrtf' : function declaration conflicts with 'sqrtf' introduced by using-declaration
c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\math.h(469) : see declaration of 'sqrtf'
c:\Users\Administrator\Documents\Visual Studio Projects\Calculator Program\main.cpp(46) : error C2883: 'logf' : function declaration conflicts with 'logf' introduced by using-declaration
c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\math.h(455) : see declaration of 'logf'
c:\Users\Administrator\Documents\Visual Studio Projects\Calculator Program\main.cpp(50) : error C2883: 'expf' : function declaration conflicts with 'expf' introduced by using-declaration
c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\math.h(447) : see declaration of 'expf'
c:\Users\Administrator\Documents\Visual Studio Projects\Calculator Program\main.cpp(54) : error C2883: 'powf' : function declaration conflicts with 'powf' introduced by using-declaration
c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\math.h(463) : see declaration of 'powf'
c:\Users\Administrator\Documents\Visual Studio Projects\Calculator Program\main.cpp(59) : error C2883: 'fabsf' : function declaration conflicts with 'fabsf' introduced by using-declaration
c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\math.h(449) : see declaration of 'fabsf'

Build log was saved at "file://c:\Users\Administrator\Documents\Visual Studio Projects\Calculator Program\Debug\BuildLog.htm"
Calculator Program - 11 error(s), 0 warning(s)


---------------------- Done ----------------------

Build: 0 succeeded, 1 failed, 0 skipped


Share this post


Link to post
Share on other sites
1. Are you getting compiler errors? If so, post them (or at least some of them) so that we can see what's going on with your code.

2. Consistent indentation is very important, both for readability and for detecting (and preventing) logic errors. Make sure that your indentation is consistent and makes sense.

3. It looks like you're trying to declare functions with the same names as those in the standard library within your case statements. What are these lines of code supposed to do exactly?

[Edit: There were a few posts made while I was writing this, but item 3, above, looks like it's still relevant.]

Share this post


Link to post
Share on other sites
This doesn't do what you think it does.
float cosf(float x);

If you call a function the syntax looks like this:
float x;
// initialize x to some value before the next line
cosf(x);

To save the result of the function call you can assign the result to a variable or print it out. Saving the result looks like this:
float x;
// initialize x to some value before the next line
float answer = cosf(x);

What you have is a function declaration, a way to describe what a function 'looks like' without saying 'what the function does'(the later is known as the function definition). That normally goes outside of a function, but is actually a perfectly valid syntax, EXCEPT when there is already a declaration for those functions. Where are they declared? cmath.

I suggest you reread the relevant section of your learning resource on calling functions. Hope that helps.

Share this post


Link to post
Share on other sites
Okay I did what you said and I have this so far


#include <iostream>
#include <cmath>
using namespace std;

int main()
{
bool quit = true;
float x = 0;
float y = 0;
// Menu
while (!quit)
{
cout << "1) cos(x), 2) sin(x), 3) tan(x), 4) atan2(y, x), 5) sqrt(x), 6) x^y, 7) ln(x), 8) e^x, 9) |x|, 10) floor(x), 11) ceil(x), 12) Exit." << endl;
int selection;
cin >> selection;

switch(selection)
{
case 1:
cin >> x;
cosf(x);
break;

case 2:
cin >> x;
sin(x);
break;

case 3:
cin >> x;
tan(x);
break;

case 4:
cin >> y;
cin >> x;
atan2(y, x);
break;

case 5:
cin >> x;
sqrt(x);
break;

case 6:
cin >> x;
x^y;
break;

case 7:
cin >> x;
ln(x);
break;

case 8:
cin >> x;
e^x;
break;

case 9:
cin >> x;
|x|;
break;

case 10:
cin >> x;
floor(x);
break;

case 11:
cin >> x;
ceil(x);
break;

case 12:
quit = false;

}
}

}


These are the errors I received from the code:

------ Build started: Project: Calculator program v2.0, Configuration: Debug Win32 ------

Compiling...
main.cpp
c:\Users\Administrator\Documents\Visual Studio Projects\Calculator program v2.0\main.cpp(47) : error C2296: '^' : illegal, left operand has type 'float'
c:\Users\Administrator\Documents\Visual Studio Projects\Calculator program v2.0\main.cpp(47) : error C2297: '^' : illegal, right operand has type 'float'
c:\Users\Administrator\Documents\Visual Studio Projects\Calculator program v2.0\main.cpp(52) : error C3861: 'ln': identifier not found, even with argument-dependent lookup
c:\Users\Administrator\Documents\Visual Studio Projects\Calculator program v2.0\main.cpp(57) : error C2065: 'e' : undeclared identifier
c:\Users\Administrator\Documents\Visual Studio Projects\Calculator program v2.0\main.cpp(62) : error C2143: syntax error : missing ';' before '|'

Build log was saved at "file://c:\Users\Administrator\Documents\Visual Studio Projects\Calculator program v2.0\Debug\BuildLog.htm"
Calculator program v2.0 - 5 error(s), 0 warning(s)


---------------------- Done ----------------------

Build: 0 succeeded, 1 failed, 0 skipped


It's saying some of my functions are wrong but that's how you told me to do them....

Share this post


Link to post
Share on other sites

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
bool quit = true;
float x = 0;
float y = 0;
// Menu
while (!quit)
{
cout << "1) cos(x), 2) sin(x), 3) tan(x), 4) atan2(y, x), 5) sqrt(x), 6) x^y, 7) ln(x), 8) e^x, 9) |x|, 10) floor(x), 11) ceil(x), 12) Exit." << endl;
int selection;
cin >> selection;

switch(selection)
{
case 1:
cin >> x;
cosf(x);
break;

case 2:
cin >> x;
sin(x);
break;

case 3:
cin >> x;
tan(x);
break;

case 4:
cin >> y;
cin >> x;
atan2(y, x);
break;

case 5:
cin >> x;
sqrt(x);
break;

case 6:
cin >> x;
x^y;
break;

case 7:
cin >> x;
ln(x);
break;

case 8:
cin >> x;
e^x;
break;

case 9:
cin >> x;
|x|;
break;

case 10:
cin >> x;
floor(x);
break;

case 11:
cin >> x;
ceil(x);
break;

case 12:
quit = false;

}
}

}




The errors are there because the stuff you tried to do does not work that way in C++. Lets fix the errors one at a time.

x^y; This operator doesn't do what you think it does. Try pow(x,y) instead.
ln(x); Again, the function is log(x)
e^x; exp(x) does what you want
|x|; abs(x)

Share this post


Link to post
Share on other sites
This seems like a homework question so we arent really supposed to help, but you are really lost so ill at least show how to do the function calls.


// say you input a float input.
cin >> input;
// then you get the result of cos(input)
float result = cosf(input);
// then you output this result
cout << result;



Is this your first assignment using functions? Someone could probably recommend a good online book for you.

Share this post


Link to post
Share on other sites
Quote:
Original post by Roberts91
[huge image]


It should be possible to copy and paste text out of a PDF... not sure how it works in the latest version of Reader though.

Quote:

Several case statements which look like:

float cosf(float x);
cin >> x;



Let's say we want to describe how to make and eat a peanut butter sandwich, clean your room, etc. We have all the instructions written down on a piece of paper, under headings, like:


To make and eat a peanut butter sandwich:
There is a way to spread peanut butter onto bread.
Take the bread and peanut butter out of the cupboard.
To clean your room:


There are four logical problems here.

#1), saying there *is a way* to do something is not the same as issuing the command to *do it*. In C++, to call a function, do not fill in the variable types: they're understood from the variables that you're passing in. (The reason you specify them where the function is declared and/or defined is so that the compiler has an idea of what should be passed, so it can check what you've passed when you make the call. Of course, in the current case, the functions are already declared and defined by the library, so you should not do so yourself.)

#2), you can't spread the peanut butter onto the bread until after you have acquired both items. In C++, statements are logically executed in the order they are put down. (The compiler can rearrange them, but only in ways that it can prove will not affect what actually happens.) This means you have to think about what is going on, in general.

#3), you might know, from your common sense, that the "To clean your room:" header means that the instructions for making a sandwich have ended, but a compiler has no common sense. It only has a specification for the language it is compiling, and the specification says that the 'labels' (the 'case whatever:' lines) inside a switch are basically just points that control jumps to: there is no implicit jump back, so code keeps running into the next case if you don't say otherwise. The way around that is the 'break' statement.

#4), you still need to do something with the peanut butter sandwich after making it. Namely, eat it. In the C++ code, you are not making any attempt to output the result of the function call.

In C++, we can - in normal cases - treat a call to a function just as if it were a variable of the same type as what the function returns.

So, we should get something like:


cin >> x;
cout << cosf(x);
break;


Quote:
As you might be able to see I wasn't even able to include all the math functions


The assignment didn't ask you for acos or asin, x^y is in the wrong place, and you're missing floor and ceiling functions at the end. Also, you really do want "atan2", not "atan". It's a long story.

Share this post


Link to post
Share on other sites

This topic is 3594 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this