//Subtraction program - Brian Story
#include <stdlib.h>
#include <iostream>
usingnamespace std;
int main() {
int a, b, c, d, score; //Setting a, b, c, and score as an integer.
score = 0; // Score is equal to 0, so it starts you from the beginning every time.
do // loop while score is less then 10
{
srand (time(NULL)); // generates random numbers based on the time.
a = rand() % 10 + 1; // Calculates a as a random number from 1-10
b = rand() % 10 + 1; // Calculates b as a random number from 1-10
d = a - b; //Subtracts a and b and outputs the answer to d.
cout << a << " " << b << endl; // Shows user the random numbers generated
cout << a << " - " << b << " =?" << endl; // Same as above, but now asks for an answer.
cin >> c; // user inputs the answer as c
if (c==d) //compares the users answer and the real answer calculated above
{
cout << " Correct" << endl; // if its true tell the user its correct
score++; // adds 1 to score -- causes problem in program --
}
else // Complains that there is an expected ; before else
cout << " Incorrect" << endl; // if its true tell the user its not correct
} while (score<10); //Do whhile score is less then 10
cout << " You win!"; // Once the loop is complete tell the user he wins.
return 0;
}
Its a subtraction program to help me with math.
Every time I enter a negative number ( -7, -2, -3, etc) as an answer it freaks out and loops crazy.
I have a feeling it's something with how I have the math set up.
Has anybody the slightest clue how I could fix this? Or sset it up better?
I know I'm new to programming and all, but I think it freaks out with negative numbers because your variables are unsigned long ints by default when you use int _ _ _ . You'll need to use signed long ints to allow for negative and positive values.
//Subtraction program - Brian Story
#include <cstdlib>
#include <ctime>
#include <iostream>
usingnamespace std;
usingnamespace std;
int main() {
unsignedlongint a, b, c, d, score; //Setting a, b, c, and score as an integer.
score = 0; // Score is equal to 0, so it starts you from the beginning every time.
do // loop while score is less then 10
{
srand (time(NULL)); // generates random numbers based on the time.
a = rand() % 10 + 1; // Calculates a as a random number from 1-10
b = rand() % 10 + 1; // Calculates b as a random number from 1-10
d = a - b; //Subtracts a and b and outputs the answer to d.
cout << a << " " << b << endl; // Shows user the random numbers generated
cout << a << " - " << b << " =?" << endl; // Same as above, but now asks for an answer.
cin >> c; // user inputs the answer as c
if (c==d) //compares the users answer and the real answer calculated above
{
cout << " Correct" << endl; // if its true tell the user its correct
score++; // adds 1 to score -- causes problem in program --
}
else // Complains that there is an expected ; before else
cout << " Incorrect" << endl; // if its true tell the user its not correct
} while (score<10); //Do whhile score is less then 10
cout << " You win!"; // Once the loop is complete tell the user he wins.
return 0;
}
Yes, it does go bad if you enter any answer that cannot be
turned into a number.
for example if you enter a letter, or a random word or something like --5
add the following lines as shown:
1 2 3 4 5 6 7 8 9 10 11 12
if (c==d) //compares the users answer and the real answer calculated above
{
cout << " Correct" << endl; // if its true tell the user its correct
score++; // adds 1 to score -- causes problem in program --
}
else // Complains that there is an expected ; before else
cout << " Incorrect" << endl; // if its true tell the user its not correct
cin.clear(); //add this line
cin.ignore(255,'\n'); //add this line
} while (score<10); //Do whhile score is less then 10
Your program was correct to begin with (aside from the headers).
int is short for signed int. You must explicitly name it as unsigned to get an unsigned type. (With the single exception of char, all integer types are signed by default.)
But the entering of non-numbers will cause an infinite loop. The way to fix it is to check against bad numbers (as guestgulkan did in lines 9 and 10). I would be more specific to only check against the failbit and badbit flags (since eof is a possibility).
int is short for signed int. You must explicitly name it as unsigned to get an unsigned type. (With the single exception of char, all integer types are signed by default.)
I still receive the infinite looping if I enter a negative number. The program will give me a number like 3 - 6. Which I answer with a -3. One I give the answer -3 it will loop a while. Then it will stop and move on.
//Subtraction program - Brian Story
#include <cstdlib>
#include <ctime>
#include <iostream>
usingnamespace std;
usingnamespace std;
int main() {
unsignedlongint a, b, c, d, score; //Setting a, b, c, and score as an integer.
score = 0; // Score is equal to 0, so it starts you from the beginning every time.
do // loop while score is less then 10
{
srand (time(NULL)); // generates random numbers based on the time.
a = rand() % 10 + 1; // Calculates a as a random number from 1-10
b = rand() % 10 + 1; // Calculates b as a random number from 1-10
d = a - b; //Subtracts a and b and outputs the answer to d.
cout << a << " " << b << endl; // Shows user the random numbers generated
cout << a << " - " << b << " =?" << endl; // Same as above, but now asks for an answer.
cin >> c; // user inputs the answer as c
if (c==d) //compares the users answer and the real answer calculated above
{
cout << " Correct" << endl; // if its true tell the user its correct
score++; // adds 1 to score
}
else // Complains that there is an expected ; before else
cout << " Incorrect" << endl; // if its true tell the user its not correct
cin.clear(); // Added for checking against bad numbers
cin.ignore(255, \n);// Also added for checking against bad numbers
} while (score<10); //Do whhile score is less then 10
cout << " You win!"; // Once the loop is complete tell the user he wins.
return 0;
}
Well, may be this is not the real problem, but your do (...) while condition is wrong (from a logical point of view, not from coding), I guess.
It only ends the loop if you score ten points. But wha tif the user is wrong all teh time? Wouldnt it be better to coutn the questions isntead of the correct answers to determine when the programm should stop?
As I said, I cannot reproduce your error, and your code looks correct to me. Can you post an (abbreviated) example of what exactly is happening? For example:
it worked on devc++ for me, btw just a suggestion on the code, put a system("CLS") somewhere in there so it clears after declaring correct, maybe put a system("PAUSE") so it doesnt go directly to the clear screen, the cout the score at the top right :P