Programming Help

This is a discussion on Programming Help within the C Programming forums, part of the General Programming Boards category; I am having some problems with the do/while and continue statement for this program. Any help or suggestions would be ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    3

    Programming Help

    I am having some problems with the do/while and continue statement for this program. Any help or suggestions would be appreciated.
    Thank you
    int

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main(void)
    //char  n; 
    
    { //begin of function main
    
        int num; //number generated
        int guess = 0;
        int games = 0;
        int counter = 0;	
    		
        srand(time(NULL)); //generate the random seed generator
        num =  rand() % 100 +1 ;  //pick a number from 1 to 100
        
    	
    	
    printf("I will generate a random number between 1 and 100\n");
    printf("Can you guess the number?\n");
    printf("Please enter your first guess.\n");
    
       do
       {
    	  while (num != guess)
    	  {
    		scanf("%d", &guess);
    		++counter;
    
    		if (guess > 100 || guess <= 0)
    		{
    		printf("Number out of range\n");
    		printf("Try again\n");
    		}
    		else if (guess > num)
    		{
    		printf("Too High! try again!\n");
    		}
    		 else if (guess < num)
    		{
    		 printf("Too Low! try again!\n");
    		}
                   }  //end while
    			
    
    	
                   printf("You guessed the number in %d tries!\n",counter);
    		//i = counter/ guess;
    	
    		if(counter >= 16)
    			{
    			printf("You need more practice \n");
    			}
    		else if (counter >= 11 && counter <= 15)
    			{
    		 printf("Your getting better \n");
    			}
    		else if (counter >= 6 && counter <= 10)
    			{
    			printf("Practice Pays off\n");
    			}
    		else if (counter >= 2 && counter <= 5)
    			{
    			printf("Nice job \n");
    			} 
    		else if (counter == 1)
    			{
    			printf("You got lucky !!\n");
    			}
    
    
    		printf("Would you like to play again?\n");
    		printf("Enter  n or N to quit\n");
      
    
       } //end of do while
       while (getchar() != 'n'|| getchar() != 'N'); 
    		{	
    		continue;
    		}
    	   if(getchar() == 'n'|| getchar() == 'N'); 
    		{
    		 printf("Game over"\n);
    		}	
    
         return 0;
    
    }// end of main

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    1) do/while looks like this:
    Code:
    do {
      /* Body */
    } while ( condition );
    No loop controls come after the while like in a normal while loop.

    2) Each time you call getchar, you actually request input. Since you want a single character to test, only call getchar once.

    3) Scanf will cause you problems when you mix it with getchar.

    Here is something workable:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main(void)
    { //begin of function main
    
      int num; //number generated
      int guess = 0;
      int game = 0;
      int counter = 0;	
    
      srand(time(NULL)); //generate the random seed generator
      num =  rand() % 100 +1 ;  //pick a number from 1 to 100
    
      printf("I will generate a random number between 1 and 100\n");
      printf("Can you guess the number?\n");
      printf("Please enter your first guess.\n");
    
      do
      {
        while (num != guess)
        {
          scanf("%d", &guess);
          ++counter;
    
          if (guess > 100 || guess <= 0)
          {
            printf("Number out of range\n");
            printf("Try again\n");
          }
          else if (guess > num)
          {
            printf("Too High! try again!\n");
          }
          else if (guess < num)
          {
            printf("Too Low! try again!\n");
          }
        }  //end while
    
        printf("You guessed the number in %d tries!\n",counter);
        //i = counter/ guess;
    
        if(counter >= 16)
        {
          printf("You need more practice \n");
        }
        else if (counter >= 11 && counter <= 15)
        {
          printf("Your getting better \n");
        }
        else if (counter >= 6 && counter <= 10)
        {
          printf("Practice Pays off\n");
        }
        else if (counter >= 2 && counter <= 5)
        {
          printf("Nice job \n");
        } 
        else if (counter == 1)
        {
          printf("You got lucky !!\n");
        }
    
        printf("Would you like to play again?\n");
        printf("Enter  n or N to quit\n");
        while ( getchar() != '\n' )
          ;
        game = getchar();
        getchar();
      } //end of do while
      while (game != 'n' && game != 'N'); 
    
      return 0;
    
    }// end of main
    My best code is written with the delete key.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    The syntax for a do/while is just:

    Code:
    do {
    
    }while(/*condition*/);
    >> while (getchar() != 'n'|| getchar() != 'N');

    getchar() takes input from the user *each and every* time it is called. Since you really only want it called once per go round, you'll need to store it's result in a variable and use that for comparison, ie:

    Code:
    char input = 0;
    do{
     input = getchar();
    }while(input != 'n' && input != 'N');
    By the time the user breaks out of the loop, there's no need to check it again - you know the input was either 'n' or 'N'.

    >> if(getchar() == 'n'|| getchar() == 'N');

    The semicolen at the end of that if statement of course expands out to:

    if(getchar() == 'n'|| getchar() == 'N'){ /* do nothing */ }

    You probably didn't mean to type that, just be mindful of your semicolens.



    [edit]
    I'm too slow.
    [/edit]
    Last edited by Sebastiani; 03-08-2004 at 07:47 PM.
    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;
    }

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    3
    Thanks for all your help, I am still confused about how to continue the session until the key word is entered 'n' or 'N' . Would a continue; following the first instance of getchar() allow the loop to go back and restart the game ?


    int

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I am still confused about how to continue the session until the key word is entered 'n' or 'N' .
    A good idea would be to wrap the game itself in a function, then use a controlling loop in main to call that function as long as the user keeps entering something other than n or N.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

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