Thread: help why does cnt not reset to 1??

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

    help why does cnt not reset to 1??

    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 hte wrong words.
    
    int main()
    {
     int num1, num2, awn;
     int cnt=1, right=0, wrong = 0, percentage=0;
    
    
         srand(time(NULL));
         num1 = 1+ rand () % 9;
         num2 = 1+ rand() % 9;
    
         while ( cnt != -1 ){
    
    
         printf ("How much is %d times %d?", num1, num2 );
         scanf ("%d", &awn );
         if ( riwo (num1, num2, awn ) == 1) {
         num1 = 1+ rand()  %9;
         num2 = 1+ rand()  %9;
         rightwords();
         right++;               }
    
         else        {
         wrongwords();
         wrong++;     }
    
    
          { percentage = ((10 - wrong)*100)/10;
          if((cnt == 10 ) && ( percentage <= 50 )){
    
         printf ("Ask you instructor for extra help\n");
        
         break;                                  }
    
         if ( cnt== 10 ){
         cnt =1;  }
         }
          ++cnt; }
    
    
    
    
           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 the problem here is when cnt == 10 it checks if the percentage is less that 50 ..So i wanted to reset cnt back to 1 so i used if ( cnt == 10 ) then cnt = 1;
    am i wrong where am i wrong??? help me out

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Depending on what you're doing, I think you need to reset cnt when the user enters the correct answer for a question. In your current code, cnt is only reset when it reaches 10, which is not the same thing.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

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

    i want to reset it to 1

    after every 10 counts if the percentage is <= 50 it should print the stament and break; else it should reset the cnt to 1,, is my code okay

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: i want to reset it to 1

    Originally posted by datainjector
    ... is my code okay
    Why not run it and see.... testing a program is just as much a part of development as writing the actual code
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Perhaps you should make the loops more readable, perhaps:


    Code:
    ...
    
    if( cnt > 10) 
     {
       cnt = 1;
    
        if( percentage <= 50 )
         {
           printf ("Ask you instructor for extra help\n");
           break;                              
         }
      }
    
    ++cnt; 
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is there a way to reset the bash or zsh idle time?
    By Overworked_PhD in forum Linux Programming
    Replies: 3
    Last Post: 04-06-2009, 09:02 AM
  2. do I really have to do pthread_mutex_lock?
    By cph in forum C Programming
    Replies: 3
    Last Post: 12-18-2008, 07:25 PM
  3. strtok reset
    By lavinpj1 in forum C Programming
    Replies: 3
    Last Post: 04-25-2006, 06:00 PM
  4. strcmp
    By kryonik in forum C Programming
    Replies: 9
    Last Post: 10-11-2005, 11:04 AM
  5. Reloading Meshes on Device Reset
    By Epo in forum Game Programming
    Replies: 6
    Last Post: 05-30-2005, 04:21 PM