Thread: Help with Program

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    46

    Question Help with Program

    my math quiz program compiles and runs but when I try to type in an answer it freezes...


    Can anyone spot an error i'm not seeing?

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    #include<string.h>
    
    int randomNumbers(){
      return rand()%100;
    } 
    
    int question(){
         int snum = 1 + randomNumbers()%4;  
         char sign; 
         switch(snum){
         case 1 :          
            sign = '+';
         break;
         case 2 :            
            sign = '-';
         break;
         case 3:             
            sign =  '*';
         break;
         case 4 :             
            sign = '/';
         default:
         break;
    }
    
        int num1 = randomNumbers();
        int num2 = randomNumbers();
        int answer = 0;
        int guess = 0;
        int point = 0;
        
        switch (sign){
               case '+' :
                   answer = num1 + num2;
                   printf ("%d %c %d\n",num1,sign,num2);
                   printf("%d",answer);
               break;
               case '-' :
                    answer = num1 - num2;
                    printf ("%d %c %d\n", num1,sign,num2);
               break;
               case '*' :
                    answer = num1 * num2;
                    printf ("%d %c %d\n", num1,sign,num2);
               break;
               case '/' :
                    answer = num1 / num2;
                    printf ("%d %c %d\n", num1,sign,num2);
               default: 
               break;
        }
        
        printf ("Answer :");
        scanf ("%d",guess);
            
        if (guess == answer){
           point = 1;
           printf("Correct");
           }
        return point;
    }
    
    int main(){
    
      int NumOfQue = 0;
      int choice = 0;
      char name[20];
      int score = 0;
      int i;  
      
      while(choice != 4){   
      printf("\nMenu");
      printf("\n1.Please Enter Your Name");
      printf("\n2.Enter the Number of Questions");
      printf("\n3.Take the Math Quiz");
      printf("\n4.Quit\n");
      scanf("%d",&choice);
      switch(choice){
      case 1 :
        printf("Please Enter Your Name: " );
        scanf("%s",name);               
        break;
      case 2 :
        printf("Please Enter the Number of Questions you want to take: ");
        scanf("%d",&NumOfQue);
        break;
      case 3 :
      for(i=0;i<NumOfQue;i++){
      printf("%d",question());  
      }                        
      printf ("%d",score);        
      default: break;
      }
     
    };
    
        return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    scanf requires an address-of -- use &guess.

  3. #3
    Why am I a programmer? shoutatchickens's Avatar
    Join Date
    Mar 2008
    Posts
    45
    When you get some extra time you may want to look into the problems of scanf. It may be better to get a character string from the user and convert it to an integer afterwards.

    Here's a link to read:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385

    Also, maybe search around on this forum. There are plenty of threads that discuss alternatives to scanf.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    http://cpwiki.sf.net/Buffer_overrun
    You certainly need to read it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM