| | |
Convert to Absolute Values
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Nov 2004
Posts: 123
Reputation:
Solved Threads: 0
I have this program to write thats a guessing game. the user specifies the amount of rounds and only 5 attempts per round is allowed a round is over when the user guesses correctly or the number of attempts are up. A Message is displayed to inform the user whether he/she is cold, hot or warm based on a certain criteria:
1. How do I work with only absolute or positive values for my criteria (say number is less than guess)
2. I want to break out of the loop when the guess is correct or when the attempts are up.
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> /*Funtion Prototypes*/ int genRandom(void); int main(void) { /*Declare variables*/ int rounds; int ranNum; int guess; int attempts=5; int score; printf("\nEnter the desired number of rounds\n"); scanf("%d",&rounds); do{ ranNum=genRandom();/*Call random function*/ printf("Your rannum is %d\n", ranNum); do{ printf("Guess the value of the random number\n"); scanf("%d",&guess); if(guess>100) { printf("Invalid!!!Your source base must be less than 100\n\n"); } attempts=attempts-1; if (ranNum==guess) { printf("Congratulations,You are correct!\n"); } if ((ranNum-guess) >40) { printf("You are Cold\a\n"); } else if (10<=(ranNum-guess)<=40) { printf("You are Warm\a\n"); } else if ((ranNum-guess) < 10) { printf("You are Hot!\a\n"); } }while(attempts!=0); }while(rounds!=0); score=5*(5-attempts); printf("Your score is %d\n", score); }/* end of main*/ int genRandom(void) { return rand()%100; }
2. I want to break out of the loop when the guess is correct or when the attempts are up.
Some points:
- To guarantee that a particular integer will always be postive (absolute), declare it as an
unsigned int.
- You check to see if the value the user entered was larger than 100, but you never checked if it was smaller than 0.
- You can use
breakto jump out of a loop if you need to, although a better way would be simply to keep checking the value of a boolean variable which represents if it's time to exit the loop.
- This statement makes no sense: Since you already checked that the difference is smaller than 40, just check that the difference isn't smaller than 10.C Syntax (Toggle Plain Text)
- if (10<=(ranNum-guess)<=40)
- Proper code indentation is a code habit to practice. Many editors have a built-in feature, so you don't even have to think about it. And it makes code much easier to read and understand.
"Technological progress is like an axe in the hands of a pathological criminal."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
•
•
Join Date: Nov 2004
Posts: 123
Reputation:
Solved Threads: 0
•
•
•
•
Some points:[LIST]
- To guarantee that a particular integer will always be postive (absolute), declare it as an
unsigned int.
what if I typecast itC Syntax (Toggle Plain Text)
((unsigned int)(ranNum-guess) >40)
- You can use
breakto jump out of a loop if you need to, although a better way would be simply to keep checking the value of a boolean variable which represents if it's time to exit the loop.
- Do you mean like if (attempts>0=1&&rounds>0=1) ?
- Proper code indentation is a code habit to practice. Many editors have a built-in feature, so you don't even have to think about it. And it makes code much easier to read and understand.
Thanks.
Last edited by boujibabe; Feb 13th, 2007 at 10:53 am.
•
•
Join Date: Nov 2004
Posts: 123
Reputation:
Solved Threads: 0
here is the revised code
The score is noncumulative and is suppsed to show at the end of each round
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> /*Funtion Prototypes*/ int genRandom(void); int main(void) { /*Declare variables*/ int rounds; int ranNum; int guess; int attempts=5; int score; printf("\nEnter the desired number of rounds\n"); scanf("%d",&rounds); do{ ranNum=genRandom();/*Call random function*/ --rounds; do{ printf("Guess the value of the random number\n"); scanf("%d",&guess); --attempts; if((guess>100)||(guess<0)) { printf("Invalid!!!your number must less than 100 or more than 0\n\n"); continue; } if (ranNum==guess) { printf("Congratulations,You are correct!\n"); break; } if ((unsigned int)(ranNum-guess) >40) { printf("You are Cold\n"); } else if (10<=(unsigned int)(ranNum-guess)) { printf("You are Warm\n"); } else if ((unsigned int)(ranNum-guess) < 10) { printf("You are Hot!\n"); } }while(attempts!=0); }while(rounds!=0); score=5*(5-attempts); printf("Your score is %d\n", score); }/* end of main*/ int genRandom(void) { return rand()%100; }
The score is noncumulative and is suppsed to show at the end of each round
You are getting incorrect output since you fail to reset the attempts value to 5 again i.e. at the end of the nested do while loop, set the value of attempts again to 5.
Also it is better to decrement the value of attempts at the end of the second do while loop, only after the successful completion of the loop since you seem to be using continue.
And btw, read this thread for getting a good IDE.
Also it is better to decrement the value of attempts at the end of the second do while loop, only after the successful completion of the loop since you seem to be using continue.
And btw, read this thread for getting a good IDE.
I don't accept change; I don't deserve to live.
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
•
•
•
•
Thanks a bundle. It works now except when I'm correct I can't break out of the loop and print the score, also It isn't starting another round after the failed attempts.
Like this:
C Syntax (Toggle Plain Text)
}while(attempts!=0); score+=5*(5-attempts);
score at the beginning of your program.You may want to print a "you failed" message before the round ends so that the user knows that he/she ran out of attempts, and additionally, print out the number of attempts remaining after each guess.
"Technological progress is like an axe in the hands of a pathological criminal."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
•
•
•
•
I been trying to work on my indentation and I thought I was getting better
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
I don't accept change; I don't deserve to live.
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
•
•
Join Date: Nov 2004
Posts: 123
Reputation:
Solved Threads: 0
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> /*Funtion Prototypes*/ int genRandom(void); int main(void) { /*Declare variables*/ int rounds; int ranNum; int guess; int attempts=5; int score; printf("\nEnter the desired number of rounds\n"); scanf("%d",&rounds); do{ ranNum=genRandom();/*Call random function*/ --rounds; do{ printf("Guess the value of the random number\n"); scanf("%d",&guess); if((guess>100)||(guess<0)) { printf("Invalid!!!your number must less than 100 or more than 0\n\n"); continue; } if (ranNum==guess) { printf("Congratulations,You are correct!\n"); break; } if ((unsigned int)(ranNum-guess) >40) { printf("You are Cold\n"); } else if (10<=(unsigned int)(ranNum-guess)) { printf("You are Warm\n"); } else if ((unsigned int)(ranNum-guess) < 10) { printf("You are Hot!\n"); } --attempts; }while(attempts!=0); score+=5*(5-attempts); attempts=5; }while(rounds!=0); printf("Your score is %d\n", score); }/* end of main*/ int genRandom(void) { return rand()%100; }
Now I'm going on infinitely instead of breaking after five attempts. A noncumulative score has to be displayed at the end of each round but once the rounds are up a cumulative score is then displayed.
![]() |
Other Threads in the C Forum
- Previous Thread: interfacing et al
- Next Thread: how do u find prime numbers in an array
Views: 5883 | Replies: 31
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays asterisks binarysearch calculate centimeter char command convert copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax directory dynamic executable fflush file fork forloop frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop kernel km lazy linked linkedlist linux linuxsegmentationfault list lists locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix problem probleminc program programming radix recursion recv repetition research scanf scheduling scripting segmentationfault send sequential shape socketprograming spoonfeeding stack standard string strings structures student systemcall testautomation turboc unix user variable voidmain() wab win32 windows.h






