# Thread: Guess the number program help needed

1. ## 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
{
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. The tries part goes inside the nested do-while loop (the one where you keep guessing).

3. 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. 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.

5. 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
{
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;
}```

6. 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.

7. Originally Posted by DeadPlanet
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 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

8. 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