Thread: Numbering questions that the user wants to answer.

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    15

    Numbering questions that the user wants to answer.

    Am learning really hard on this program, but am stuck. This code works fine for only one random equation, but now I need to get the user to ask the number of questions they need which will be given in number form, i.e question 1,2,3 and so on. I can't get that to work, also when they answer the question the score should be given at the end that is number correct. Any help will be appreciated. Thanks.
    Code:
    //included libraries
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <math.h>
    #define  MAX_VAL 10
    
    int main() {
    //Variables declaration. This defines both the problem, random numbers and answer functions
    int problem = 0, num1, num2, op, total;
    float answer, answer2 = 0;
    
    
    //This function seeds the random function with the computer's internal clock
    srand(time(0));
    
    //To get random operator and random numbers -50 to 50 inclusive.
    op = rand()%4;
    problem =1 + rand ()%MAX_VAL;
    num1 = (rand()%101)-50;
    num2 = (rand()%101)-50;
    total = rand()%10;
    
    
    //Prints the welcome statement.
    printf("Welcome! How many problems would you like?.\n\n");
    scanf("%d", &problem);
    printf("Here is random problem number:%\n", total);
    
    //This part of the code generates the operation
    
    while (op == problem){
    answer = num1 + num2;
    printf("%d + %d= ?\n", num1, num2);
    printf("\nAnswer:");
    scanf("%f", &answer2);
    }  if (op == 1) {
    answer = num1 - num2;
    printf("%d - %d= ?\n", num1, num2);
    printf("\nAnswer:");
    scanf("%f", &answer2);
    }  if (op == 2) {
    answer = num1 * num2;
    printf("%d * %d= ?\n", num1, num2);
    printf("\nAnswer:");
    scanf("%f", &answer2);
    }  if (op == 3) {
    answer = num1 / num2;
    printf("%d / %d= ?\n", num1, num2);
    printf("\nAnswer:");
    scanf("%f", &answer2);
    
    
    }
    
    
    
    
    
    printf("\nCongratulations! You have answered the problem correctly!\n");
    
    
    return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Your indentation, which is totally lacking, needs to improve real quick.
    SourceForge.net: Indentation - cpwiki
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    15
    What do you mean Salem?? Show me what indentation that is lacking and what I need to do; As explained earlier in each of my post, am still learning. And I need to know where I made the error so I can correct them. Thanks.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Currently, all of your lines of code start in column zero. Read the link Salem provided (click on the red text "SourceForge.net..."). Pay attention to how some lines are more indented than others. That helps you organize your code visually, so it's easier to understand when you read and write it.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    This is your code, indented.
    Code:
    //included libraries
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <math.h>
    
    #define  MAX_VAL 10
    
    int main()
    {
      //Variables declaration. This defines both the problem,
      // random numbers and answer functions
      int problem = 0, num1, num2, op, total;
      float answer, answer2 = 0;
    
      //This function seeds the random function with the computer's internal clock
      srand(time(0));
    
      //To get random operator and random numbers -50 to 50 inclusive.
      op = rand() % 4;
      problem = 1 + rand() % MAX_VAL;
      num1 = (rand() % 101) - 50;
      num2 = (rand() % 101) - 50;
      total = rand() % 10;
    
      //Prints the welcome statement.
      printf("Welcome! How many problems would you like?.\n\n");
      scanf("%d", &problem);
      printf("Here is random problem number:%\n", total);
    
      //This part of the code generates the operation
      while (op == problem) {
        answer = num1 + num2;
        printf("%d + %d= ?\n", num1, num2);
        printf("\nAnswer:");
        scanf("%f", &answer2);
      }
      if (op == 1) {
        answer = num1 - num2;
        printf("%d - %d= ?\n", num1, num2);
        printf("\nAnswer:");
        scanf("%f", &answer2);
      }
      if (op == 2) {
        answer = num1 * num2;
        printf("%d * %d= ?\n", num1, num2);
        printf("\nAnswer:");
        scanf("%f", &answer2);
      }
      if (op == 3) {
        answer = num1 / num2;
        printf("%d / %d= ?\n", num1, num2);
        printf("\nAnswer:");
        scanf("%f", &answer2);
      }
    
      printf("\nCongratulations! You have answered the problem correctly!\n");
      return 0;
    }
    Notice how it is so much easier to follow the program flow.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Jun 2012
    Posts
    15
    I am so sorry for not paying attention to indention. I will be more careful. Will someone please help.

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Why are you assigning a random value to the "problem" variable, and then (without using this value) assigning user input to the same variable?

    Code:
    problem = 1 + rand() % MAX_VAL;
    // ...
    scanf("%d", &problem);
    Also, if you enter the "while" loop, how do you expect to leave it? The values of "op" and "problem" never change in the body of the loop.

    Code:
    while (op == problem)
    {
        answer = num1 + num2;
        printf("%d + %d= ?\n", num1, num2);
        printf("\nAnswer:");
        scanf("%f", &answer2);
    }
    It appears that you need to re-think the "part of the code that generates the operation" - it doesn't appear to do what you think it does (unless I'm completely misunderstanding your goal here).

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Well, I get the following when I compile, for starters:
    Code:
    $ make foo
    gcc -Wall -g -std=c99  -lssl -lm -lpthread -lcurses -lefence  foo.c   -o foo
    foo.c: In function ‘main’:
    foo.c:29: warning: unknown conversion type character 0xa in format
    foo.c:29: warning: too many arguments for format
    Which is this line:
    Code:
    printf("Here is random problem number:%\n", total);
    I think you need a 'd' after the percent, like so: "...number: %d\n"

    Then, you need to wrap all your logic for a single question/problem/equation in a loop, like so:
    Code:
    ask user how many problems
    read num_problems
    
    for i from 0 to num_problems
        get random operator, num1, num2
        if operator == 1
            display addition problem
            calculate correct answer
        else if operator == 2
            display subtraction problem
            calculate correct answer
       ...
       ask user for answer
       inform them whether they are correct
    Since we were just talking about indentation, notice the indentation in that pseudo code. It should tell you what parts belong "under" what other parts, i.e. what parts are subordinate to other parts.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c questions again. Please answer.
    By malvado in forum C Programming
    Replies: 39
    Last Post: 06-08-2011, 11:02 AM
  2. Answer to these questions
    By mdennis10 in forum C++ Programming
    Replies: 3
    Last Post: 04-19-2011, 07:48 AM
  3. A couple of questions that someone might know the answer to...
    By Finchie_88 in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 04-15-2005, 08:26 AM
  4. Sentinel Value and numbering
    By fjf314 in forum C++ Programming
    Replies: 10
    Last Post: 01-09-2004, 09:04 PM
  5. Questions ab an answer...
    By JohnMayer in forum C Programming
    Replies: 8
    Last Post: 08-11-2002, 11:06 PM