Advertisement

Ahhhhhh!

Started by September 07, 2001 02:38 AM
1 comment, last by PaulB 23 years, 3 months ago
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.

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.

Advertisement
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
------------------
------------------Trouble is my business------------------

This topic is closed to new replies.

Advertisement