Jump to content
  • Advertisement

Archived

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

PaulB

Ahhhhhh!

This topic is 6190 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

My program shows strange numbers when I calculate. #include #include #include int main() { double first, second, answer; char symbol; cout << "First: "; cin >> first; cout << "\nSecond: "; cin >> second; cout << "\nEquation (+, -, *, /): "; cin >> answer; if (symbol == ''+'') answer = first + second; else if (symbol == ''-'') answer = first - second; else if(symbol == ''*'') answer = first * second; else if (symbol == ''/'') answer = first / second; cout << "Answer: " << answer; getchar(); return 0; } I''m using Dev-C++ if that helps.

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster

I don''t know if Dev C++ ha a decent debugger available, but if it doesn''t then either find a third party debugger and use that, or get a real compiler Once you have a debugger, step through the program a line at a time, watching the values of every variable. Then you''ll see something go wrong, and you''ll have your bug. (Learning to use a debugger is the single most important thing you can do - it''s amazing how many problems are horrible to find by reading code, but can be trivial with a debugger that tells you exactly where the error happens - at least once you learn to use it.)

quote:
Original post by PaulB
cout << "\nEquation (+, -, *, /): ";
cin >> answer;

if (symbol == ''+'')
answer = first + second;



The problem is there - you get the symbol into ''answer'' and then test the value of ''symbol''. The weird numbers are because the variables aren''t cleared to ''0'' - so whatever data ends up in them is what''s used.

Share this post


Link to post
Share on other sites
Shouldn''t this

cout << "\nEquation (+, -, *, /): ";
cin >> answer;

read

cout << "\nEquation (+, -, *, /): ";
cin >> symbol;

as symbol is not set correctly, the if..else stuff never gets called, which means you are printing out the character that you stored in answer



------------------
Trouble is my business
------------------

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!