Archived

This topic is now archived and is closed to further replies.

Thrust

Please help me with this code, One small error and I cant find it.

Recommended Posts

With this code it all works fine but when im done doing a math problem and its suppost to show the menu it shows up twice. Also my teacher says I need to put a return 0; somewhere because he consideres that cheating but I dont know where I would put it. Thanks for your help. #include <iostream> #include <string> using namespace std; int main() { bool loop = true; while(loop == true) { string toDo=""; float num1 = 0.0; float num2 = 0.0; float answer = 0.0; cout<<"What would you like to do?"<>num1; cout<<"Enter the second number:"<>num2; answer = num1 + num2; cout<<"The answer in "<>num1; cout<<"Enter the second number:"<>num2; answer = num1 - num2; cout<<"The answer in "<>num1; cout<<"Enter the second number:"<>num2; answer = num1 * num2; cout<<"The answer in "<>num1; cout<<"Enter the numerator:"<>num2; answer = num1 / num2; cout<<"The answer in "<

Share this post


Link to post
Share on other sites
quote:
Also my teacher says I need to put a return 0; somewhere because he consideres that cheating but I dont know where I would put it. Thanks for your help.


You just return where you want to leave the function. In your case that is at the end of the program when you've finished.
Put it before the final '}'

Normally the compiler would complain when you miss it out if a function is declared to return something (like the 'int' your 'main' is supposed to return). With 'main' functions though compiler writers let you get away with it for historical reasons. But to be correct you should return something. Usually 0 if there are no problems or something else if there are.

If, for example, you tested for dividing by zero and found the user had entered a zero denominator, you could print a message and return an error code (such as 1). How/where you do that is up to you.

[edited by - petewood on March 6, 2003 10:32:29 AM]

Share this post


Link to post
Share on other sites
I figured out my problem, lol check it out


//Mark''s Special pretty calculator, HAHAHAHA
#include <iostream>
#include <string>
using namespace std;
int main()
{
bool loop = true;
while(loop == true)
{
string toDo="";
float num1 = 0.0;
float num2 = 0.0;
float answer = 0.0;

cout<<"What would you like to do?"< <<"1.add"< <<"2.subtract"< <<"3.multiplication"< <<"4.division"< getline (cin,toDo);

if (toDo =="1")
{
cout<<"Enter the first number:"< cin>>num1;
cout<<"Enter the second number:"< cin>>num2;
answer = num1 + num2;
cout<<"The answer in "< getline (cin,toDo);
}

else if (toDo =="2")
{
cout<<"Enter the first number:"< cin>>num1;
cout<<"Enter the second number:"< cin>>num2;
answer = num1 - num2;
cout<<"The answer in "< loop = false;
getline (cin,toDo);
}
else if (toDo =="3")

{ cout<<"Enter the first number:"< cin>>num1;
cout<<"Enter the second number:"< cin>>num2;
answer = num1 * num2;
cout<<"The answer in "< getline(cin,toDo);
}

else if (toDo =="4")
{
cout<<"Enter the denominator:"< cin>>num1;
cout<<"Enter the numerator:"< cin>>num2;
answer = num1 / num2;
cout<<"The answer in "< getline(cin,toDo);
}
else
{
cout<<"I dont recognize that option."< getline(cin,toDo);
}
}
return 0;
}


I somewhat know VB and now im learning C++, YAY FOR ME!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by petewood
With ''main'' functions though compiler writers let you get away with it for historical reasons. But to be correct you should return something.
Not returning anything is correct too. It''s also in the standard, not just in compilers.

http://www.zib.de/benger/C%2B%2B/clause3.html#s3.6.1

"If control reaches the end of main without encountering a return statement, the effect is that of executing

return 0;"

Share this post


Link to post
Share on other sites
thrust: I don't know whether you've read your post but the code isn't showing up correctly. This is because of the way the webpage treats < and > signs. It causes formatting rules to be used which aren't what you want. To get round this do as it says in the FAQ (which I suggested and you ignored). Put [ source ] and [ /source ] around your code (but without the spaces), otherwise people won't be able to help you.

AP: I see.

[edited by - petewood on March 6, 2003 11:06:26 AM]

Share this post


Link to post
Share on other sites
When you said faq I didnt know what you mean but thanks for pointing it out. I tried remaking it at home today and I keep getting errors, grr. If I dont figure it out by tonight/tomorrow ill post the new one. Thanks for all of your help.

I somewhat know VB and now im learning C++, YAY FOR ME!

Share this post


Link to post
Share on other sites