loop bug in my calculator

sup peps !

this is actually my first C++ program i also checked to find an anwser but found none ... let's have a look at it see were it bug!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// my first program in C++ (calculator)
// loop bug !

#include <iostream>
using namespace std;

int main ()
{
     int response;
     int select;
     int a, b;
     do
     {
         system ("cls"); // its here just because of the loop
         cout << "Choose two number to calculate, separated them whit a blank space, then press [enter]." << endl;
         cin >> a >> b;
         cout << "Do you want to ...\n" << endl;
         cout << "1. Add!\n" << endl;
         cout << "2. Substract!\n" << endl;
         cout << "3. Multiply!\n" << endl;
         cout << "4. Divide!\n" << endl;
         cout << "Specify operation by selecting right number then pressing [enter].\n\n";
         cin >> select;
         cout << "=" <<endl;
         
         switch(select)
         {
                       case 1: cout << a + b << endl; break;
                       case 2: cout << a - b << endl; break;
                       case 3: cout << a * b << endl; break;
                       case 4: cout << a / b << endl; break;
                       default: cout <<"Invalid operation!\n";
                       }
     //here start the problem. the calculator will infinite loop last operation
     //ask'ed to the program ( if " y " is pressed ).
         cout << "Press y to continue or n to exit.\n";
         cin >> response;
         }
    while (response);
    system ("paused"); // i use it just because its the only way i know
    
     return 0;
}


the program work well when it come to calculate but if " y " is pressed it WILL go on a infinite loop :(

does anyone can help me?
while (response); If it is not 0, then it's true;
int response; read integers, not characters. (response can't have "y" as value)

system ("paused"); // i use it just because its the only way i know

http://www.cplusplus.com/forum/beginner/1988/
system ("cls"); // its here just because of the loop
http://www.cplusplus.com/forum/beginner/1988/page3.html
Last edited on
Hi

A couple of errors but pretty good for your first program.

you need to change
1
2
3
4
5
        int     response;

        // to

        char    response;


because you want to input a character and not an integer

the other thing is you need to change your while statement to something like this
 
        while (response == 'y')


may need to test for uppercase as well

Hope this helps
Shredded

Last edited on
thx ne555 and shredded its work well, now i can try to get that thing better :P

Topic archived. No new replies allowed.