General Discussion Undecided where to post - do it here. |
Reply to Thread New Thread |
![]() |
#1 |
|
I have to write a program that:
ask the user to guess a number between 1 and 20. The computer has picked a number between 1 and 20. If the user picks the right number, then the computer prints out the ‘Congratulations’ and game ends, else the user has five chances, if the user can not find the right answer in five guesses, the computer prints the message, ‘You Loose’ and the program stops. and so far i have: { int y,z; y=1+rand()%20; printf("Please Guess the Number between 1 and 20, You have five tries.\n"); while(z!=y){ scanf("%d", &z); if (z==y){printf("Congratulations\n");} } } which basically will ask them the number and if they get it right ends the program but how would i limit this to 5 gusses, im guessing to use a for loop like for(int x=1;x |
![]() |
![]() |
#2 |
|
pseudo code that may work...
Code: Code
int number = 1+rand()%20; boolean done = false; int tries = 5; int guess; while(!done && tries >0){ printf("Please Guess the Number between 1 and 20, You have %d tries remaining.\n", tries); scanf("%d", &guess); tries--; if(guess==number){ printf(blah); done = true; } } You may want to put something in so that if a user enters something outside the range it doesn't count as one of their tries. I feel like I've given too much away possibly! ![]() |
![]() |
![]() |
#3 |
|
Can you have two conditions in a while loop? If you can then you could have the loop add 1 to another variable for an incorrect guess then have the while loop stop when that integer equals 5 (ie go while its not equal to 5). You might be able to use something like:
while((z!=y)&&(w!=5)) Although I'm not sure if it'll work...worth a try anyway! Edit: Meh, TomG's better... |
![]() |
![]() |
#4 |
|
pseudo code that may work... weve basically learned functions, random numbers, for and while loops and other random crap BUT! some of the code helped me get it right! thanks alot man! |
![]() |
![]() |
#6 |
|
eh ive never learned any boolean commands yet so i think he would think i was cheating You could replace boolean with int, true with 1 and false with 0 to get the same effect, though it seems silly if you have not been taught about all the basic types. It's very easy anyway. Just wait till you get to start doing pointers and stuff with memory, you will probably see "Segmentation Fault" a lot! Edit: could you also tell me how i could possibly generate 100 random numbers without having to type it all out :O |
![]() |
![]() |
#8 |
|
|
![]() |
![]() |
#9 |
|
that gets me the hundred random numbers, what i now need to do is have it only display the ones that are divisible by 2 and 3.....(into a whole number i believe 1. Make a function that takes a number and returns a boolean - true if it's divisible by 2 or 3 and false otherwise. Then have an if statement that only printf's it if that function returns true on it. 2. Make the same function as before then generate the random numbers and only store the divisible ones in the array. Then iterate over the array printf-ing them all. |
![]() |
![]() |
#10 |
|
ahh thanks to you both! got it working
i dont know why he is testing us with things we havent even learned yet? edit: ahh getting these programs betetr now, my teacher barely speaks english as well so yeah this helps alot what does the %d mean? i always put it in the scanf functions but thats just because he told me to and it works, he never explains why |
![]() |
![]() |
#11 |
|
ah **** guys one more questions
i have to do this: 4- Write a program in C that has the following output: Please make a choice: 1-grade calculation 2-end Sample Input 1 Please input your first grade: 76 Please input your second grade: 84 Please input your third grade: 92 The average of your grades is: 84.0 This corresponds to a grade of: B Do more calculations?(Y or N) Sample Input: N Good By so far i have void main() { int a,b,c,d,h,z,y; while(a!=2){ printf("Please make a choice\n"); printf("1-Grade Calculation\n"); printf("2-END\n"); scanf("%d", &a); if (a==1){ printf("Please enter your first grade:"); scanf("%d", &b); printf("\n"); printf("Please enter your second grade:"); scanf("%d", &c); printf("\n"); printf("Please enter your third grade:"); scanf("%d", &d); printf("The average is:"); h=(b+c+d)/3; printf("%d", h); printf("\n"); if(h>=90 && h=80 && h=70 && h=60 && h |
![]() |
![]() |
#12 |
|
Do you not have a textbook?
If not this is an excellent C tutorial: http://www.crasseux.com/books/ctutorial/ |
![]() |
![]() |
#13 |
|
|
![]() |
![]() |
#19 |
|
void main()
{ int a,b,c,d,h,z; int y; while(a!=2){ printf("Please make a choice\n"); printf("1-Grade Calculation\n"); printf("2-END\n"); scanf("%d", &a); if (a==1){ printf("Please enter your first grade:"); scanf("%d", &b); printf("\n"); printf("Please enter your second grade:"); scanf("%d", &c); printf("\n"); printf("Please enter your third grade:"); scanf("%d", &d); printf("The average is:"); h=(b+c+d)/3; printf("%d", h); printf("\n"); if(h>=90 && h=80 && h=70 && h=60 && h |
![]() |
![]() |
#20 |
|
It looks good, you're almost there.
As for your question, to make the scanf() function accept letters instead of numbers, change the format parameter (1st parameter) to "%c", which means single character (%d means decimal integer). Then use an "if" statement to test if the character is a "y" or "n" So, the code (with changes in bold) should look something like this: Code: [code]void main() { int a,b,c,d,h,z; while(a!=2){ char y; printf("Please make a choice\n"); printf("1-Grade Calculation\n"); printf("2-END\n"); scanf("%d", &a); if (a==1){ printf("Please enter your first grade:"); scanf("%d", &b); printf("\n"); printf("Please enter your second grade:"); scanf("%d", &c); printf("\n"); printf("Please enter your third grade:"); scanf("%d", &d); printf("The average is:"); h=(b+c+d)/3; printf("%d", h); printf("\n"); <b>if(h>=90 && h=80 && h=70 && h=60 && h=0 && h |
![]() |
Reply to Thread New Thread |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
|