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; Originally Posted by Adak You don't have to limit it to using the loop counter. Stick in your own separate ...

  1. #16
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by Adak View Post
    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?
    I wish i could use visual C. im using Borland compiler..we've not gone as far as to use to rand() and i dont wanna introduce something foreign//we are as far as arrays..

  2. #17
    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.
    Hey man, what exactly does i=getchar() do? i is an index and an int..yet u assign a character to it? what does that mean?
    also this i my new code now..but ders a problem..if i type 123 at once..it doesnt show error..im using char as datat type for option..
    Code:
    //Author: Emmanuel Adenola
    //Group A, Nov 13/11/2009
    /*this code is for to ask a number of questions in a quiz contest re-displaying a menu */
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    main() 
    {
        int i, one=2, three=4, four= 4  ; 
        int answer=0, num1=0; 
        int correct=0, wrong=0 ; 
        char yesno; char 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("%1s", &option) ;//waits for an input for 1, 2, 3 or 4
            
           
            while(option < '1'&&option>'4') //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
            {
                  if(option!='1'&&num1!=0)
                  {
                      break ;
                  }
               
                if(i=getchar()) 
                {
                    printf("invalid\n");
                    break ;
                }
                printf("You must choose 1 before you can proceed to quiz\n") ;
                scanf("%1s", &option) ;
                if(option=='4'||option=='1')
                {
                    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)
                    {   
                        if(i=getchar()) ;
                        {
                            printf("invalid character\n") ;
                             printf("You must choose from 1-4 before you can proceed to quiz\n") ;
                            scanf("%d", &num1) ;
                        }
                        printf("Invalid choice. You must choose from 1-4 before you can proceed to quiz\n") ;
                        scanf("%d", &num1) ;
                        
                        i++ ;
                    }
                    
                    printf("You chose %d..Enter 2 to continue quiz", num1) ;
                    
                    break ;
                }
        
                case '2': 
                {    
                    if(option=='2' && option!='1'&&num1==0)
                    { 
                        printf("you must choose 1 before proceeding\n") ;
                        break ;
                    }
                    if(correct!=0||wrong!=0 &&option!='3')//user can't choose 2 until 3 has been chosen
                    {
                        printf("You must view answers, enter 4 to exit or enter1 to restart to quiz\n") ;
                        break ;
                    }
                    switch(num1)//start of option 2 using num1 as a variable of numbers of questions to be asked 
                    {//the index i will always be less than num1 the number of questions..the value of one and i will keep changing or be incremented to create a semblance of randomness
                        case 1:
                        {
                            printf("What is 1+1: *\n") ;
                            scanf("%d", &answer) ;//the answer is stored in a variable one, two, three, four, and matched with answer-5. if it matches..then result is display
                            if(answer==1+1) 
                            {
                                printf("%d is Correct!\n", answer) ;
                                correct++ ;
                            }
                            else
                            {
                                printf("%d is Incorrect. Answer is: %d!\n", answer, 1+1) ;
                                wrong++ ;
                            }
                            break;
                        }//end of case 1 of question 1 
                        
                        case 2:
                        {   
                            for(i=0; i<num1; one++, i++)
                            {
                                printf("what is %d * %d\n", one, i+3) ;
                                scanf("%d", &answer) ;
                                if(answer==one*(i+3))
                                {
                                    printf("%d is Correct!\n", answer) ;
                                    correct++ ;
                                    
                                }
                                else
                                {
                                    printf("%d is Incorrect. Answer is: %d!\n", answer, one*(i+3)) ;
                                    wrong++ ;
                                }
                            }
                           
                            break ;
                        }//end of question 2
                        //case 3
                        case 3:
                        { 
                            for(i=0; i<num1; three*=2, i++)
                            {
                                printf("what is %d - %d\n",three, i+2) ;
                                scanf("%d", &answer) ;
                                if(answer==(three)-(i+2))
                                {
                                    printf("%d is Correct!\n", answer) ;
                                    correct++ ;
                                }
                                else
                                {
                                    printf("%d is Incorrect. Answer is: %d\n", answer, (three)-(i+2)) ;
                                    wrong++ ;
                                }
                            }
                            break ;
                        }
                        case 4:
                        {
                            for(i=0; i<num1; i++,four)
                            {
                                printf("what is %d / %d\n", four,i+1) ;
                                scanf("%d", &answer) ;
                                if(answer==(four)/(i+1))
                                {
                                    printf("%d is Correct!\n", answer) ;
                                    correct++ ;
                                }
                                else
                                {
                                    printf("%d is Incorrect. Answer is: %d!\n", answer,(four)/(i+1))                                ;
                                    wrong++ ;
                                }
                            }
                            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(option == '3' && answer == 0)//if condition is true it is going to break..it can't show answers if question hasn't been answered
                    {   
                        printf("You haven't answered question\n") ;
    
                        break ;
                    }
                   
                    printf("You got %d Correct!\n", correct) ;
                    printf("You got %d Wrong!\n", wrong) ; 
                    correct = 0;
                    wrong = 0;//will clear the value of correct and wrong or it will be incrementing if program hasn't been closed by user
                    break ;
                }
                
                case '4':
                {               
                    printf("Are you sure you want to exit quiz?\n") ;
                    printf("Y/N") ;
                    printf("\n") ;
                    scanf("%1s", &yesno) ;
                    while(yesno!='Y' && yesno!='y'&&yesno!='N' && yesno!='n')
                    {
                        printf("Invalid choice\n") ;
                        break ;//invalid choice will return to main menu
                    }
                    if(yesno== 'Y'|| yesno == 'y')
                    {
                        exit(0) ;//quits program
                    }
                    if(yesno=='N'|| yesno == 'n')
                    {
                        clrscr() ;//return to main meni
                        break ;
                    }
                    
                    break ; /*user must input y to quit*/
                }
            
                default: 
                {
                    printf("Please input valid character\n") ;
                    break ; 
                }             
            }
        }while(option!='1' || option!='2' ||option!='3' || option != '4') ;
        
        getch() ;
    }

  3. #18
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This just stops the program until you hit enter. I use it to hold the console window open at the end of the program, especially.

    Code:
    i = getchar();
    At the lower levels, the computer doesn't know the alphabet - only raw numbers. What the computer does it take those raw numbers and pigeonhole them into what they should be:

    All digits have an ascii range of numbers, as do all char's, The computer just knows that a char will be a certain size (usually 8 bytes), and an integer will be larger (usually 16 or 32 bytes), and so on.

    so a char is just a "small" number, that (usually 1-256).

    Google man scanf(), and see what you can figure out about limiting the number of keystrokes it will accept, or use a loop with getchar(), and do your own testing for valid input, right inside the loop.

  4. #19
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by Adak View Post
    This just stops the program until you hit enter. I use it to hold the console window open at the end of the program, especially.

    Code:
    i = getchar();
    At the lower levels, the computer doesn't know the alphabet - only raw numbers. What the computer does it take those raw numbers and pigeonhole them into what they should be:

    All digits have an ascii range of numbers, as do all char's, The computer just knows that a char will be a certain size (usually 8 bytes), and an integer will be larger (usually 16 or 32 bytes), and so on.

    so a char is just a "small" number, that (usually 1-256).

    Google man scanf(), and see what you can figure out about limiting the number of keystrokes it will accept, or use a loop with getchar(), and do your own testing for valid input, right inside the loop.
    hi i'm trying to understand arrays..and its not going so good..
    i'm trying to find the smallest number in array size of 10..but im using 5 to make it simpler for me.it will replace the smallest number with 9999 , it should do this each time it goes through the loop
    so for example 1 2 3 4 5 then 9999 2 3 4 5 then 9999 9999 3 4 5
    it is only printing the first number but it doenst continue through the loop like it should..and this isnt assignment..its just an exercise i saw in the book..thanks
    [code]

    #include <stdio.h>
    #include <conio.h>
    #define ROW 5

    main()
    {
    Code:
    int unsorted[ROW] ;
        int i ;
        int j = 0 ;
        int var[5] = {0};
      
        printf("Enter %d numbers\n", ROW);
        for(i=0;i<ROW; i++) 
        {
            scanf("%d", &unsorted[i]) ;
        }
        for(i=0,j=0; i<5,j<5;i++, j++) 
        {
            var[j] = unsorted[i] ;
        }
      
    
        printf("\n") ;
        
        for(i=0,j=0; i<5,j<5;i++, j++) 
        {
            if(var[j] <= unsorted[i]) ;
            {
                var[j] = 9999 ;
            printf("%d\t", var[j]) ;
            }
            
            
        }
      
        
                
            
          
            
        
        
    
        
            
    
        getch() ;
    }
    Last edited by Eman; 11-20-2009 at 12:52 PM.

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

    Arrays

    Hi again,
    I have to create a program that should replace the smallest number in a 1-d array, on each turn
    so 5 elements
    1
    2
    3
    4
    5
    it should be 9999 2 3 4 5
    newline
    9999 9999 3 4 5
    Code:
    #include <stdio.h>
    #include <conio.h>
    #define ROW 5
    main()
    {
        int unsorted[ROW] ;
        int i ;
        int j  ;
        int smallest[ROW] ;
    
        int temp ;
        
        printf("Enter %d numbers\n", ROW);
        for(i=0;i<ROW; i++) 
        {
            scanf("%d", &unsorted[i]) ;
        }
          for(i=0;i<ROW; i++) 
            {
                smallest[i] = unsorted[i] ;
            }
      
        
      
        for(i=0;i<5;i++)
        {
           if(unsorted[i] < smallest[i+1]) 
           {
                
                for(i=0;i<ROW; i++) 
                {
                    temp = 9999 ;
                    unsorted[i] = temp ;
                
                   
                    for(i=0; i<ROW; i++)
                    {
                        printf("%d\t", unsorted[i]) ;
                    }
                   
                }
                
               
            }
        }
           
          
           
      
       
                
            
    
    
        getch() ;
    }
    Please help...it doesnt finish the loop!!

Page 2 of 2 FirstFirst 12
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