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

Started by
5 comments, last by Thrust 21 years, 1 month ago
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 "<<answer<<"."<<endl; } } } I somewhat know VB and now im learning C++, YAY FOR ME!
Mark St. Jean - OwnerWastedInkVwmaggotwV@Yahoo.com
Advertisement
read the faq to find out how to format code
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]
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!
Mark St. Jean - OwnerWastedInkVwmaggotwV@Yahoo.com
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;"

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]
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!
Mark St. Jean - OwnerWastedInkVwmaggotwV@Yahoo.com

This topic is closed to new replies.

Advertisement