Hi, Quiz C program Assignment updated

This is a discussion on Hi, Quiz C program Assignment updated within the C Programming forums, part of the General Programming Boards category; Hi, Quiz C program I'm a first year CS student, and we have our first assignment. We have to make ...

  1. #1
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629

    Hi, Quiz C program Assignment updated

    Hi, Quiz C program
    I'm a first year CS student, and we have our first assignment. We have to make a quiz with a menu that will keep re-displaying
    1.
    2. and so on. But i can't enter 2 without choosing 1 and i cant display 3 without choosing 1 and answering 2. I've written the whole code..but the validation is getting to me. I have no idea what to do..i have used if and while loops but i keep getting blocked out. If use characters like underscore the loop i use to redisplay the menu goes crazy..so i have to find a way to fix that/ but I'm not sure how to tell the scanf to display error if character is inputted. Also, I'm thinking of using char option..as you'll see in my source code..well i used that..but default: always exits out of the loop and i cant tell it to redisplay instead of exiting.
    Please help and i hope u understand what i'm trying to say lol..
    Oh and however this code is crap im proud of it..its the longest code i've ever written..so no plagiarism! I have till Nov. 19 so ur help will be appreciated as soon as possible thanks.
    I found a way to get option 3 if u answered the question and ur in the main menu. Butif you haven't answered the questions, and u entered 3..the code isnt doing what i want it to do..This is the question: in case you don't understand
    Assignment 1

    Due Date: Thursday, 19th November, 2009

    You are required to develop a program that will simulate a mathematics quiz game. The game will include various features, each of which must be incorporated into your program.

    Your program should be menu-driven and must display a main menu when run. The menu should include the following options:

    1. Enter the number of questions to be asked for this round of the quiz (maximum of 5 questions allowed).
    2. Start quiz
    3. Display the number of questions that were answered (i) correctly and (ii) incorrectly for this round.
    4. Exit Program



    Criteria to include:

    (a) Your program should continually run and re-display the main menu after each option has completed, i.e. start over again. Only when the customer enters option 4 (i.e. Exit Program), should your program terminate.

    (b) For option 2, your program should display a simple mathematical question and ask the user to enter the answer. After the user has entered their answer, your program should display the correct answer beside the user's answer. Your program should then ask the next question (if there are more to ask) and continue this sequence.

    For example:

    Question 1: 9 / 3 + 6 = ?
    6
    You entered: 6 - Wrong !! The correct answer: 9

    (c) After the user selects option 2, then finishes the quiz and your program returns to the main menu, your program should not allow the user to select option 2 again until either option 3 or 4 is selected.

    (d) Try to take account of input errors by the user and display appropriate error messages.
    And this is my updated code. I tried char for the switches, but it exits the loop..
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    /*this code is for to ask a number of questions in a quiz contest re-displaying a menu */
    main() 
    {
        int i ;
        int one, two, three, four ;
        int answer1 = 2 ;
        int answer2 = 9 ;
        int answer3 = 24;
        int answer4 = 100;
        char yesno ;
        int num1 = 0;
        int option = 0 ;
        do
        {
                  
                                printf("         \n   ") ;
                                printf("      ((----))\n") ;
                                printf("      <--<\\\\_//>->\n") ;
                                printf("           | |   \n") ;
                                printf("          (===)\n") ;
                                printf("\n") ;
                                printf("    QUIZZITCH  CUP\n") ;//This is just some drawing of the prize cup that the winner would have
        
                printf("\n") ;
            
        
            printf("1. Enter the number of questions to be asked for this round of the quiz\n") ; 
            printf("2. Start quiz\n") ;
            printf("3. Display the numbers of questions that were (i) correctly (ii) incorrectly\n") ;
            printf("4. Exit Program\n") ;
            scanf("%d", &option) ;//waits for an input for 1, 2, 3 or 4
         
            while(option != 1&&option!=4&&option!=3) //if option is not =1 it willforce user to enter1. but if user chooses 3..it would go to the other while loop//this enables it to display the results of option3 if variable num1 isn't empty. if it is empty and user chose 3..then menu would be redisplayed
            {
                printf("You must choose 1 before you can proceed to quiz\n") ;
                scanf("%d", &option) ;
                if(option==4)
                {
                    return 0;
                }
                i++ ;
                  
            }    
    
            while(option!=3)
            {
                if(option == 3 && num1 == 0)
                {   
                    printf("You must choose 1 before you can proceed to menu\n") ;
                    scanf("%d", &option) ;
                }
                else
                {
                        break ;
                }
                    i++ ;
            }
    
           
            switch(option) //the switch option allows me to choose number of questions i want .
            {
                case 1:
                {
                    
                    printf("1. One question\n") ;
              
                    printf("2. Two questions\n") ; 
                
                    printf("3. Three questions\n") ;
               
                    printf("4. Four questions\n") ; 
                
                    scanf("%d", &num1) ; //this scanf to read in the option for the number questions to be answered
                    while(num1 != 1 && num1 != 2&& num1 != 3 && num1 != 4)
                    {
                        printf("You must choose from 1-4 before you can proceed to quiz\n") ;
                        scanf("%d", &num1) ;
                        i++ ;
                    }
                }
                
                
                case 2: 
                {
                    
                    switch(num1)//start of option 2 using num1 as a variable of numbers of questions to be asked 
                    {
                        case 1:
                        {
                            printf("What is 1+1: *\n") ;
                            scanf("%d", &one) ;//the answer is stored in a variable one, two, three, four, and matched with answer1-5. if it matches..then result is displayed
                            if(one == answer1)
                            {
                                printf("%d is Correct!\n",answer1) ;
                                
                            }
                            else
                            {
                                printf("Incorrect!Answer is: %d\n",answer1) ;
                                
                            }
                           
                            break;
                        }//end of case 1 of question 1 
                        
                        case 2:
                        {   
                            printf("1. what is 1+1: *\n") ;
                            scanf("%d", &one) ;
                            if(one == answer1) 
                            {
                                printf("Correct!\n") ;
                                
                            }
                            else 
                            {
                                printf("Incorrect! Answer is: %d\n", answer1) ;
                            }
                
                            printf("2. what is 3 * 3: *\n") ;
                            scanf("%d", &two) ;
                            if(two == answer2)
                            {
                                printf("Correct!\n") ;
                            }
                            else
                            {
                                printf("Incorrect! Answer is: %d\n", answer2) ;
                            }
                            break ;
                        }//end of question 2
                        //case 3
                        
                      
                        case 3:
                        { 
                              
                            printf("1. what is 1+1: *\n") ;
                            scanf("%d", &one) ;
                            if(one == answer1)
                            {
                               
                                printf("Correct!\n") ;
                            }
                            else
                            {
                                
                                printf("Incorrect! Answer is %d\n", answer1) ;
                            }
                    
                            printf("2. what is 3 * 3: *\n") ;
                            scanf("%d", &two) ;
                       
                            if(two == answer2)
                            {
                              
                                printf("Correct!\n") ;
                            }
                            else
                            {
                               
                                printf("Incorrect! Answer is %d\n", answer2) ;
                            }
                            
                            printf("3. what is 4!: *\n") ;
                            scanf("%d", &three) ;
                            
                            if(three == answer3)
                            {
                                
                                printf("Correct!\n") ;
                            }
                            else
                            {
        
                                printf("Incorrect! Answer is %d\n",answer3) ;
                            }
                            
                            break ;
                        }
                        case 4:
                        {
                            printf("1. what is 1+1: *\n") ;
                            scanf("%d", &one) ;
                            if(one == answer1)
                            {
                                printf("Correct!\n") ;
                            }
                            else
                            {
                                printf("Incorrect! Answer is %d\n", answer1) ;//one
                            }
                            //two
                            printf("2. what is 3 * 3: * \n") ;
                            scanf("%d", &two) ;
                            if(two == answer2)
                            {
                                printf("Correct!\n") ;
                            }
                            else
                            {
                                printf("Incorrect! Answer is %d\n",answer2) ;
                            }
                            //two
                            printf("3. what is 4!: *\n") ;
                            scanf("%d", &three) ;
                            if(three ==answer3)
                            {
                                printf("Correct!\n") ;
                            }
                            else
                            {
                                printf("Incorrect! Answer is %d\n", answer3) ;
                            }
                            printf("4. what is 800/2*4: *\n") ;
                            scanf("%d", &four) ;
                        
                            if(four == answer4)
                            {
                                printf("Correct!\n") ;
                            }
                            else
                            {
                               printf("Incorrect! Answer is %d\n", answer4) ;
                            }
                            break ; 
                        }
                        
                    }
                   break ;
                }//end of case 2 of switch(option)
                
              
                case 3:                  //beginning of case 3. case 3 shows how many questions the user answer correctly
                {
                    if(num1==1)
                    {
                        //by checking the various possibilities the user could have answered the question
                        if(one==2)
                        {
                            printf("i. You got 1 Correct!\n") ;
                        }
                        else
                        {
                            printf("ii. You got 1 Incorrect!!\n") ;
                        }
                        break ; 
                    }
                    if(num1==2) 
                    {
                        if(one == 2 && two == 9)
                        {
                            printf("i. You got 2 Correct!\n") ;
                            printf("ii. You got none Incorrect!!\n") ;
                        }
                        else if(one == 2 && two!= 9||one!=2 && two == 9)
                        {
                            printf("i. You got 1 Correct!\n") ;
                        }
                        else
                        {
                            printf("ii. You got Everything Incorrect!!\n") ;
                        }
                        break ;
                    }
                              
                    if(num1==3)
                    {
                        if(one != 2 && two !=9 && three == 24||one != 2 && two == 9 && three != 24||one == 2 && two != 9 && three != 24)
                        {
                            printf("i. You got 1 Correct!\n") ;
                            printf("ii. You got 2 Incorrect!!\n") ;
                           
                        }
                       
                            
                        else if(one == 2 && two == 9 && three != 24||one != 2 && two == 9 && three == 24||one == 2 && two != 9 && three == 24)
                        {
                            printf("i. You got 2 Correct!\n") ;
                            printf("ii. You got 1 Incorrect!!\n") ;
                           
                        }
                      
                        else if(one==2 && two==9 && three==24)
                        {
                            printf("i. You got Everything Correct\n") ;
                        }
                        else
                        {
                            printf("i. You got Everything Incorrect!!\n") ;
                        }
                        
                    }
                    
                    if(num1==4)
                    {
                        if(one == 2 && two !=9 && three != 24 && four != 100||one != 2 && two == 9 && three != 24 && four != 100||one != 2 && two != 9 && three == 24 && four!= 100||one != 2 && two != 9 && three != 24 && four==100)
                        {
                            printf("i.  You got 1 correct!\n") ;
                            printf("ii. You got 3 incorrect!!\n") ;
                           
                        }
                        
                        else if(one != 2 && two != 9 && three == 24 && four == 100||one == 2 && two == 9 && three != 24 && four != 100|| one != 2 && two == 9 && three == 24 && four != 100|| one == 2 && two != 9 && three != 24 && four == 100)
                        {
                            printf("i. you got 2 correct\n") ;
                            printf("ii. you got 2 incorrect\n") ;
                            
                        }
                        else if(one != 2 && two == 9 && three == 24 && four == 100||one == 2 && two != 9 && three == 24 && four == 100||one == 2 && two == 9 && three != 24 && four== 100||one == 2 && two == 9 && three == 24 && four!= 100)
                        {
                            printf("i. You got 3 Correct!\n") ;
                            printf("ii. You got 1 Incorrect!!\n") ;
                         
                        }
                        else if(one == 2 && two == 9 && three == 24 && four == 100)
                        {
                            printf("i. You got everything Correct\n") ;
                           
                        }
                        else
                        {
                            printf("i. you got everything Incorrect\n") ;
                        }
                        break;
                    }
                    break ;
                }//end of switch(num1)
                
                case 4:
                { 
                    printf("Are you sure you want to exit quiz?\n") ;
                    printf("Y/N") ;
                    printf("\n") ;
                    scanf("%1s", &yesno) ;
                    if(yesno== 'Y'|| yesno == 'y')
                    {
                        exit(0) ;
                    }
                    clrscr() ;
                    break ; /*user must input y to quit*/
                }
            
                default: 
                {
                    printf("Can't you follow simple instructions?\n") ;
                    
                    break ; 
                }
                 
            }
        }while(option==1 || option==2 ||option==3 || option == 4) ;
        
        getch() ;
    }
    No, if I choose 3 in main menu..it should have followed the code which is up..but it doesnt
    and im still stuck with the problem of char.if u type in _ or = or any whitespace the loop goes infinite..please help
    im pure nervous lol..ideas please!!

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Calm down Eman. You are trying to deal with too much at once.

    scanf() has a return value. You should check that.

    Also, our #1 problem here at cboard IMO involves the stdin buffer and that is probably the case here again. Here is something I wrote about it yesterday, take a look at that and tell us if it makes sense:

    Why my while loop is not working ??? pls help

    It refers to getchar(), but scanf() can have the same consequences.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by MK27 View Post
    Calm down Eman. You are trying to deal with too much at once.

    scanf() has a return value. You should check that.

    Also, our #1 problem here at cboard IMO involves the stdin buffer and that is probably the case here again. Here is something I wrote about it yesterday, take a look at that and tell us if it makes sense:

    Why my while loop is not working ??? pls help

    It refers to getchar(), but scanf() can have the same consequences.
    thanks man for replying..i had no idea someone had replied, i was relying on a e-mail
    i found a way to stop the program from crashing..using another while loop until a valid character is entered...i used %1s not %d..i still have no idea how to solve that problem...
    do u know if there is another way to shorten the code? its too long i think

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You're not using loops very well, so your code is very long.

    In your first switch statement - that's a fright. All you need is to ask them how many questions they want and then have a while or for loop to handle the result - be it print out, taking guesses, whatever.

    you should *not* be coding "1 + 1", and then repeating it two more times, to handle three questions.


    get number of questions the user wants, then:
    Code:
    for( i = 0; i < NumberOfQuestions; i++) {
        //"1+1" questions, go in here.
    }
    And please don't have "you got 1 correct" and "you got 2 correct" all hard coded - use variables FCOL!
    printf("\n You got %d correct", correctVariable)

    MK you're being either *way* generous, or very busy. This code should be 1/2 this size, max.
    Last edited by Adak; 11-13-2009 at 01:39 PM.

  5. #5
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by Adak View Post
    You're not using loops very well, so your code is very long.

    In your first switch statement - that's a fright. All you need is to ask them how many questions they want and then have a while or for loop to handle the result - be it print out, taking guesses, whatever.

    you should *not* be coding "1 + 1", and then repeating it two more times, to handle three questions.


    get number of questions the user wants, then:
    Code:
    for( i = 0; i < NumberOfQuestions; i++) {
        //"1+1" questions, go in here.
    }
    And please don't have "you got 1 correct" and "you got 2 correct" all hard coded - use variables FCOL!
    printf("\n You got %d correct", correctVariable)

    MK you're being either *way* generous, or very busy. This code should be 1/2 this size, max.
    I know i'm a dreadful programmer...i knew there was a shorter way to do it..can u please elaborate on the for loop?
    do i have to use a nested for loop? how would i print the questins in or out..please help. thanks

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Let me run your program and see what you're trying to do. I read through it, but it has so much *STUFF* in it, I can't see the forest for the tree's.

    Back in a bit.

  7. #7
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by Adak View Post
    Let me run your program and see what you're trying to do. I read through it, but it has so much *STUFF* in it, I can't see the forest for the tree's.

    Back in a bit.
    lol thanks..im reluctant to do this..but if u wanna see the question it is Michael Collins - Lecturer, School of Computing, DIT Kevin Street, Dublin. ..it shows the requirement..i tried the for loop but i cant get it to work properly..like if num1 is 2..so print two questions and use two scan..maybe im bad at this stuff..im not creative

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Aside from missing a return on the end of main(), it compiles and runs.

    You show the correct and incorrect number of guesses only for the last round of questions and their answers. So when I do two rounds in a row, it doesn't show the first round correct and incorrect numbers.

    Is that OK?

    I'll let you deal with the assignment. Just post up what doesn't work, OK?

    I'll put up an example after bit.

  9. #9
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by Adak View Post
    Aside from missing a return on the end of main(), it compiles and runs.

    You show the correct and incorrect number of guesses only for the last round of questions and their answers. So when I do two rounds in a row, it doesn't show the first round correct and incorrect numbers.

    Is that OK?

    I'll let you deal with the assignment. Just post up what doesn't work, OK?

    I'll put up an example after bit.
    to say i'm a bit lost is an understatement..it compiles for me..out of interest what compiler do u use for c? can u use visual c++?

    I'm trying to display the number of questions according to what the user wants as u suggested with the for loop..my problem is the loop isnt working properly..it prints the first loop once.the second twice, the third thrice and so on..when it shouldnt.i was thinking i would do correct and incorrect guesses later..

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    With programming, KISS is a great guide. Keep it as simple, as possible, while still getting the right answer, in a good run-time, with a good user interface.

    This is more like what I'd recommend:

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    void summary(int correct, int wrong); //function declaration
    
    /*this code is for to ask a number of questions in a quiz contest re-displaying a menu */
    
    int main() 
    {
        char yesno ;
        int i, correct, option, wrong, answer, num1;
        correct = wrong = 0;
    
        do
        {
                                printf("         \n\n   ") ;
                                printf("      ((---))\n") ;
                                printf("      <--<\\\\_//>-->\n") ;
                                printf("           | |   \n") ;
                                printf("          (===)\n") ;
                                printf("\n") ;
                                printf("    QUIZZITCH  CUP\n") ;//This is just some drawing of the prize cup that the winner would have
        
                printf("\n") ;
            
            printf("1. Enter the number of questions to be asked for this round of the quiz\n") ; 
            printf("2. Start quiz\n") ;
            printf("3. Display the numbers of questions that were (i) correctly (ii) incorrectly\n") ;
            printf("4. Exit Program\n") ;
    
            scanf("%d", &option) ;//waits for an input for 1, 2, 3 or 4
            if(option < 1 || option > 4) {
              printf("\n Valid answers are 1, 2, 3, or 4\n");
              i = getchar();
              continue;
            }
            switch(option) //handles all the menu choices
            {
                case 1: 
                  do {
                    printf("\n How many questions would you like to be asked? [1-4]: ");
                    scanf("%d", &num1) ;
                    if(num1 < 1 || num1 > 4) {
                      i = getchar();
                      printf("\n Valid answers are 1, 2, 3, or 4\n");
                    }  
                  }while(num1 < 1 || num1 > 4);
                  break;
                case 2:   //handles all the questions
                  for(i = 0; i < num1; i++) {
                    printf("\n What is 1 + %d ? ", i+1);
                    scanf("%d", &answer);
                    if(answer == 2+i) {
                      printf("%d is Correct!\n", answer) ;
                      correct++;
                    }
                    else {
                      printf("Incorrect! Answer is: %d\n", (2+i)) ;
                      wrong++;
                    }
                  }
                  break;
                case 3: 
                  summary(correct, wrong);
                  break;
                case 4:
                  printf("Are you sure you want to exit quiz?\n") ;
                  printf("Y/N") ;
                  printf("\n") ;
                  scanf("%1s", &yesno) ;
                  if(yesno== 'Y'|| yesno == 'y')
                     option = 5;
                  break;
                default: 
                  printf("Can't you follow simple instructions?\n") ;
                  break ; 
            }
        }while(option > 0 && option < 5) ;
       
        if(correct || wrong) 
          printf("\n For All Rounds:\n ===============");
        summary(correct, wrong);
        printf("\n\n\t\t\t     press enter when ready\n");
        getch() ;
    
        return 0;  
    }
    void summary(int correct, int wrong) {
    
        if(correct && !wrong) {
          if(correct < 2)
            printf("\n Your answer was correct!", correct);
          else
            printf("\n All %d answers were correct!", correct);
        }
        if(correct && wrong) {
          if(correct < 2 && wrong < 2)
            printf("\n You have one answer correct, and one answer wrong");
          else if(correct < 2 && wrong > 1)
            printf("\n You have one answer correct, and %d answers wrong", wrong);
          else if(correct > 1 && wrong < 2)
            printf("\n You have %d answers correct, and one answer wrong", correct);
          else
            printf("\n You have %d answers correct, and %d answers wrong", correct, wrong);
    
        }
        if(!correct && wrong) 
          if(wrong < 2)
            printf("\n You have one answer wrong!");
          else
            printf("\n You have all %d answers wrong!", wrong);        
    }
    I saw the conio.h header file, so I used Turbo C for this. I have Visual C/C++, but only use it for bigger programs that need the memory, etc.

  11. #11
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by Adak View Post
    With programming, KISS is a great guide. Keep it as simple, as possible, while still getting the right answer, in a good run-time, with a good user interface.

    This is more like what I'd recommend:

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    void summary(int correct, int wrong); //function declaration
    
    /*this code is for to ask a number of questions in a quiz contest re-displaying a menu */
    
    int main() 
    {
        char yesno ;
        int i, correct, option, wrong, answer, num1;
        correct = wrong = 0;
    
        do
        {
                                printf("         \n\n   ") ;
                                printf("      ((---))\n") ;
                                printf("      <--<\\\\_//>-->\n") ;
                                printf("           | |   \n") ;
                                printf("          (===)\n") ;
                                printf("\n") ;
                                printf("    QUIZZITCH  CUP\n") ;//This is just some drawing of the prize cup that the winner would have
        
                printf("\n") ;
            
            printf("1. Enter the number of questions to be asked for this round of the quiz\n") ; 
            printf("2. Start quiz\n") ;
            printf("3. Display the numbers of questions that were (i) correctly (ii) incorrectly\n") ;
            printf("4. Exit Program\n") ;
    
            scanf("%d", &option) ;//waits for an input for 1, 2, 3 or 4
            if(option < 1 || option > 4) {
              printf("\n Valid answers are 1, 2, 3, or 4\n");
              i = getchar();
              continue;
            }
            switch(option) //handles all the menu choices
            {
                case 1: 
                  do {
                    printf("\n How many questions would you like to be asked? [1-4]: ");
                    scanf("%d", &num1) ;
                    if(num1 < 1 || num1 > 4) {
                      i = getchar();
                      printf("\n Valid answers are 1, 2, 3, or 4\n");
                    }  
                  }while(num1 < 1 || num1 > 4);
                  break;
                case 2:   //handles all the questions
                  for(i = 0; i < num1; i++) {
                    printf("\n What is 1 + %d ? ", i+1);
                    scanf("%d", &answer);
                    if(answer == 2+i) {
                      printf("%d is Correct!\n", answer) ;
                      correct++;
                    }
                    else {
                      printf("Incorrect! Answer is: %d\n", (2+i)) ;
                      wrong++;
                    }
                  }
                  break;
                case 3: 
                  summary(correct, wrong);
                  break;
                case 4:
                  printf("Are you sure you want to exit quiz?\n") ;
                  printf("Y/N") ;
                  printf("\n") ;
                  scanf("%1s", &yesno) ;
                  if(yesno== 'Y'|| yesno == 'y')
                     option = 5;
                  break;
                default: 
                  printf("Can't you follow simple instructions?\n") ;
                  break ; 
            }
        }while(option > 0 && option < 5) ;
       
        if(correct || wrong) 
          printf("\n For All Rounds:\n ===============");
        summary(correct, wrong);
        printf("\n\n\t\t\t     press enter when ready\n");
        getch() ;
    
        return 0;  
    }
    void summary(int correct, int wrong) {
    
        if(correct && !wrong) {
          if(correct < 2)
            printf("\n Your answer was correct!", correct);
          else
            printf("\n All %d answers were correct!", correct);
        }
        if(correct && wrong) {
          if(correct < 2 && wrong < 2)
            printf("\n You have one answer correct, and one answer wrong");
          else if(correct < 2 && wrong > 1)
            printf("\n You have one answer correct, and %d answers wrong", wrong);
          else if(correct > 1 && wrong < 2)
            printf("\n You have %d answers correct, and one answer wrong", correct);
          else
            printf("\n You have %d answers correct, and %d answers wrong", correct, wrong);
    
        }
        if(!correct && wrong) 
          if(wrong < 2)
            printf("\n You have one answer wrong!");
          else
            printf("\n You have all %d answers wrong!", wrong);        
    }
    I saw the conio.h header file, so I used Turbo C for this. I have Visual C/C++, but only use it for bigger programs that need the memory, etc.
    Adak, you are incrementing only i..but i have different questions and operators..do you think it is possible to use a for loop to change the operators? Since i'm using different questions and operators..is it not good that way,..unless there is a way to change the operators that i don't know off
    Last edited by Eman; 11-14-2009 at 07:08 AM.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Adak, you are incrementing only i..but i have different questions and operators..do you think it is possible to use a for loop to change the operators? Since i'm using different questions and operators..is it not good that way,..unless there is a way to change the operators that i don't know off
    Yes, you can have any number of operators that you need. If you can use variable names then it helps to shorten the whole program:

    Code:
    multiply1 = 5 ;
    multiply2 = 7;
    
    printf("\nWhat is %d * %d ?", multiply1, multiply2);
    scanf("%d", &answer);
    
    if(answer == (multiply1 * multiply1))
       printf("\nCorrect!"); 
    else 
      printf("\nIncorrect! The answer is: %d" multiply1 * multiply2);
      
    //etc. And you can use multiply1 and multiply2 for any value, over and over.
    I just used i as an example. You could have add1 and add2, subtract1 and subtract2, divide1 and divide2. You could use any
    of them with each other, or interact them with i - but don't change i of course!
    Last edited by Adak; 11-14-2009 at 10:33 AM.

  13. #13
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by Adak View Post
    Yes, you can have any number of operators that you need. If you can use variable names then it helps to shorten the whole program:

    Code:
    multiply1 = 5 ;
    multiply2 = 7;
    
    printf("\nWhat is %d * %d ?", multiply1, multiply2);
    scanf("%d", &answer);
    
    if(answer == (multiply1 * multiply1))
       printf("\nCorrect!"); 
    else 
      printf("\nIncorrect! The answer is: %d" multiply1 * multiply2);
      
    //etc. And you can use multiply1 and multiply2 for any value, over and over.
    I just used i as an example. You could have add1 and add2, subtract1 and subtract2, divide1 and divide2. You could use any
    of them with each other, or interact them with i - but don't change i of course!
    Yeah i know but i mean..i cant put hem in the same for loop..cause it will print all the questions even if i<num1 . i willshow u an example

  14. #14
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Code:
     switch(option) //the switch option allows me to choose number of questions i want .
            {
                case 1:
                {
                    
                    printf("1. One question\n") ;
              
                    printf("2. Two questions\n") ; 
                
                    printf("3. Three questions\n") ;
               
                    printf("4. Four questions\n") ; 
                
                    scanf("%d", &num1) ; //this scanf to read in the option for the number questions to be answered
                    while(num1 != 1 && num1 != 2&& num1 != 3 && num1 != 4)
                    {
                        printf("You must choose from 1-4 before you can proceed to quiz\n") ;
                        scanf("%d", &num1) ;
                        i++ ;
                    }
                    clrscr() ;
                }
                
                
                case 2: 
                {
                    for(i=0; i<num1; i++) 
                    {
                        printf("what is %d+2?\n", i+2) ;
                        scanf("%d", &input1) ;
                        printf("what is %d*2\n", i+3) ;
                        scanf("%d", &input2) ;
                    }
                }
    so if i choose option 1 it will still print twice..
    if i choose option 2..it will print 4 times
    this is making me go bonkers lol
    i could only use plus for the loop but thats boring
    Last edited by Eman; 11-14-2009 at 11:58 AM.

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You don't have to limit it to using the loop counter.

    Stick in your own separate variable, and you can set it up with a random number each time you want a number.

    Check out rand() and other random functions, on your compiler?

    And what compiler are you using, btw? Visual C?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  2. Help with a C program assignment. Please?
    By Loopah in forum C Programming
    Replies: 3
    Last Post: 11-16-2005, 08:37 PM
  3. Help with Programming Assignment
    By JHaney in forum C++ Programming
    Replies: 18
    Last Post: 11-08-2005, 03:45 AM
  4. Replies: 2
    Last Post: 05-10-2002, 05:16 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21