I'm just starting to teach myself C++ from the tutorial on cprogramming.com, and I made a little command line program that asks you some questions and tells you if you win or fail. The problem is, if you enter a letter instead of a number, the program ends instead of asking you to try again. I've been trying to fix this myself for 2 hours and just got fed up with it. As the title suggests, I think it's a problem with the loop I used to allow it to restart.
I'm also curious to know if I'm writing good code, or if I should be doing things differently or something. You guys are the only feedback I have, since I'm not taking a class and none of my friends even know how to write a webpage.
#include <iostream>
usingnamespace std;
int main() // VERY VERY VERY IMPORTANT TO REMEMBER THIS PART!
{
int correct = 1; // Declaring the variables
int re = 0;
do
{
int a = 0;
int b = 1;
re = 0;
cout<<"Enter 2 numbers that are the same...\n"; //Display the challenge
cin>> a; // User's first imput becomes a
cin>> b; // User's second input becomes b
if ( a == b ) // Check that a and b are equal
{
cout<<"That's right!\nWhat is 4+3?\n"; // Display the success message and ask the next question
cin>> a; // variable a is now being used for this question
if ( a == 7 ) // check for the right answer
{
cout<<"You're good.\nWhat comes after 3857?\n"; // Lots of success
cin>> a; // variable a is now being used for this question. You get the idea.
cin.ignore();
if ( a == 3858 ) // again, checking if the answer is right
{
correct = 1;
}
else
{
correct = 0; // telling the program that the answer is not correct
}
}
else
{
correct = 0;
}
}
else
{
correct = 0;
}
if ( correct == 1 )
{
cout<<"\nVICTORY SCREAM.\n"; // you win and the program ends
re = 0;
}
else
{
cout<<"FAILURE!\n";
cout<<"Enter 1 to try again\n";
cin>> re;
if ( re != 1 )
{
re = 0; // if you enter something other than 1, the loop will break
}
}
}
while ( re == 1 );
cin.get();
}
#include <iostream>
usingnamespace std;
bool add_qs(int a, int b); //declaration, allows for use w/o putting main() at bottom
int main(){
bool correct;
correct = add_qs(3, 4);
if (correct)
cout << "right" << endl;
else //not correct
cout << "wrong" << endl;
return 0;}
bool add_qs(int a, int b){ //define our function here
int ans; //temp variable local to function
cout << "What is " << a << " + " << b << " ?" << endl;
cin >> ans;
if (ans==a+b)
returntrue; //'correct' gets the return value, in this case 'true' is returned
else //this else isn't even necessary, but this may make it more readable
returnfalse;}
The use of multiple functions allows for much easier changes to a program. This way, if you wanted to ask the user more questions, the amount of code needed to be added to main() would be minimal. You could also add the messages "wrong" and "right" right inside of the secondary function.
That's more advanced than what I can figure out right now. I can only do inputs, outputs, if's, and loops. But thanks for the help anyway; I will probably use it as I keep learning more. From what I can make out, that's exactly the kind of thing I would have trouble getting used to because I'm so used to writing html, which is annoyingly static.
Look into other data types like bool and double. I see you like int a lot, but bool could be used in this program for your first two variables, and it will make it look a little cleaner.
The tutorial I'm learning from hadn't told me about those data types yet, only int, so that's what I used. I've moved on beyond this stage in the program though and pretty much solved my problem. Thanks for the help!