Problems with Assignment once again

Reply

Join Date: Feb 2008
Posts: 43
Reputation: curt1203 is an unknown quantity at this point 
Solved Threads: 0
curt1203 curt1203 is offline Offline
Light Poster

Problems with Assignment once again

 
0
  #1
Feb 17th, 2008
I once again am having trouble with this program. I really thought i understood this right off the bat but once i got into it, there is a lot of little technical things my professor wants in it. Like the functions and generating random numbers. Any help would be awesom, i just got done with my other assignment now i have this one due friday....i have an attached file of what the output should look like...


  1.  
  2. //Assignment 5: Guessing game with random numbers and functions
  3. //By Curtis Davenport 2/17/08
  4.  
  5. #include <iostream>
  6. #include <ctime>
  7. #include <cstdlib>
  8. #include <math>
  9.  
  10. using namespace std;
  11.  
  12. int GuessFunction (int)
  13.  
  14. int main()
  15. {
  16.  
  17. int guess;
  18. int target;
  19. int chancesLeft;
  20. bool foundTarget = false;
  21. bool endGame = false;
  22.  
  23. chancesLeft=5 //I need to give a random target for the user to hit and do
  24. //a guess function for it.
  25. target =???? //I dont know how to set up for a user to input a number so he/she
  26. //can hit a random target.
  27.  
  28. do //Overall I need to give the user 5 guesses and give options after.
  29. {
  30. cout << "Guess a number (0-100): ";
  31. cin >> guess;
  32.  
  33. if (guess < target)
  34. {
  35. cout << "Your Guess is too LOW!!! \n";
  36. }
  37. if (guess > target)
  38. {
  39. cout << "Your Guess is too HIGH!!! \n";
  40. }
  41.  
  42. if (
  43.  
  44. chancesLeft--;
  45.  
  46. if (guess == target)
  47. {
  48. foundTarget = true;
  49. endGame = true;
  50. }
  51. if (chancesLeft == 0)
  52. {
  53. endGame = true;
  54. }
  55.  
  56. } while (!endGame);
  57.  
  58. if (foundTarget)
  59. {
  60. cout << "You got it right!!! \n";
  61. }
  62. else
  63. {
  64. cout << "You ran out of chances. \n";
  65.  
  66. return 0;
  67. }
  68. int GuessFunction(int)
  69. { // I am supposed to use a function here, do not know how to set it up.
  70. int result;
  71.  
  72. srand( time(0) );
  73.  
  74. result =
  75.  
  76. return result;
  77. }
  78. //I am supposed to
  79. // (1)Write a program that generates a random integer number
  80. // between 1 and 100 and allows the user to make a maximum of
  81. // five 5 attempts to guess the number.
  82. // (2)The program must let the user know whether the current guess is too high or too low.
  83. // (3)The program must notify the user if he/she made the right guess or ran out of chances.
  84. // (4)At the end of the cycle the program must ask the user for a choice of
  85. // trying again 1 or exiting 2. This answer must be validated (only 1 or 2 are acceptable).
  86. // (5)Seed the random number generation sequence at the beginning of the program using
  87. // the current time in seconds.
  88. // (6)The random target value must be generated inside the main function and passed
  89. // as an input parameter to a Guess function.
  90. // (7)The Guess function must ask the user to enter the guess value, compare it
  91. // with the target, notify the user if the guess is too high or too low, accumulate
  92. // the number of attempts, and check if the maximum number of attempts has been reached.
  93. // (8)The Guess function must return the number of attempts by the user to the main
  94. // function and the main function must use this value to notify the user whether he/she
  95. // ran out of chances or made the right guess.
Attached Files
File Type: doc cetassignment5.doc (50.5 KB, 2 views)
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 88
Reputation: knight fyre is an unknown quantity at this point 
Solved Threads: 1
knight fyre's Avatar
knight fyre knight fyre is offline Offline
Junior Poster in Training

Re: Problems with Assignment once again

 
0
  #2
Feb 17th, 2008
That should solve your problem with the rand function.

  1. int GuessFunction(int)
  2. {
  3. int result;
  4. srand( time(NULL) );
  5. result = rand()% 99 + 1 // range for rand() = (max - min) + min
  6. return result;
  7. }
  8. //I am supposed to
  9. // (1)Write a program that generates a random integer number
  10. // between 1 and 100
Last edited by Ancient Dragon; Feb 19th, 2008 at 2:56 pm. Reason: replaced ICODE tags with CODE tags
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 88
Reputation: knight fyre is an unknown quantity at this point 
Solved Threads: 1
knight fyre's Avatar
knight fyre knight fyre is offline Offline
Junior Poster in Training

Re: Problems with Assignment once again

 
0
  #3
Feb 17th, 2008
After a more thorough reading I corrected your code. The comments are in the file. Feel free to post any issues you might have.
Attached Files
File Type: cpp random.cpp (4.0 KB, 10 views)
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 43
Reputation: curt1203 is an unknown quantity at this point 
Solved Threads: 0
curt1203 curt1203 is offline Offline
Light Poster

Re: Problems with Assignment once again

 
0
  #4
Feb 19th, 2008
Hey knight fyre, can you post the code you have improved upon in daniweb rather than an attachmet. I am having trouble opening up your document. Anyways thanks so much for your input on this assignment. You have helped me out a bunch, if you could post your improvements to my code in daniweb id like to see what changes you have made and see if i can actually learn this stuff!!! thanks again
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 88
Reputation: knight fyre is an unknown quantity at this point 
Solved Threads: 1
knight fyre's Avatar
knight fyre knight fyre is offline Offline
Junior Poster in Training

Re: Problems with Assignment once again

 
0
  #5
Feb 19th, 2008
I'm not verse with posting actual code on daniweb but here goes:

  1. //Assignment 5: Guessing game with random numbers and functions
  2.  
  3. //By Curtis Davenport 2/17/08
  4.  
  5.  
  6. #include <iostream>
  7. #include <ctime>
  8. #include <cstdlib>
  9. #include <math.h>
  10.  
  11. using namespace std;
  12.  
  13. int GuessFunction (int);
  14.  
  15. int main()
  16. {
  17. int target, attempts, choice; // can be declared in the same line if the data types are the same
  18.  
  19. do // needed for requirement (4)
  20. {
  21. srand( time(NULL)); // maximum + minumum
  22. target = rand()% 99 + 1; // range for rand() = (max - min) + min
  23. // for range between 100 to 1 (100 - 1) + 1 = rand() % 99 + 1
  24. // the result of rand() is stored in target
  25.  
  26. attempts = GuessFunction(target); // GuessFunction accepts the int variable target and returns attempts to main
  27.  
  28. // Tells the user whether they were sucessful or not after five or less tries
  29. if (attempts <= 5)
  30. {
  31. cout << "You got it right in " << attempts << " attempts" << "!!! \n";
  32. }
  33.  
  34. if (attempts > 5)
  35. {
  36. cout << "You ran out of chances. \n";
  37. }
  38.  
  39. cout << "\n\nDo you want to play again? 1 for Yes - 2 for No: ";
  40. cin >> choice;
  41. system("PAUSE"); // pauses screen and prints message. I do not recommend this method in favour of cin.get();
  42. } while(choice == 1); // end of dowhile for (4)
  43.  
  44. return 0;
  45. }
  46.  
  47. /*The Guess function must ask the user to enter the guess value, compare it
  48. with the target, notify the user if the guess is too high or too low, accumulate
  49. the number of attempts, and check if the maximum number of attempts has been reached.
  50.  
  51. NOT THE MAIN!!!*/
  52.  
  53. // GuessFunction implementation
  54. int GuessFunction(int target)
  55. {
  56. bool foundTarget = false;
  57. bool endGame = false;
  58. int guess, chancesLeft=5;
  59.  
  60. do
  61. {
  62. cout << "Guess a number (0-100): ";
  63. cin >> guess;
  64.  
  65. if (guess < target)
  66. {
  67. cout << "Your Guess is too LOW!!! \n";
  68. chancesLeft = chancesLeft - 1; // decrement chances left by one to represent attempt
  69. }
  70.  
  71. if (guess > target)
  72. {
  73. cout << "Your Guess is too HIGH!!! \n";
  74. chancesLeft = chancesLeft - 1; // decrement chances left by one to represent attempt
  75. }
  76.  
  77. if (guess == target)
  78. {
  79. chancesLeft = chancesLeft - 1; // decrement chances left by one to represent attempt
  80. foundTarget = true;
  81. endGame = true;
  82. }
  83.  
  84. if (chancesLeft == 0)
  85. {
  86. endGame = true;
  87. }
  88.  
  89. } while (!endGame);
  90.  
  91. if (foundTarget)
  92. {
  93. return (5 - chancesLeft); // returns a value that represents the number of attempts to main
  94. } // 5 represents the total allowed chances
  95. else // 5 less the # of chances left equals the # of chances used
  96. {
  97. return 6; // any value greater that 5 would suffice. use to activate the second if function in main
  98. }
  99.  
  100. } // end of function


It looks better when you click "Toggle Plain Text" and cut and paste it to a c++ compiler.
Last edited by knight fyre; Feb 19th, 2008 at 6:05 pm. Reason: Included c++ highlighting
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 43
Reputation: curt1203 is an unknown quantity at this point 
Solved Threads: 0
curt1203 curt1203 is offline Offline
Light Poster

Re: Problems with Assignment once again

 
0
  #6
Feb 20th, 2008
I made some of the changes you made and it worked great. I see how you used the function and how it was implemented. It just takes such a visionary to come up with even a simple program like this. I just can not see in my head how it should look in the compiler, it makes sense once i read and see how the order of the code is. But on my own i have a difficult time coming up with this off the top of my head. But thanks so much for your help, this class is so hard for me and i am putting in many hours to try to learn it. Thanks again for your help i really appreciate it!!!
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 88
Reputation: knight fyre is an unknown quantity at this point 
Solved Threads: 1
knight fyre's Avatar
knight fyre knight fyre is offline Offline
Junior Poster in Training

Re: Problems with Assignment once again

 
1
  #7
Feb 20th, 2008
Programming takes reading and plenty of practice!

Before you even open the compiler spend 2 minutes reading through the question and 3 minutes analyzing it. While analyzing ask yourself:

What information is my program going to accept from the user and what is their data type? It could be a persons name: type string/char[x], their age: type int, or their salary: type float, and so on.

After that determine the functions you are going to need to create based on the requirements of the question then ask yourself:

What is my function going to accept to do the job? It could be s person's name which is of type char or string, a user's salary of type int or float, and a user's income tax: type float or int in order to calculate their net salary.

Then ask:

Is it going to return anything? If nothing is a procedure and it might just print something to the screen, something like
  1. salary = salary - tax;
  2. printf("salary is %d", salary);
or it could return a value to main or even another function!
  1. salary = salary - tax;
  2. return salary;

Then ask:

Do you need a loop (while or for) to accomplish xyz task or a decision (switch/case, if, if else) in the function to accomplish xyz task or both.

As a beginner it's best break down each task into a function.

Originally Posted by curt1203 View Post
I made some of the changes you made and it worked great. I see how you used the function and how it was implemented. It just takes such a visionary to come up with even a simple program like this. I just can not see in my head how it should look in the compiler, it makes sense once i read and see how the order of the code is. But on my own i have a difficult time coming up with this off the top of my head. But thanks so much for your help, this class is so hard for me and i am putting in many hours to try to learn it. Thanks again for your help i really appreciate it!!!
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 5
Reputation: kjc367 is an unknown quantity at this point 
Solved Threads: 0
kjc367 kjc367 is offline Offline
Newbie Poster

Re: Problems with Assignment once again

 
0
  #8
Feb 21st, 2008
Take a close look at line 22. Modulo returns a number from 0...N-1. So modulo 99 + 1 returns a value from 1 to 99 not 100.

Also, look at lines 68, 74, 79. Shouldn't this be one line outside the if()s?
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 88
Reputation: knight fyre is an unknown quantity at this point 
Solved Threads: 1
knight fyre's Avatar
knight fyre knight fyre is offline Offline
Junior Poster in Training

Re: Problems with Assignment once again

 
0
  #9
Feb 21st, 2008
A closer look at my rand() and some debugging revealed it to be flawed. This is a working formula for rand:

  1. rand () % (high - low + 1) + low;

Lines 68, 74, and 79 does the job but 1 (ONLY ONE)
  1. chancesLeft = chancesLeft - 1;
is needed if placed outside the if statements and must be placed in the do...while loop.

Originally Posted by kjc367 View Post
Take a close look at line 22. Modulo returns a number from 0...N-1. So modulo 99 + 1 returns a value from 1 to 99 not 100.

Also, look at lines 68, 74, 79. Shouldn't this be one line outside the if()s?
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 43
Reputation: curt1203 is an unknown quantity at this point 
Solved Threads: 0
curt1203 curt1203 is offline Offline
Light Poster

Re: Problems with Assignment once again

 
0
  #10
Feb 22nd, 2008
Thanks so much for your help. I think i am starting to sorta get a grip on this stuff. We are now talking about arrays and these seem a bit overwhelming. If i have any more problems ill post something. But thanks again for all of the help and direction in programming.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1146 | Replies: 9
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC