Guess the number program help needed

This is a discussion on Guess the number program help needed within the C Programming forums, part of the General Programming Boards category; Hello first part of assignment I completed which was to create a guess the number prog. 2nd part I have ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    5

    Guess the number program help needed

    Hello

    first part of assignment I completed which was to create a guess the number prog. 2nd part I have to limit # of tries.. less than 10 print either u know r u got lucky , =10 print ahah u know the secret, or lmore than 10 u should be able to do better.. i been trying to plug in if tries=10, <10 , >10 etc but that doesn't work..not sure where i would place it. can someone assist.

    thanks!

    Code:
     #include<stdio.h>
    #include<time.h>
    #include<stdlib.h>
    
    int main(void)
    {
    	int theNumber;
    	int tries,guess,yesno=1;
    	srand (time(0));
    	do 
    	{
    		theNumber=rand()% 1000+1;
    		tries=0;
    		printf("I have a number between 1 and 1000.""Can you guess my number? \n\n");
    		do
    		{
    			printf("Please type your first guess:");
    			scanf("%d",&guess);
    			++tries;
    			if (guess > theNumber)
    				printf("To high!! Try again.\n\n");
    			if (guess < theNumber)
    				printf("To low!! Try again. \n\n");			
    		}
    		while (guess != theNumber);
    		printf("Excellent! You guessed the number!\n");
    		printf("Would you like to play again?\n");
    		printf("Please type (1=yes, 2=no)?\n");
    		scanf("%d",&yesno);
    	}
    	while(yesno==1);
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4
    The tries part goes inside the nested do-while loop (the one where you keep guessing).

  3. #3
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    printf("I have a number between 1 and 1000.""Can you guess my number? \n\n");

    should be

    printf("I have a number between 1 and 1000. Can you guess my number? \n\n");

    Only error I can see.

    To test the guesses against the number, your best bet is just to use if, else if and else statements.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,733
    Quote Originally Posted by DeadPlanet
    printf("I have a number between 1 and 1000.""Can you guess my number? \n\n");

    should be

    printf("I have a number between 1 and 1000. Can you guess my number? \n\n");

    Only error I can see.
    That is not an error since adjacent string literals are automatically concatenated.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by msbrownsugah View Post
    Hello

    first part of assignment I completed which was to create a guess the number prog. 2nd part I have to limit # of tries.. less than 10 print either u know r u got lucky , =10 print ahah u know the secret, or lmore than 10 u should be able to do better.. i been trying to plug in if tries=10, <10 , >10 etc but that doesn't work..not sure where i would place it. can someone assist.

    thanks!
    I've modified the code. It should work.(sorry for the indentation part)
    Code:
     #include<stdio.h>
    #include<time.h>
    #include<stdlib.h>
    
    int main(void)
    {
    	int theNumber;
    	int tries,guess,yesno=1;
    	srand (time(0));
    	do 
    	{
    		theNumber=rand()% 1000+1;
    		tries=0;
    		printf("I have a number between 1 and 1000.""Can you guess my number? \n\n");
    		do
    		{
    			printf("Please type your first guess:");
    			scanf("%d",&guess);
    			++tries;
    			if (guess > theNumber)
    				printf("To high!! Try again.\n\n");
    			if (guess < theNumber)
    				printf("To low!! Try again. \n\n");			
    		}
    		while (guess != theNumber && tries<=10);
                                    if(tries<10)
    		printf("either u know or u got lucky ");
                                    else if(tries==10)
                                    printf("ahah u know the secret");
                                   else
                                    printf(" u should be able to do better");
    		printf("Would you like to play again?\n");
    		printf("Please type (1=yes, 2=no)?\n");
    		scanf("%d",&yesno);
    	}
    	while(yesno==1);
    	return 0;
    }
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  6. #6
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    Quote Originally Posted by laserlight View Post
    That is not an error since adjacent string literals are automatically concatenated.
    Wow, it's funny that on the only day I've even seen something like that, is the day that I read about it 3 hours after I've just told someone it was wrong.

    Sorry about that

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by DeadPlanet View Post
    Wow, it's funny that on the only day I've even seen something like that, is the day that I read about it 3 hours after I've just told someone it was wrong.

    Sorry about that
    Sorry to side-track, but it comes in REAL handy when you have things like menus and such to print:
    Code:
    printf("1. Add...\n"
          "2. Delete...\n"
          "3. List...\n"
          "0. Exit...\n");
    is much easier to read/follow than a single line with embedded newlines, without adding extra calls to printf.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    5
    ok thanks for that help i got some errors but i was able to figure it out.

    lastly i am so confuse on this part and it may be so simple... i have to include in the program a function which displays a header with my name and brief description of the program. than Write the portion of the program that gathers new guesses as a separate function and Write the portion of the program that checks the new guesses as a separate function also Define all the function prototypes in a header file and include the function definitions as a
    separate C file.

    im like huh???!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program to calculate the square root
    By saszew in forum C Programming
    Replies: 7
    Last Post: 10-28-2008, 12:53 PM
  2. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  3. Mutiliplcation Program that uses a (do, while, and for loop)?
    By Debbie Bremer in forum C++ Programming
    Replies: 4
    Last Post: 10-11-2008, 06:04 PM
  4. problem with my prime number program
    By datainjector in forum C Programming
    Replies: 4
    Last Post: 07-12-2002, 12:30 PM

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