Thread: when I compile it, I see same equation keep repeating.

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

    when I compile it, I see same equation keep repeating.

    when I compile this code, I see same equation keep repeating. I need to get different equations that the user has requested and at the end to loop it and get what is correct out of total requested problems i.e 3/5. I can't figure out to get the percentage of what they got too. Please help.thanks.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include<time.h>
    
    int main(void){
    int count=1 , times=1, x, number;
    int num1, num2, op;
    float answer, answer2 = 0;
    
    srand(time(0)); op = rand()%4;
    num1 = rand()%101-50;
    num2 = rand()%101-50;
    
    printf("Welcome, how many problems would you like?\n");
    scanf("%d",&x);
    for (times; times <= 1; times += 1) {
    for (count; count <= x; count += 1) {
    printf("Here is the random problem number %d \n", count);
    
    if(op == 0) /*this is where the operations start*/
    {
    answer = num1 + num2;
    printf("%d + %d=?\n\n", num1, num2);
    printf("\nAnswer:");
    scanf("%f", &answer2);
    }
    else if (op == 1)
    {
    answer = num1 - num2;
    printf("%d - %d=?\n\n", num1, num2);
    printf("\nAnswer:");
    scanf("%f", &answer2);
    }
    else if (op == 2)
    {
    answer = num1 * num2;
    printf("%d * %d=?\n\n", num1, num2);
    printf("\nAnswer:");
    scanf("%f", &answer2);
    }
    else if (op == 3)
    {
    answer = num1 / num2;
    printf("%d / %d=?\n\n", num1, num2);
    printf("\nAnswer:");
    scanf("%f", &answer2);
    }
    }
    while (answer != count) {
    printf("\nYou got %d correct. That is %d!\n");
    scanf("%f", &answer);
    }
    }
    getchar();
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Yech, sorry, but this formatting is terrible. Here's an example of what it should look like:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include<time.h>
    
    int main(void)
    {
        int count=1 , times=1, x, number;
        int num1, num2, op;
        float answer, answer2 = 0;
    
        srand(time(0));
        op = rand()%4;
        num1 = rand()%101-50;
        num2 = rand()%101-50;
    
        printf("Welcome, how many problems would you like?\n");
        scanf("%d",&x);
        for (times; times <= 1; times += 1)
        {
            for (count; count <= x; count += 1)
            {
                printf("Here is the random problem number %d \n", count);
    
                if(op == 0) /*this is where the operations start*/
                {
                    answer = num1 + num2;
                    printf("%d + %d=?\n\n", num1, num2);
                    printf("\nAnswer:");
                    scanf("%f", &answer2);
                }
                else if (op == 1)
                {
                    answer = num1 - num2;
                    printf("%d - %d=?\n\n", num1, num2);
                    printf("\nAnswer:");
                    scanf("%f", &answer2);
                }
                else if (op == 2)
                {
                    answer = num1 * num2;
                    printf("%d * %d=?\n\n", num1, num2);
                    printf("\nAnswer:");
                    scanf("%f", &answer2);
                }
                else if (op == 3)
                {
                    answer = num1 / num2;
                    printf("%d / %d=?\n\n", num1, num2);
                    printf("\nAnswer:");
                    scanf("%f", &answer2);
                }
            }
            while (answer != count)
            {
                printf("\nYou got %d correct. That is %d!\n");
                scanf("%f", &answer);
            }
        }
        getchar();
    }
    And don't forget to "return 0;" at the end!

    Anyway, regarding the problem you described: "op" is given a random value at the beginning of the program, before you enter any of your loops. This value is never updated after that, so it will always pick the same equation for each iteration of the loop. Perhaps if you updated the "op" variable within the second "for()" loop, you'd get a different result each time.

    Code:
    printf("\nYou got %d correct. That is %d!\n");
    And you aren't providing variables to match the two format specifiers in this "printf()" statement.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.

  4. #4
    Registered User
    Join Date
    Jun 2012
    Posts
    8
    As Matticus said you are not updating the value of 'op' so it will stay te same every time the for loop starts

  5. #5
    Registered User
    Join Date
    Jun 2012
    Posts
    15
    Here is my code, division needs to be 2 decimals, also I can't figure out how to input the right code for total correct and wrong answers and the right code for what percentage was attained. thanks.

    //included libraries
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include<time.h>
    
    int main(void)
    {
        int count=1 , times=1, x, number;
        int num1, num2, op;
        float answer, answer2 = 0;
    
        srand(time(0));
    
    
    
        printf("Welcome, how many problems would you like?\n");
        scanf("%d",&x);
        for (times; times <= 1; times ++)
        {
            for (count; count <= x; count ++)
              {
                printf("Here is the random problem number %d \n", count);
                op = rand()%4;
                num1 = (rand()%101)-50;
                num2 = (rand()%101)-50;
              {
    
                  if(op == 0) /*this is where the operations start*/
                  {
                    answer = num1 + num2;
                    printf("%d + %d=?\n\n", num1, num2);
                    printf("\nAnswer:");
                    scanf("%f", &answer2);
                  }
    
                  else if (op == 1)
                  {
                    answer = num1 - num2;
                    printf("%d - %d=?\n\n", num1, num2);
                    printf("\nAnswer:");
                    scanf("%f", &answer2);
                  }
                  else if (op == 2)
                  {
                    answer = num1 * num2;
                    printf("%d * %d=?\n\n", num1, num2);
                    printf("\nAnswer:");
                    scanf("%f", &answer2);
                  }
                  else if (op == 3)
                  {
                    answer = num1 / num2;
                    printf("%d / %d=?\n\n", num1, num2);
                    printf("\nAnswer:");
                    scanf("%f", &answer2);
                  }
    
             while (answer!=answer2) 
                 {
    
                printf("\nYou got %d/%d correct. That is %d%!\n", answer, answer2);
    
                 }
              }
    
            }
    
        }
    
        return 0;
    }

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well at least it has some indentation now.

    > printf("\nYou got %d/%d correct. That is %d%!\n", answer, answer2);
    Look at this line
    1. Count the % conversions, and look how many parameters you're passing.
    2. Look at each type of conversion (say %d), and cross-reference with the types of the variables (say double). You can't print a double using %d.
    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.

  7. #7
    Registered User
    Join Date
    Jun 2012
    Posts
    15
    Thanks Salem, still learning to be like you guys.

  8. #8
    Registered User
    Join Date
    Jun 2012
    Posts
    15
    Total correct answers over total problems, then figure the percentage. see attached image. also the code;
    Code:
    #include <stdio.h>
     #include <stdlib.h>
     #include <math.h>
     #include<time.h>
    
    int main(void)  {
    
      int count = 1,i, x, nCount = 1, times = 1;
      int num1, num2, op;
      srand(time(0));
      float answers = 0.0f, answers2 = 0.0f;
    
       printf("Welcome, how many problems would you like?\n");
       scanf("%d", &x);
    
       for (count; count <= x; count++) 
      {
          for (count; count <= x; count ++) 
          {
            printf("Here is the random problem number %d \n", count);
            op = rand() % 4;
            num1 = (rand() % 101) - 50;
            num2 = (rand() % 101) - 50;
            if (op == 0) { 
           /*this is where the operations start */
        
           answers = num1 + num2;
           printf("%d + %d=?\n\n", num1, num2);
           printf("\nanswer:");
           scanf("%f", &answers2);
           }
        else if (op == 1) 
         {
          answers = num1 + num2;
          printf("%d - %d=?\n\n", num1, num2);
          printf("\nanswer:");
          scanf("%f", &answers2);
           }
         else if (op == 2) 
          {
           answers = num1 * num2;
           printf("%d * %d=?\n\n", num1, num2);
           printf("\nanswer:");
           scanf("%f", &answers2);
           }
        else if (op == 3)
          {
          answers = roundf((((num1 * 1.0f) / num2)) * 100.0f)/100.0f;
          printf("%d / %d=?\n\n", num1, num2);
          printf("\nanswer:");
          scanf("%f", &answers2);
          answers2 = roundf((answers2) * 100.0f)/100.0f;
           }
       }
       for (count = 0; count < times; count++)
           for (times=0; times<=x; times++)
           times = times + 1;
    
    
         printf("\nYou got %0.0f out of %0.0f correct. That is %d percent!\n",   count++, x);
    
       return 0;
    }
    when I compile it, I see same equation keep repeating.-image1-jpg
    Last edited by KumarSingh; 06-22-2012 at 05:23 PM.

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    >> printf("\nYou got %0.0f out of %0.0f correct. That is %d percent!\n", count++, x);

    Three conversions, two numbers. You've already been told to pass the correct number of arguments. Also make sure that the types of the variables match the conversions.

    I think you're doing the percentage completely wrong: part / whole * 100

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Your code compiles!?
    Code:
    $ gcc -Wall -Wextra -ansi foo.c
    foo.c: In function `main':
    foo.c:48: warning: implicit declaration of function `roundf'
    foo.c:60: warning: double format, different type arg (arg 2)
    foo.c:60: warning: double format, different type arg (arg 3)
    foo.c:60: warning: too few arguments for format
    foo.c:60: warning: double format, different type arg (arg 2)
    foo.c:60: warning: double format, different type arg (arg 3)
    foo.c:60: warning: too few arguments for format
    foo.c:63: error: parse error at end of input
    foo.c:8: warning: unused variable `i'
    foo.c:8: warning: unused variable `nCount'
    It's missing a closing brace.

    See all those warnings on line 60.
    That's due to all the inconsistencies in your printf call.

    You would do yourself a favour if you got the gcc compiler (or an IDE that comes with gcc pre-configured, such as code::blocks). Then you'll be able to check for a lot more suspect things in your code before running it (only to find it doesn't work).

    What is the purpose of this?
    Code:
       for (count; count <= x; count++) 
      {
          for (count; count <= x; count ++)
    If you just deleted one of them, I suspect things would be a lot better.
    You should say
    for (count=1; count <= x; count ++)
    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.

  11. #11
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Also mystified by this:

    Quote Originally Posted by KumarSingh View Post
    Code:
       for (count = 0; count < times; count++)
           for (times=0; times<=x; times++)
           times = times + 1;
    No idea what this is meant to be doing. Pretty sure it's not doing whatever it's meant to.

    To work out the percentage you need to know the total number of questions (which you do: it's x) and the number of correct answers (which you don't). You need to check the answers given are right and keep count of the number correct. I'd focus on that and worry about the percentage later.

    Code:
    answers = roundf((((num1 * 1.0f) / num2)) * 100.0f)/100.0f;
    You can use a cast rather than multiplying by 1.0f to get a float type:
    Code:
    answers = roundf(((float)num1 / num2) * 100.0f)/100.0f;
    That's quite a nifty way of getting 2 decimal places. I like!
    Beware that roundf() isn't standard though (I think it's gcc specific) so if this needs to be compilable with other compilers you should fine an alternative way of rounding.

  12. #12
    Registered User
    Join Date
    Jun 2012
    Posts
    15
    I removed that part
    Code:
    for (count; count <= x; count ++)
    and it doesn't change anything. the only part am stuck is getting the right code for right answers/total answers and the then the code for what percentage was correct.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to avoid repeating
    By krakatao in forum C Programming
    Replies: 1
    Last Post: 04-11-2012, 11:34 AM
  2. Repeating Decimals
    By jaypeebee18 in forum C Programming
    Replies: 11
    Last Post: 08-03-2008, 09:47 AM
  3. repeating a function
    By Mythic Fr0st in forum C++ Programming
    Replies: 2
    Last Post: 01-17-2007, 10:32 PM
  4. repeating strings
    By h_howee in forum C++ Programming
    Replies: 9
    Last Post: 11-29-2006, 12:30 AM
  5. What the? Loop repeating to much?
    By Blackroot in forum C++ Programming
    Replies: 4
    Last Post: 02-03-2006, 05:17 PM