What am I doing wrong ?

This is a discussion on What am I doing wrong ? within the C Programming forums, part of the General Programming Boards category; Hi all, I am a real newbie to the forum and to C programming. I just started learning a week ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Location
    New York
    Posts
    4

    What am I doing wrong ?

    Hi all,

    I am a real newbie to the forum and to C programming. I just started learning a week ago. This is my first try at a program and I have something wrong and I can not figure it out. I know it will be something simple but I can not find it.

    would you please take a look at it and give some pointers on what I am doing wrong. Beside it being slopy code, and probably not to efficient. Why will it not loop ?

    Here is my code:

    Code:
    /* This is a guessing game. You try to guess what the magic number is.
          You get 10 tries or guess the right number before the game terminates.
    	  Then the game terminates and tell you how may right and wrong answers
    	  that you had. */
    	  
    #include<stdio.h>
    
    int main(void)
    {
    		int right ;         // Declare the variables
    		int wrong ;
    		int magicnum =123;
    		int guess ;
    		int tries ;
    		int i = 1;
    		
    		printf(" Try to guess the magic number\n");
    		
    		
    		for(i=0 ; i<11 && guess!=magicnum ; i++);        
    		{
    		        printf(" Enter your guess.\n");
    		        scanf("%d.\n", &guess);
    	           
    				 
    		      if(guess==magicnum)
    				{
    					tries=1;
    					printf("Right !\n");
    					printf(" It took %d try.\n", tries  );
    					right++;
    					
    				 } 
    		             if(guess > magicnum)
    					 {
    					    printf("Try Again.\n");
    						printf(" Your number was too high.");
    						wrong++;
    						tries++;
    					 }
    						  else 
    						  {	
    							printf("Wrong !\n Your number is too low !\n");
    							wrong++;
    							tries++;
    						   } 
    				
    		     }	
    			    
    			 printf(" It took %d tries.\n", tries);
    			 printf(" You had %d right and %d wrong !\n", right, wrong);
    			 printf("The program will now terminate !");
    		
    		return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    It does execute every statement in your loop 10 times. Unfortunately the only statement in your for loop is
    Code:
    ;
    so that doesn't really help.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Location
    New York
    Posts
    4
    Really ? I can not get it too loop at all. I tried the game and no matter what you put in it for a number it will terminate . Have you tried running it and it worked . I wonder if there is something wrong with my complier? I am using Geany.

    Thanks for the input

    Glenn

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The only statement in your for loop is ;. All that stuff starting with the printf is not physically in your for loop.

  5. #5
    Registered User
    Join Date
    Oct 2009
    Location
    New York
    Posts
    4
    Ok, thanks for the info. I will see if I can figure out how to put everything in the loop.

    Many thanks,
    Glenn

  6. #6
    Registered User
    Join Date
    Oct 2009
    Location
    New York
    Posts
    4
    Well, I finally got it ! Thank you so much for the hint. I would have never found it ! I must be a little more careful from now on.
    Thanks again,
    Glenn


    Here is what I ended up with;

    Code:
    #include<stdio.h>
    
    int main(void)
    {
    		int right ;         // Declare the variables
    		int wrong ;
    		int magicnum =123;
    		int guess ;
    		int tries ;
    		int i ;
    		
    		printf(" Try to guess the magic number\n");
    		
    		
    		for(i=0;i<11 && guess!=magicnum;i++){
    		        printf(" Enter your guess.\n");
    		        scanf("%d.\n", &guess);
    				
    				  if(guess==magicnum)
    				    {
    					tries=i;
    					printf("Right !\n");
    					printf(" It took %d try.\n", tries + 1 );
    					right++;
    					break;
    					}
    				 if(guess > magicnum)
    					 {
    					    
    						printf(" Wrong !\n Your number was too high.");
    						wrong++;
    						tries++;
    					   }
    						 else
    						  {
    						   printf("Wrong !\n Your number is too low !\n");
    							wrong++;
    							tries++;}
    						 
    		}	
    		printf(" DONE!");
    
    		              return 0;
    }

  7. #7
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Quote Originally Posted by tabstop View Post
    It does execute every statement in your loop 10 times. Unfortunately the only statement in your for loop is
    Code:
    ;
    so that doesn't really help.
    oh man sweet catch tab, i totally overlooked that one
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 09:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM

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