Thread: need help with my programm that uses functions

  1. #1
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356

    Unhappy need help with my programm that uses functions

    I want to modify my program so that it can coun the number of correct and incorrect responses typed by the student.After the student types 10 awnsers, my program should calculate the percentage of the correct responses.If the percentage is lower than 75 percent, your program should print "Please ask you instructor for extra help" and then terminate..

    My code:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <time.h>
    
    
    int riwo ( int ,int, int ); //function prototype .
    void rightwords ( void ); //function for the right words.
    void wrongwords (void ); //function for the wrong words.
    void tutorhelp ( int, int, int ); //function for calculating the percentage.
    
    int main()
    {
     int num1, num2, awn;
         srand(time(NULL));
         num1 = 1+ rand () % 9;
         num2 = 1+ rand() % 9;
    
    
             while (riwo(num1, num2,awn) == 0) {
    
          printf ("How much is %d times %d: ", num1 , num2);
       scanf ("%d", &awn );
    
        if ( riwo(num1, num2, awn) == 1) {
           rightwords();
    while ( riwo( num1, num2, awn) == 1){
          num1 = 1+ rand () % 9;
          num2 = 1+ rand() % 9;
    
         printf ("How much is %d times %d: ", num1 , num2);
                scanf ("%d", &awn );
        if ( riwo ( num1, num2, awn) == 1 ){
         rightwords();}
                } }
            wrongwords();
            tutorhelp(num1, num2, awn);
                                   }
    
    
          system("PAUSE");
          return 0;
    }
    
    int riwo (int a, int b, int tot )
    {
    
        if ( tot == a*b )
        return (1);
        else
        return (0);
    }
    
    void rightwords( void )
    
    {
         int right;
    
         right = 1+rand () % 4;
    
         switch ( right ){
    
                case 1:
                printf ("Very good!\n");
                break;
    
                case 2:
                printf ("Excellent!\n");
                break;
    
                case 3:
                printf ("Nice work!\n");
                break;
    
                case 4:
                printf ("Keep up the good work!\n");
                break;
                }
    
      }
    
    void wrongwords ( void )
    {
     int wrong;
         wrong = 1+rand() % 4;
    
         switch ( wrong ){
    
                case 1:
                     printf ("No. Please try again.\n");
                     break;
    
                case 2:
                     printf ("Wrong. Try once more.\n");
                     break;
    
                case 3:
                     printf ("Don't give up!\n");
                     break;
    
                case 4:
                     printf ("No.keep trying\n");
                     break;
                     }
    
    }
    
    void tutorhelp ( int x, int y, int z )
    {
     int cnt, addwrong, addright, percentage, totright, totwrong;
     percentage = ((addright- addwrong) *100) /10;
    
    
    
     if ( riwo (x, y, z) ==1 )
       totright += addright;
     else
       totwrong +=  addwrong;
    
     if ( totright + totwrong == 10 ){
     percentage = ((totright- totwrong) *100) /10;
     if ( percentage <= 75 ){
     printf ("Please ask your instructor for extra help");
      }}}
    Thanks alot

  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
    1. You seem to be calling riwo far too many times - in particular, the first call in the first while loop passes an uninitialised value for awn.

    2. Fix the indentation of your code, it's all over the place. Modifying it will be more difficult unless you can see the flow of the code from the indentation.

  3. #3
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356

    my code again

    Code:
     
     #include <stdlib.h>
                                  #include <stdio.h>
                                  #include <time.h>
    
    
                                  int riwo ( int ,int, int ); //function prototype .
                                  void rightwords ( void ); //function for the right words.
                                  void wrongwords (void ); //function for the wrong words.
                                  void tutorhelp ( int, int, int ); //function for calculating the percentage.
    
                                  int main()
                                  {
                                   int num1, num2, awn;
                                       srand(time(NULL));
                                       num1 = 1+ rand () % 9;
                                       num2 = 1+ rand() % 9;
    
    
                                     while (riwo(num1, num2,awn) == 0) {
    
                                        printf ("How much is %d times %d: ", num1 , num2);
                                        scanf ("%d", &awn );
    
                                 	if ( riwo(num1, num2, awn) == 1) {
                                    rightwords();
    
                                     while ( riwo( num1, num2, awn) == 1){
                                        num1 = 1+ rand () % 9;
                                        num2 = 1+ rand() % 9;
    
                                       printf ("How much is %d times %d: ", num1 , num2);
                                       scanf ("%d", &awn );
    
                                    if ( riwo ( num1, num2, awn) == 1 ){
                           		rightwords();}
                                              } 
    						}
                                   	wrongwords();
                                    tutorhelp(num1, num2, awn);
                                                                 }
    
    
                                        system("PAUSE");
                                        return 0;
                                  }
    
                                  int riwo (int a, int b, int tot )
                                  {
    
                                      if ( tot == a*b )
                                      return (1);
                                      else
                                      return (0);
                                  }
    
                                  void rightwords( void )
    
                                  {
                                       int right;
    
                                       right = 1+rand () % 4;
    
                                       switch ( right ){
    
                                              case 1:
                                              printf ("Very good!\n");
                                              break;
    
                                              case 2:
                                              printf ("Excellent!\n");
                                              break;
    
                                              case 3:
                                              printf ("Nice work!\n");
                                              break;
    
                                              case 4:
                                              printf ("Keep up the good work!\n");
                                              break;
                                              }
    
                                    }
    
                                  void wrongwords ( void )
                                  {
                                   int wrong;
                                       wrong = 1+rand() % 4;
    
                                       switch ( wrong ){
    
                                              case 1:
                                                   printf ("No. Please try again.\n");
                                                   break;
    
                                              case 2:
                                                   printf ("Wrong. Try once more.\n");
                                                   break;
    
                                              case 3:
                                                   printf ("Don't give up!\n");
                                                   break;
    
                                              case 4:
                                                   printf ("No.keep trying\n");
                                                   break;
                                                   }
    
                                  }
    
                                  void tutorhelp ( int x, int y, int z )
                                  {
                                   int cnt, addwrong, addright, percentage, totright, totwrong;
                                   		percentage = ((addright- addwrong) *100) /10;
    
    
    
                                   if ( riwo (x, y, z) ==1 )
                                     totright += addright;
                                   else
                                     totwrong +=  addwrong;
    
                                   if ( totright + totwrong == 10 ){
                                   percentage = ((totright- totwrong) *100) /10;
                                   if ( percentage <= 75 ){
                    	       printf ("Please ask your instructor for extra help");
                                    }
    					}
    						}
    Yeah salem u said i have called riwo to many times
    well i dont c why i shouldnt.Help me out man

  4. #4
    Unregistered
    Guest
    Not sure if I've understood as there seem to be a few inconsistencies.
    Your code seems to keep asking the question until it is answered correctly, therefore the user will always get 100% if they can answer correctly.
    Wish I got exams like that, mind you I'd probably never leave the exam room.
    If changed to ask 10 times then you really want to ask the question once or a set number of times in order to test the user properly.

    The code below asks the user up to three times if wrong.

    Hope this is of some use.


    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <time.h>
    #define NO_OF_QUESTIONS 10
    
    int riwo (int ,int, int); //function prototype .
    void rightwords (void); //function for the right words.
    void wrongwords (int wrong); //function for the wrong words.
    void tutorhelp (int right); //function for calculating the percentage.
    
    int main()
    {
      int num1, num2, awn, count, right, wrong;
    
      srand(time(NULL));
    
      right = 0;
    
      for(count = 0; count < NO_OF_QUESTIONS; count++)
      {
        num1 = 1+ rand () % 9;
        num2 = 1+ rand() % 9;
        wrong = 0;
    
        while(wrong < 3)
        {
          printf ("How much is %d times %d: ", num1 , num2);
          scanf ("%d", &awn );
    
          if(riwo(num1, num2, awn) == 1)
          {
            rightwords();
            right++;
            wrong = 3;
          }
          else
          {
            wrongwords(wrong);
            wrong++;
          }
        }
      }
      tutorhelp(right);
    
      system("PAUSE");
      return 0;
    }
    
    int riwo (int a, int b, int tot)
    {
      if ( tot == a*b )
      return (1);
      else
      return (0);
    }
    
    void rightwords(void)
    
    {
      int right;
    
      right = 1+rand () % 4;
    
      switch ( right )
      {
        case 1  :  printf ("Very good!\n");
                        break;
    
        case 2  :  printf ("Excellent!\n");
                        break;
    
        case 3  :  printf ("Nice work!\n");
                   break;
    
        case 4  :  printf ("Keep up the good work!\n");
                   break;
    
        default :  ;
      }
    
    }
    
    void wrongwords (int wrong)
    {
    
      switch (wrong)
      {
        case 0  :  printf ("No. Please try again.\n");
                   break;
    
        case 1  :  printf ("Last Chance.\n");
                   break;
    
        case 2  :  printf ("Sorree, you're wrong!\n");
                   break;
    
        default :  ;
      }
    
    }
    
    void tutorhelp (int right)
    {
      float perc_right = 0;
    
      perc_right = ((float)right/(float)NO_OF_QUESTIONS) * 100;
      if((int)perc_right < 75)
        printf ("Please ask your instructor for extra help");
    }

  5. #5
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    What Salem is saying is that when you run your program,
    you are calling riwo(num1,num2,awn) yet there's nothing
    useful in awn yet.

    What you need to do is rather is a do...while() loop so that
    the loop runs succesfully at least once.

    AND

    Salem is right, you've nested a while loop within another
    that does the same thing!
    This is not necessary.

    Rather do this:
    Code:
    if(riwo(num1,num2,awn))
         continue;
    Then you won't need another while loop.

  6. #6
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Your code seems to keep asking the question until it is answered correctly, therefore the user will always get 100% if they can answer correctly.
    I think you're misunderstanding what datainjector wants to do.
    It's not about getting 100% or anything like that.

    It's meant as an encouragement to the user to keep trying if he
    got the answer wrong OR as a compliment if he got it right!

  7. #7
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356

    help me out okay hear is the modifyed code

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <time.h>
    
    
                                  int riwo ( int ,int, int ); //function prototype .
                                  void rightwords ( void ); //function for the right words.
                                  void wrongwords (void ); //function for the wrong words.
                                 
                                  int main()
                                  {
                                   int num1, num2, awn;
                                       srand(time(NULL));
                                       num1 = 1+ rand () % 9;
                                       num2 = 1+ rand() % 9;
    
    
                                          do {
    						printf ("How much is %d times %d?",num1, num2);
    						scanf ("%d", &awn );
    						if ( riwo( num1, num2, awn) == 1)
    						rightwords();
    						else
    						wrongwords();
    					  }while ( riwo ( num1, num2, awn) == 0 );
    
                                        system("PAUSE");
                                        return 0;
                                  }
    
                                  int riwo (int a, int b, int tot )
                                  {
    
                                      if ( tot == a*b )
                                      return (1);
                                      else
                                      return (0);
                                  }
    
                                  void rightwords( void )
    
                                  {
                                       int right;
    
                                       right = 1+rand () % 4;
    
                                       switch ( right ){
    
                                              case 1:
                                              printf ("Very good!\n");
                                              break;
    
                                              case 2:
                                              printf ("Excellent!\n");
                                              break;
    
                                              case 3:
                                              printf ("Nice work!\n");
                                              break;
    
                                              case 4:
                                              printf ("Keep up the good work!\n");
                                              break;
                                              }
    
                                    }
    
                                  void wrongwords ( void )
                                  {
                                   int wrong;
                                       wrong = 1+rand() % 4;
    
                                       switch ( wrong ){
    
                                              case 1:
                                                   printf ("No. Please try again.\n");
                                                   break;
    
                                              case 2:
                                                   printf ("Wrong. Try once more.\n");
                                                   break;
    
                                              case 3:
                                                   printf ("Don't give up!\n");
                                                   break;
    
                                              case 4:
                                                   printf ("No.keep trying\n");
                                                   break;
                                                   }
    
                                  }
    Well help me out hear.. i used the do-while as u said and u said i should use the continue stament..well i am, a bit confused..where should i use it...

  8. #8
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Here: You didn't need a continue statement i just misunderstood
    what it was that you wanted.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <time.h>
    
    int riwo ( int ,int, int ); //function prototype .
    void rightwords ( void ); //function for the right words.
    void wrongwords (void ); //function for the wrong words.
                                 
    int main()
    {
    	int num1, num2, awn;
    	int right=0, wrong=0, i=0;
        srand(time(NULL));
    	num1 = 1+ rand () % 9;
    	num2 = 1+ rand() % 9;
    		
        do
    	{
    		printf ("How much is %d times %d?",num1, num2);
    		scanf ("%d", &awn );
    		if ( riwo( num1, num2, awn) == 1)
    		{
    			rightwords();
    			num1 = 1+ rand () % 9;
    			num2 = 1+ rand() % 9;
    			right++;
    		}
    		else
    		{
    			wrongwords();
    			wrong++;
    		}
    		i++;
    	}while(i < 10);
    
        printf("You got %d%% right", right*10);
    	
    	system("PAUSE");
        return 0;
    }
    
    int riwo (int a, int b, int tot )
    {
    	if ( tot == a*b )
    		return (1);
        else
            return (0);
    }
    
    void rightwords( void )
    {
    	int right;
        right = 1+rand () % 4;
    	switch ( right )
    	{
    		case 1:	printf ("Very good!\n");break;
            case 2: printf ("Excellent!\n");break;
            case 3: printf ("Nice work!\n");break;
            case 4: printf ("Keep up the good work!\n");break;
        }
    }
    
    void wrongwords ( void )
    {
    	int wrong;
        wrong = 1+rand() % 4;
    
        switch ( wrong )
    	{
    	    case 1:	printf ("No. Please try again.\n");break;
            case 2: printf ("Wrong. Try once more.\n");break;
            case 3: printf ("Don't give up!\n");break;
            case 4: printf ("No.keep trying\n");break;
        }
    }

  9. #9
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356

    thanks alort

    Thanks alot man i was stuck on this question for about 2 weeks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  2. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  3. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM
  4. Inline functions and inheritance
    By hpy_gilmore8 in forum C++ Programming
    Replies: 3
    Last Post: 01-14-2004, 06:46 PM
  5. functions - please help!!!!
    By linkies in forum C Programming
    Replies: 1
    Last Post: 08-21-2002, 07:53 AM