944,162 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 8449
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Feb 12th, 2007
0

Convert to Absolute Values

Expand Post »
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.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. /*Funtion Prototypes*/
  6.  
  7. int genRandom(void);
  8.  
  9.  
  10. int main(void)
  11.  
  12. {
  13. /*Declare variables*/
  14.  
  15. int rounds;
  16. int ranNum;
  17. int guess;
  18. int attempts=5;
  19. int score;
  20.  
  21.  
  22. printf("\nEnter the desired number of rounds\n");
  23. scanf("%d",&rounds);
  24.  
  25.  
  26. do{
  27. ranNum=genRandom();/*Call random function*/
  28.  
  29. printf("Your rannum is %d\n", ranNum);
  30.  
  31. do{
  32. printf("Guess the value of the random number\n");
  33. scanf("%d",&guess);
  34.  
  35. if(guess>100)
  36. {
  37. printf("Invalid!!!Your source base must be less than 100\n\n");
  38.  
  39. }
  40.  
  41. attempts=attempts-1;
  42.  
  43.  
  44. if (ranNum==guess)
  45. {
  46. printf("Congratulations,You are correct!\n");
  47.  
  48. }
  49.  
  50.  
  51. if ((ranNum-guess) >40)
  52. {
  53. printf("You are Cold\a\n");
  54. }
  55. else if (10<=(ranNum-guess)<=40)
  56. {
  57. printf("You are Warm\a\n");
  58.  
  59. }
  60. else if ((ranNum-guess) < 10)
  61. {
  62. printf("You are Hot!\a\n");
  63.  
  64. }
  65. }while(attempts!=0);
  66.  
  67. }while(rounds!=0);
  68.  
  69.  
  70. score=5*(5-attempts);
  71.  
  72. printf("Your score is %d\n", score);
  73.  
  74. }/* end of main*/
  75.  
  76. int genRandom(void)
  77. {
  78. return rand()%100;
  79. }
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.
Reputation Points: 10
Solved Threads: 0
Junior Poster
boujibabe is offline Offline
123 posts
since Nov 2004
Feb 13th, 2007
0

Re: Convert to Absolute Values

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 break to 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:
    1. if (10<=(ranNum-guess)<=40)
    Since you already checked that the difference is smaller than 40, just check that the difference isn't smaller than 10.
  • 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.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Feb 13th, 2007
0

Re: Convert to Absolute Values

Some points:
  • To guarantee that a particular integer will always be postive (absolute), declare it as an unsigned int.
[LIST]

what if I typecast it
  1. ((unsigned int)(ranNum-guess) >40)
  • You can use break to 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.
I been trying to work on my indentation and I thought I was getting better can you suggest a good code editor that would help?

Thanks.
Last edited by boujibabe; Feb 13th, 2007 at 10:53 am.
Reputation Points: 10
Solved Threads: 0
Junior Poster
boujibabe is offline Offline
123 posts
since Nov 2004
Feb 13th, 2007
0

Re: Convert to Absolute Values

here is the revised code
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. /*Funtion Prototypes*/
  5. int genRandom(void);
  6.  
  7. int main(void)
  8.  
  9. {
  10. /*Declare variables*/
  11.  
  12. int rounds;
  13. int ranNum;
  14. int guess;
  15. int attempts=5;
  16. int score;
  17.  
  18.  
  19. printf("\nEnter the desired number of rounds\n");
  20. scanf("%d",&rounds);
  21. do{
  22. ranNum=genRandom();/*Call random function*/
  23. --rounds;
  24. do{
  25. printf("Guess the value of the random number\n");
  26. scanf("%d",&guess);
  27. --attempts;
  28.  
  29. if((guess>100)||(guess<0))
  30. {
  31. printf("Invalid!!!your number must less than 100 or more than 0\n\n");
  32. continue;
  33. }
  34.  
  35. if (ranNum==guess)
  36. {
  37. printf("Congratulations,You are correct!\n");
  38. break;
  39. }
  40.  
  41. if ((unsigned int)(ranNum-guess) >40)
  42. {
  43. printf("You are Cold\n");
  44. }
  45. else if (10<=(unsigned int)(ranNum-guess))
  46. {
  47. printf("You are Warm\n");
  48. }
  49. else if ((unsigned int)(ranNum-guess) < 10)
  50. {
  51. printf("You are Hot!\n");
  52. }
  53. }while(attempts!=0);
  54. }while(rounds!=0);
  55.  
  56. score=5*(5-attempts);
  57.  
  58. printf("Your score is %d\n", score);
  59.  
  60. }/* end of main*/
  61.  
  62. int genRandom(void)
  63. {
  64. return rand()%100;
  65. }

The score is noncumulative and is suppsed to show at the end of each round
Reputation Points: 10
Solved Threads: 0
Junior Poster
boujibabe is offline Offline
123 posts
since Nov 2004
Feb 13th, 2007
0

Re: Convert to Absolute Values

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.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Feb 13th, 2007
0

Re: Convert to Absolute Values

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.
Reputation Points: 10
Solved Threads: 0
Junior Poster
boujibabe is offline Offline
123 posts
since Nov 2004
Feb 13th, 2007
0

Re: Convert to Absolute Values

Click to Expand / Collapse  Quote originally posted by boujibabe ...
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.
Hmm... the "break" statement works fine for me, except that you aren't calculating the score correctly. You should calculate the score right after the round finishes, and before you reset the attempts.

Like this:
  1. }while(attempts!=0);
  2. score+=5*(5-attempts);
Notice that I put += instead of just =, since the score is supposed to be accumlative. Which means you're going to have to initalize 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.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Feb 14th, 2007
0

Re: Convert to Absolute Values

Click to Expand / Collapse  Quote originally posted by boujibabe ...
I been trying to work on my indentation and I thought I was getting better
Here's a tutorial about code formatting. Concentrate on whitespace as well as indentation. A differnt IDE won't do it all for you, and knowing how to format is extremely important.
Moderator
Reputation Points: 3281
Solved Threads: 896
Posting Sage
WaltP is offline Offline
7,749 posts
since May 2006
Feb 14th, 2007
0

Re: Convert to Absolute Values

Click to Expand / Collapse  Quote originally posted by boujibabe ...
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.
Post the updated code so I can see what you have come up with...
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Feb 15th, 2007
0

Re: Convert to Absolute Values

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. /*Funtion Prototypes*/
  5. int genRandom(void);
  6.  
  7. int main(void)
  8.  
  9. {
  10. /*Declare variables*/
  11.  
  12. int rounds;
  13. int ranNum;
  14. int guess;
  15. int attempts=5;
  16. int score;
  17.  
  18.  
  19. printf("\nEnter the desired number of rounds\n");
  20. scanf("%d",&rounds);
  21. do{
  22. ranNum=genRandom();/*Call random function*/
  23. --rounds;
  24. do{
  25. printf("Guess the value of the random number\n");
  26. scanf("%d",&guess);
  27.  
  28.  
  29. if((guess>100)||(guess<0))
  30. {
  31. printf("Invalid!!!your number must less than 100 or more than 0\n\n");
  32. continue;
  33. }
  34.  
  35. if (ranNum==guess)
  36. {
  37. printf("Congratulations,You are correct!\n");
  38. break;
  39. }
  40.  
  41. if ((unsigned int)(ranNum-guess) >40)
  42. {
  43. printf("You are Cold\n");
  44. }
  45. else if (10<=(unsigned int)(ranNum-guess))
  46. {
  47. printf("You are Warm\n");
  48. }
  49. else if ((unsigned int)(ranNum-guess) < 10)
  50. {
  51. printf("You are Hot!\n");
  52. }
  53. --attempts;
  54. }while(attempts!=0);
  55.  
  56. score+=5*(5-attempts);
  57. attempts=5;
  58.  
  59. }while(rounds!=0);
  60.  
  61. printf("Your score is %d\n", score);
  62.  
  63. }/* end of main*/
  64.  
  65. int genRandom(void)
  66. {
  67. return rand()%100;
  68. }

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.
Reputation Points: 10
Solved Threads: 0
Junior Poster
boujibabe is offline Offline
123 posts
since Nov 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: interfacing et al
Next Thread in C Forum Timeline: how do u find prime numbers in an array





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC