0

I'm making an addition/multiplication game for my intro to C class. The objective of the program is to ask the user, what's the max number that you want to use which will seed random numbers within that max range and how many different problems you want to do. When I run the program and do the math, it tells me that the sum is incorrect and provides me an answer that isn't correct and is usually a large number like "1254323." Can you point me in the right direction as to what I am doing wrong?

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

int main() {

    int maxNumber, num1, num2, sum, answer, problems, i;

    srand(time(NULL));

    //printf("Would you like 1)Addition or 2)Multiplication?\n")
    printf("Enter your Max Number:");
    scanf("%d", &maxNumber);

    printf("How many problems do you want?\n");
    scanf("%d", &problems);

    sum = num1 + num2;

    while(i != problems) {
        num1 = rand()%maxNumber;
        num2 = rand()%maxNumber;
        i++;

        printf("What is %d + %d\n",num1, num2);
        scanf("%d", &answer);

        if(answer != sum){
            printf("Sorry, that's incorrect, the answer is %d\n", sum);
        }

        else{
            printf("Correct!\n");
        }
    }

    return 0;
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • Note that (even after you get the line `sum = num1 + num2` into the loop, you are producing _undefined behaviour_. Read [here](http://stackoverflow.com/questions/30821406/why-does-rand-rand-produce-negative-numbers/30821874#30821874). – too honest for this site Jul 01 '15 at 17:14

4 Answers4

1

You are assigning sum before setting num1 and num2.

Move the sum = num1 + num2; line into the loop after the num2 = rand()%maxNumber; line and it should work correctly.

There are some other errors as well (such as initializing i with 0).

BTW, it is generally considered a better practice to use a for loop instead of a while loop.

Here's slightly more readable code

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

int main(){
  int maxNumber, num1, num2, sum, answer, problems, i;
  srand(time(NULL));

  printf("Enter your Max Number:");
  scanf("%d", &maxNumber);

  printf("How many problems do you want?\n");
  scanf("%d", &problems);

  for (i = 0; i < problems; i++) {
    num1 = rand()%maxNumber;
    num2 = rand()%maxNumber;
    sum = num1 + num2;

    printf("What is %d + %d\n",num1, num2);
    scanf("%d", &answer);

    if(answer != sum){
      printf("Sorry, that's incorrect, the answer is %d\n", sum);
    } else {
      printf("Correct!\n");
    }
  }

  return 0;
}
Jay Bosamiya
  • 3,011
  • 2
  • 14
  • 33
1

You are using your variables without initializing them. Change:

int maxNumber, num1, num2, sum, answer, problems, i;

To:

int maxNumber = 0, num1 = 0, num2 = 0, sum = 0, answer = 0, problems = 0, i = 0;

Also, move the sum = num1 + num2; line to after where you create num1 and num2.

rost0031
  • 1,894
  • 12
  • 21
0

There are multiple issues with your program, including formatting. Here are the corrections:

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

int main(){
    int maxNumber, num1, num2, sum, answer, problems, i;

    srand(time(NULL));

    //printf("Would you like 1)Addition or 2)Multiplication?\n")
    printf("Enter your Max Number:");
    scanf("%d", &maxNumber);

    printf("How many problems do you want?\n");
    scanf("%d", &problems);

    // issue: i was not initialized
    i = 0;
    while(i != problems){
        i++;
        num1 = rand()%maxNumber;
        num2 = rand()%maxNumber;

        printf("What is %d + %d\n", num1, num2);
        // issue: sum was not calculated here
        sum = num1 + num2;
        scanf("%d", &answer);

        if(answer != sum){
            printf("Sorry, that's incorrect, the answer is %d\n", sum);
        }
        else{
            printf("Correct!\n");
        }
    }
    return 0;
}
Serge Rogatch
  • 13,865
  • 7
  • 86
  • 158
0

You define sum = num1 + num2, where num1 and num2 aren't assigned at all. C doesn't initialize numbers by default to zero. That's why you have such a big number.

Just add sum = num1 + num2 after num2 = rand()%maxNumber; and everything should work!

mblaettler
  • 31
  • 3