Hey, I'm new here and this is my first post. I have started on my journey down the path of learning the language of computers. I know a lot of tutorials say to mess around and try to create your own program with the knowledge that you learned so far. So that is exactly what I attempted. I was wondering why this program will not loop. It posts the question, accepts the answer, but when I go to try again/start new problem, I am not allowed. It automatically jumps to the end "Okay." is displayed and the program terminates.
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
usingnamespace std;
int main()
{
char input[1];
int x = rand() & 25;
int y = rand() & 25;
int a = x+y;
int answer;
cout<<"This program is a simple test. You must add 2 random numbers.\n\n";
do {
cout<<"The question is: " << x << "+" << y << "= ";
cin>>answer;
cout<<"\n\n";
if (answer == a){
cout<<"Correct!\n\n";
cout<<"Would you like to try a new problem? (y/n): ";
gets(input);
}
elseif (answer != a) {
cout<<"Incorrect!\n\n";
cout<<"Would you like to try again? (y/n): ";
gets(input);
}
} while (strcmp (input, "y") == 0);
cout<<"Okay.";
cin.get();
}
Line 10: You declare input as a single character array.
Lines 23,28: You call gets. gets stores characters until a new line is encountered and and then stores a terminating null writing past the single character you've allocated corrupting storage in your program. gets has no knowledge of how large input is. gets is deprecated and shouldn't be used.
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <ctime>
usingnamespace std;
int main(int nNumberofArgs, char* pszArgs[])
{
char input;
int x;
int y;
int a;
cout << "This program is a simple test. You must add 2 random numbers.\n";
while(input != 'n')
{
srand((unsigned)time(0));
x = rand() & 25;
y = rand() & 25;
a = x + y;
int answer;
cout << "The question is: " << x << "+" << y << "= ";
cin >> answer;
cout << "\n";
if (answer == a)
{
cout << "Correct!\n";
cout << "Would you like to try a new problem? (y/n): ";
cin >> input;
}
elseif (answer != a)
{
cout << "Incorrect!\n";
cout << "The correct answer was: " << a << endl;
cout << "Would you like to try again? (y/n): ";
cin >> input;
}
}
cout << "Okay.";
return 0;
}
Thank you all for replying. I seem to have fixed it by changing input[1] to input, gets() to cin>>input, and while (strcmp (input, "y") == 0) to while (input == 'y')
@GreenLeaf, thank you for the input and fixes, I will study your code and figure out what everything does so that hopefully in the future I can avoid this mistake :)
The previous replies are all good.
I'm just suggesting an approach you could have taken to find the problem.
Your program doesn't loop therefore input never equals "y".
Your program doesn't stop and wait for a value for input to be entered so it must already have a value for input. What is it and where did it come from?
input may be a garbage value so initialize it to "y".
Program still misbehaves so gets(input) is changing input but to what?
The answer is because of the way extraction operator (>>) works (which BTW should be included in a beginners tutorial).
After cin>>answer; there is an end of line character left in the buffer (stream?).
gets(input); reads the end of line and puts '\0' into input.*
input is never "y" so your program never loops.
*I'm not sure if gets() uses same buffer (stream?). Feel free to enlighten me.
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
usingnamespace std;
int main()
{
char input[2]; // change to 2 for correctness but this alone is not a fix
int x = rand() & 25;
int y = rand() & 25;
int a = x+y;
int answer;
cout<<"This program is a simple test. You must add 2 random numbers.\n\n";
do {
cout<<"The question is: " << x << "+" << y << "= ";
cin>>answer;
cout<<"\n\n";
gets(input); // added line "eats" '\n' and the buffer is empty
if (answer == a){
cout<<"Correct!\n\n";
cout<<"Would you like to try a new problem? (y/n): ";
gets(input);
}
elseif (answer != a) {
cout<<"Incorrect!\n\n";
cout<<"Would you like to try again? (y/n): ";
gets(input);
}
} while (strcmp (input, "y") == 0);
cout<<"Okay.";
cin.get();
}