Thread: urgent need hlp..i'm develop a math quiz program thy was several error

  1. #1
    Registered User
    Join Date
    Jul 2017
    Posts
    48

    Unhappy urgent need hlp..i'm develop a math quiz program thy was several error

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    #pragma warning(disable:4996)
    int menu();
    void additionmenu();
    void subtractionmenu();
    void multiplicationmenu();
    
    
    
    
    void main()
    {
        int choice;
        //printf("You have selected the choice %d",menu());
    
    
        if (choice == 1)
        {
            printf("You have selected addition\n");
            additionmenu();
        }
        else if (choice == 2)
        {
            printf("You have selected subtraction\n");
            subtractionmenu();
        }
        else if (choice == 3)
        {
            printf("You have selected multiplication\n");
            multiplicationmenu();
        }
        else
        {
            printf("Invalid choice\n");
        }
    
    
        system("pause");
    }
    int menu()
    {
        int choice;
        printf("1.addition\n");
        printf("2.subtraction\n");
        printf("3.multiplication\n");
        printf("Please choose either 1,2 or 3:");
        scanf("%d", &choice);
        return choice;
    
    
    }
    
    
    void additionmenu()
    {
        char choice;
        printf("Please select the level");
        printf("A level 1");
        printf("B level 2");
        printf("C level 3");
        scanf("%c", &choice);
        if (choice == A)
        {
            printf("You have selected level 1\n");
            additionmenu();
        }
        else if (choice == B)
        {
            printf("You have selected level 2\n");
            subtractionmenu();
        }
        else if (choice == C)
        {
            printf("You have selected level 3\n");
            multiplicationmenu();
        }
        else
        {
            printf("Invalid choice\n");
    
    
        }
    
    
        int n1, n2, pcans, userans;
        int count = 1, mark = 0;
        
    
    
        do {
            srand((unsigned int)time(NULL));
            n1 = rand() % 10;
            n2 = rand() % 10;
            pcans = n1 + n2;
    
    
            printf("%d.%d+%d>", count, n1, n2);
            scanf("%d", &userans);
            if (userans == pcans);
            {
                printf("You are right\n");
                mark = mark + 5;
            }
            else
            {
                printf("You are wrong\n");
            }
            count++;
        } while (count <= 3);
        printf("Your score is %d", mark);
    }



    its show error at line 58,63,68,84,96 pls help me

  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
    The first problem is you wrote too much code before pressing 'compile'.

    So instead of one or two errors, you have lots.

    Start here
    Code:
    int menu();
    int main()  //!! yes, really!, main returns int
    {
        int choice;
        choice = menu();
        printf("You have selected the choice %d\n",choice);
     
     
        system("pause");
    }
    int menu()
    {
        int choice;
        printf("1.addition\n");
        printf("2.subtraction\n");
        printf("3.multiplication\n");
        printf("Please choose either 1,2 or 3:");
        scanf("%d", &choice);
        return choice;
    }
    Test it to see that it compiles and works as expected.

    Then add small amounts of your other code, compiling and testing as you go.
    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
    Jul 2017
    Posts
    48
    its work but how about line 92 its say time is undifined and how i create a sub function under addition for level1,2 and 3

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    time() is defined in time.h

    > and how i create a sub function under addition for level1,2 and 3
    You already know how to create functions.

    So what about
    addEasy()
    addMedium()
    addDifficult()
    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.

  5. #5
    Registered User
    Join Date
    Jul 2017
    Posts
    48
    at line 57 or the beginning?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > at line 57 or the beginning?
    You already know that prototypes go at the start, and the implementations are later on in the file.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    #pragma warning(disable:4996)
    
    int menu();
    void additionmenu();
    void subtractionmenu();
    void multiplicationmenu();
    void addEasy();
    void addMedium();
    void addDifficult();
    
    int main()
    {
        int choice = menu();
        printf("You have selected the choice %d",choice);
    
        if (choice == 1)
        {
            printf("You have selected addition\n");
            additionmenu();
        }
        else if (choice == 2)
        {
            printf("You have selected subtraction\n");
            subtractionmenu();
        }
        else if (choice == 3)
        {
            printf("You have selected multiplication\n");
            multiplicationmenu();
        }
        else
        {
            printf("Invalid choice\n");
        }
        system("pause");
    }
    
    int menu()
    {
        int choice;
        printf("1.addition\n");
        printf("2.subtraction\n");
        printf("3.multiplication\n");
        printf("Please choose either 1,2 or 3:");
        scanf("%d", &choice);
        return choice;
    }
    
    void additionmenu()
    {
        char choice;
        printf("Please select the level");
        printf("A level 1");
        printf("B level 2");
        printf("C level 3");
        scanf(" %c", &choice);
        if (choice == 'A')
        {
            printf("You have selected level 1\n");
            addEasy();
        }
        else if (choice == 'B')
        {
            printf("You have selected level 2\n");
            addMedium();
        }
        else if (choice == 'C')
        {
            printf("You have selected level 3\n");
            addDifficult();
        }
        else
        {
            printf("Invalid choice\n");
        }
    }
    
    void subtractionmenu() {
      printf("To Do\n");
    }
    
    void multiplicationmenu() {
      printf("To Do\n");
    }
    
    void addEasy() {
      printf("That's easy\n");
    }
    
    void addMedium() {
      printf("That's a bit harder\n");
    }
    
    void addDifficult() {
      printf("That's really tricky\n");
    }
    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
    Jul 2017
    Posts
    48

    stop running after i choose easy,medium or diffcult,what is the problem

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    #include<time.h>
    #pragma warning(disable:4996)
    
    
    int menu();
    void additionmenu();
    void subtractionmenu();
    void multiplicationmenu();
    void addEasy();
    void addMedium();
    void addDifficult();
    
    
    int main()
    {
        int choice = menu();
        printf("You have selected the choice %d", choice);
    
    
        if (choice == 1)
        {
            printf("You have selected addition\n");
            additionmenu();
        }
        else if (choice == 2)
        {
            printf("You have selected subtraction\n");
            subtractionmenu();
        }
        else if (choice == 3)
        {
            printf("You have selected multiplication\n");
            multiplicationmenu();
        }
        else
        {
            printf("Invalid choice\n");
        }
        system("pause");
    }
    
    
    int menu()
    {
        int choice;
        printf("1.addition\n");
        printf("2.subtraction\n");
        printf("3.multiplication\n");
        printf("Please choose either 1,2 or 3:");
        scanf("%d", &choice);
        return choice;
    }
    
    
    void additionmenu()
    {
        char choice;
        printf("Please select the level");
        printf("A level 1");
        printf("B level 2");
        printf("C level 3");
        scanf("%c", &choice);
        if (choice == 'A' || choice == 'a')
        {
            printf("You have selected level 1\n");
            addEasy();
        }
        else if (choice == 'B' || choice == 'b')
        {
            printf("You have selected level 2\n");
            addMedium();
        }
        else if (choice == 'C' || choice == 'c')
        {
            printf("You have selected level 3\n");
            addDifficult();
        }
        else
        {
            printf("Invalid choice\n");
    
    
        }
    }
    
    
    
    
    void subtractionmenu() {
        printf("To Do\n");
    }
    
    
    void multiplicationmenu() {
        printf("To Do\n");
    }
    
    
    void addEasy() {
        printf("That's easy\n");
        
        {
    
    
            int n1, n2, pcans, userans;
            int count = 1, mark = 0;
    
    
    
    
            do {
                srand((unsigned int)time(NULL));
                n1 = rand() % 10;
                n2 = rand() % 10;
                pcans = n1 + n2;
    
    
                printf("%d.%d+%d>", count, n1, n2);
                scanf("%d", &userans);
                if
                    (userans == pcans) {
                    printf("You are right\n");
                    mark = mark + 5;
                }
                else
                {
                    printf("You are wrong\n");
                }
                count++;
            } while (count <= 3);
            printf("Your score is %d", mark);
        }
    
    
    
    
    }
    
    
    void addMedium() {
        printf("That's a bit harder\n");
    }
    
    
    void addDifficult() {
        printf("That's really tricky\n");
    }
    its stop running after i choose the addEasy and why its display invalid choice before i choose? cannot jump to the addEasy part and display question for user to answer
    Last edited by kkkcj; 07-25-2017 at 03:38 AM.

  8. #8
    Registered User
    Join Date
    Jul 2017
    Posts
    2
    You need to clear the input buffer.
    scanf() reads from input buffer, but doesn't clear it.

    Pleae read about c functions before using them, internet is full of documentation.

    possible solution
    fflush(stdin);
    or while ( getchar() != '\n' );
    Last edited by Horror; 07-25-2017 at 07:19 AM.

  9. #9
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Quote Originally Posted by Horror View Post
    possible solution
    fflush(stdin);
    NO! Never use fflush() on streams that was open for input or input/output where the last operation was input!
    Only use fflush() on streams that was open for output or input/output where the last operation was output.

    This results in undefined behavor!
    Other have classes, we are class

  10. #10
    Registered User
    Join Date
    Jul 2017
    Posts
    2
    Quote Originally Posted by WoodSTokk View Post
    NO! Never use fflush() on streams that was open for input or input/output where the last operation was input!
    Only use fflush() on streams that was open for output or input/output where the last operation was output.

    This results in undefined behavor!
    The standard on Linux is as follows

    For input streams, fflush() discards any buffered data that has been fetched from the underlying file, but has not been consumed by the application.

    Windows behaves like linux in this point.

    We use stdin, which is mostly a copy of the keyboard buffer.
    Please submit detailed information what system you are refering to and and an example to show how this undefined behavior interacts with stdin.

    Other than that, i consider it as quite safe for the example code above.

  11. #11
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,110
    "The standard on Linux is as follows"

    Please quote your source for the "standard"

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Considering that I wrote
    scanf(" %c", &choice); //!! YES, the leading space is important, it's not idle decoration
    to specifically address this buffer problem, one wonders who's paying attention.
    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.

  13. #13
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Quote Originally Posted by Horror View Post
    The standard on Linux is as follows

    For input streams, fflush() discards any buffered data that has been fetched from the underlying file, but has not been consumed by the application.

    Windows behaves like linux in this point.

    We use stdin, which is mostly a copy of the keyboard buffer.
    Please submit detailed information what system you are refering to and and an example to show how this undefined behavior interacts with stdin.

    Other than that, i consider it as quite safe for the example code above.
    The system doesn't matter. C is OS independent! Every beginner should learn programming with the C-Standard in mind.
    You can also learn features that some OSes add on top of the C-Standard, but then you know that this is not always available and not standard.
    If you learn programming for a specific OS, you will later run into problems if you develop a project on another OS and you will not know why there is a problem.
    So, the best practice in this forum is to teach programming along the C-Standard.
    Last edited by WoodSTokk; 07-25-2017 at 09:11 AM.
    Other have classes, we are class

  14. #14
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    I have a cunning plan regarding fflush. We'll see what the standard says!

    N1570 April 12, 2011 ISO/IEC 9899:201x

    If stream points to an output stream or an update stream in which the most recent operation was not input, the fflush function causes any unwritten data for that stream to be delivered to the host environment to be written to the file; otherwise, the behavior is undefined.
    Well that was easy to clear up Undefined behavior just as mentioned.

  15. #15
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by Horror View Post
    The standard on Linux is as follows

    For input streams, fflush() discards any buffered data that has been fetched from the underlying file, but has not been consumed by the application.

    Windows behaves like linux in this point.
    You pulled that out of your ass. Windows works that way, but linux doesn't.

    If you have access to a linux system, try the following. Enter two numbers on the same line in response to the first scanf. They will both be printed.
    Code:
    #include <stdio.h>
    
    int main() {
        int n;
    
        scanf("%d", &n);
        printf("%d\n", n);
    
        fflush(stdin);
    
        scanf("%d", &n);
        printf("%d\n", n);
    
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Math Quiz Program Help
    By HystericalFool in forum C Programming
    Replies: 5
    Last Post: 11-17-2014, 12:00 AM
  2. IDE or compilier to develop c program in windows
    By sharonch in forum Windows Programming
    Replies: 1
    Last Post: 11-09-2012, 02:40 AM
  3. Replies: 1
    Last Post: 11-21-2011, 08:27 AM
  4. making a math quiz program
    By mackieinva in forum C Programming
    Replies: 10
    Last Post: 09-17-2007, 03:38 PM
  5. I'm Requested to develop a C program
    By BadProgrammer in forum C Programming
    Replies: 16
    Last Post: 05-18-2003, 12:10 AM

Tags for this Thread