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).
// simple calculator
#include <iostream>
usingnamespace 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;
}
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;:
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
// simple calculator
#include <iostream>
usingnamespace 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;
// Simple calculator
#include <iostream>
#include <string>
#include <sstream>
usingnamespace 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;
}