Quit function in my basic calculator

Hey everybody,
I'm pretty new to C++ programing, and I was trying to build a basic calculator with a loop so that I could continue to do calculations without having to close the program every time that I used it. I got that to work, but when I tried to add a quit function, the whole thing went into a fast infinite loop. I was wondering if someone could read my code and help me place the if statement correctly and how to write it (because i have a feeling that I am not doing it right).

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
// simple calculator

#include <iostream>

using namespace std;

int main()
{
    float a;
    float b;
    loop:
    cout << "Please enter two numbers and hit enter" << endl;
    cout << "first number (a) " ;
    cin >> a;
    if (a == 'quit') {
           return 0; }
    cout << "second number (b) ";
    cin >> b;
     if (b == 'quit') {
           return 0; }
    cout << endl;  
    cout << "A * B: " << a * b << endl;
    cout << "A / B: " << a / b << endl;
    cout << "A + B: " << a + b << endl;
    cout << "A - B: " << a - b << endl;
    cout << endl;
    cout << "Type quit and hit enter to terminate the program" << endl << endl;
    goto loop;
}


Thanks for the help :)
On line 15 and 19, you want double quotes, as single quotes are used for single characters only. Also, a float is a number and cannot ever store a string; inputting letters when it asks for a float will not work properly.
ok, I'll make the changes to the quotes, but how would you suggest I maintain my floating variables while also allowing for a string. is there a variable that allows for alphanumerical values? or how would go about doing that. that I think is really the core of my problem, while the quotes are easily changed. thanks for the reply
On line 15 and 19, you want double quotes, as single quotes are used for single characters only.


This is correct, but keep in mind that you will need to use the string compare function to compare the two strings.

Also, a "goto" loop is a very bad way to perform a loop. Use a While loop.

but how would you suggest I maintain my floating variables while also allowing for a string. is there a variable that allows for alphanumerical values?


Char*. I don't think you should go that route though. I'd reccomend that at the end of your loop, ask the user whether he wants to continue, and have the user enter yes or no.
Thanks for the reply,
I was able to ask the user if they would like to continue using a while loop, but i was unsure how to loop back to the beginning of the program without a goto loop. i know that that loop is undesirable, I have had several problems with it, but I am unsure how else to go about this.

1
2
3
4
5
6
cout << "Would you like to continue? Type any key to resume or quit to terminate. " << endl << endl;
    string x;
    cin >> x;
    while ( x == "quit"){
          return 0; }
    goto loop;


That seemed to fix my biggest problem. thanks alot. Another problem that I am having however is that anytime you place an alpha character when it is looking for a float, it goes on a speed infinite tantrum. are there any ways that i can this or have it recognized by the code to break out of the loop and start over again? that would be a really handy piece of coding to have.
As Vexer said, you could use while loop. I'm not experienced myself, but loops seem much better to me, at least because of readability. You can do it like this:

1
2
3
4
5
6
7
8
9
10
11
string x;
do {

//your stuff to loop

cout << "\"quit\" to terminate: ";
cin >> x;

} while (x != "quit");

return 0;


About the "alpha character", try placing this after cin >> variable;:

cin.ignore(numeric_limits<streamsize>::max(), '\n');

It works for me if I type non-digit characters after digits, otherwise console closes down.
the
cin.ignore(numeric_limits<streamsize>::max(), '\n');
didn't help with anything. It didn't stop the code from working, but it also didn't help the code from not working. the problem is when you enter a character that is not a number when the program is expecting a float. when it asks for a number and you put the letter 'a' for example, the program just streamlines calculations way faster than what you can see, and it doesn't stop until you manually press the escape. There has got to be a way for the program to recognize that either it is going out of control or that an invalid character was entered, and can stop its self before zooming out of control. anyone have any suggestions? I have changed the code since the beginning and it now looks like this

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
// simple calculator

#include <iostream>

using namespace std;

int main()
{
    float a;
    float b;
    string x;
    do {
    cout << "Please enter two numbers and hit enter" << endl;
    cout << "first number (a) " ;
    cin >> a;
    cout << "second number (b) ";
    cin >> b;
    cout << endl;  
    cout << "A * B: " << a * b << endl;
    cout << "A / B: " << a / b << endl;
    cout << "A + B: " << a + b << endl;
    cout << "A - B: " << a - b << endl;
    cout << endl;
    cout << "Type quit and hit enter to terminate the program" << endl << endl;
    cin >> x;}
    while (x != "quit");
          return 0;
}
That's one of the problems about reading directly into a float or other similar variable; if you input invalid valid data (in this case, alphabetical values) cin will go into an error state and stop working.

1
2
3
4
cout<< "first number (a) ";
while(!(cin>>a)) {
    cout<<"not valid input"<<endl<<"first number (a) ";
}

This is, I think, what you are looking for. I haven't tested it though so I may have made a typo or logic error.
Nope it doesn't work. It does recognize that it is not a float value that is being entered, but it gives me the "not a valid input, first number (a)" in an infinite loop.

Edit:
I figured it out using
1
2
3
4
5
6
7
 while (true) {
   cout << "First value (a): ";
   cin >> a;
   stringstream myStream(a);
   if (myStream >> myNumberA)
     break;
   cout << "Invalid number" << endl;


thanks for all the help.
Last edited on
Perhaps trying clearing the error state:
1
2
3
4
5
cout<< "first number (a) ";
while(!(cin>>a)) {
    cout<<"not valid input"<<endl<<"first number (a) ";
    cin.clear();
}
I have finished the program and it works great if you would like to give it a try here's the code.

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
44
45
46
47
// Simple calculator 

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main() {
 string a;
 string b;
 float myNumberA; // variable that will be used durring calculations
 float myNumberB; // variable that will be used durring calculations
 string x; // quit varible
 do { // beginning of a while loop
 cout << "Please enter your values " << endl;
 while (true) {
   cout << "First value (a): ";
   cin >> a;
   // This code converts from string to number safely.
   stringstream myStream(a);
   if (myStream >> myNumberA)
     break;
   cout << "Invalid number" << endl;
 }
 // if you want to make a refrence to the variable, do it after the }
 while (true) {
       cout << "Second Value (b) ";
       cin >> b;
       // This code converts from string to number safely
       stringstream myStream (b);
       if (myStream >> myNumberB)
          break;
       cout << "Invalid number" << endl;
       }
 cout << endl;
 // calculations
 cout << "A * B = " << myNumberA * myNumberB << endl;
 cout << "A / B = " << myNumberA / myNumberB << endl;
 cout << "A + B = " << myNumberA + myNumberB << endl;
 cout << "A - B = " << myNumberA - myNumberB << endl << endl;
 cout << "Type any value and hit enter to continue, or quit to terminate" <<endl;
 cin >> x; }
 // quit function
 while (x != "quit");
 return 0;
}
Topic archived. No new replies allowed.