Weird problem with -#'s

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
//Subtraction program - Brian Story

#include <stdlib.h>
#include <iostream>
using namespace 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?
Last edited on
It works correctly for me.

BTW, the includes at the top should read:
1
2
3
4
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;

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.

I might be wrong though.... just trying to help.
I changed the include, I also changed the int to unsigned long int. When I am prompted to input a negative number it freaks out!

Here's my current 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
//Subtraction program - Brian Story

#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
using namespace std;

int main() {
    unsigned long 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; 
}
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 
Last edited on
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).

Hope this helps.
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.)


Doh... I had it backwards :(
You can change that behavior with compiler options... Then you'd have had it forwards. ;-)
Last edited on
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.

Any ideas on how to fix 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
29
30
31
32
33
34
35
36
37
38
39
//Subtraction program - Brian Story

#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
using namespace std;

int main() {
    unsigned long 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 
         }
    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?

int main
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:

8 1
8 - 1 =?
7
Correct
10 10
10 - 10 =?
0
Correct
5 9
5 - 9 =?
-4
Correct
...
You win!

Also, please change that unsigned long int back to a signed number.

Also, what compiler and options are you using?
Last edited on
Thanks Duoas! Changing the unsigned long int back to int is what corrected the problem.

Everything works fine now!
:-)
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
Topic archived. No new replies allowed.