# Guess the number program help needed

• 05-06-2009
msbrownsugah
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; }```
• 05-06-2009
void_t
The tries part goes inside the nested do-while loop (the one where you keep guessing).
• 05-06-2009
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.
• 05-06-2009
laserlight
Quote:

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.
• 05-06-2009
BEN10
Quote:

Originally Posted by msbrownsugah
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; }```
• 05-06-2009
Quote:

Originally Posted by laserlight
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. :D

• 05-06-2009
matsp
Quote:

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. :D

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
• 05-06-2009
msbrownsugah
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???!