943,923 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 493
  • C++ RSS
Oct 28th, 2008
0

C++ Function Homework

Expand Post »
For my homework I have to create a program with functions and nexted while loops. I have got most of the program written but it's not executing how I want it to. The program is setup as a football program, (RB = Running Back it's a football playing position)
it first ask the user number of weeks which must be more then 0 but less than 7;
next it ask for the RB's name, if x is typed the program terminates;
then it asks for the number of yards for that week;
then it displays the RB's name the week number and a row of stars with each star counting for 10 yards;
the program goes through all the weeks then displays the RB's name and the maximum yards he/she got in a week.
then it executes the loop again asking for the RB's name, if x is typed the program terminates; etc.

I'm having four problems so far.
1) After entering the number of weeks, name and yardage it displays the name and yardage again but with the answers then asks for next weeks yardage.
2) It never displays the maximum yards.
3) It asks for the next RB's name put instead of asking for their yardage it asks the next RB's name.
4) When x is entered at the beginning it displays that the maximum yardage is 0 I'd rather it didn't display anything.

C++ Syntax (Toggle Plain Text)
  1.  
  2. #include <iostream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7.  
  8. void det_max(int yards,int max)
  9. {
  10. if (yards >= max)
  11. max = yards;
  12. }
  13.  
  14. void showRByards(string playername, int weekno, int yards, int stars)
  15. {
  16. cout << "Input a RB Name: (or type x to stop)" << playername <<
  17. endl;
  18. cout << "Input Week " << weekno <<"'s yardage : "<< yards <<
  19. endl;
  20. cout << "Yards for the week: " << yards << endl;
  21.  
  22. cout << playername << "'s rushing yardage for week " << weekno << ": " ;
  23. stars = yards/10;
  24. int j = 0;
  25. while (j<stars)
  26. {
  27. cout << "*";
  28. j++;
  29. }
  30. cout << endl;
  31. }
  32. int main ()
  33. {
  34. int max = 0;
  35. int count = 0;
  36. int weekno = 1;
  37.  
  38. string name;
  39. string playername = " ";
  40.  
  41. // Variable for player names
  42.  
  43. int weeks, yards, stars;
  44.  
  45. cout << "Input the number of weeks: ";
  46. cin >> weeks;
  47.  
  48. if (weeks>0 && weeks<7)
  49. {
  50. while (playername != "x")
  51. {
  52. cout << "Input the RB name (or type x to stop): ";
  53. cin >> playername;
  54. while (count<weeks)
  55. {
  56. count++;
  57. cout << "Input Week " <<weekno<<"'s yardage : ";
  58. cin >> yards;
  59.  
  60. det_max(yards,max);
  61. showRByards(playername,weekno,yards,stars);
  62. weekno++;
  63. }
  64. }
  65. cout << playername<<"'s best week was with "<<max << "yards.";
  66. }
  67. else
  68. cout <<"Invalid number of weeks." <<endl;
  69. return 0;
  70.  
  71. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
StainlessSteelR is offline Offline
17 posts
since Oct 2008
Oct 28th, 2008
0

Re: C++ Function Homework

1) You ask for the name in main( ), and again in the showRByards( ) function. Pick one.

2)
C++ Syntax (Toggle Plain Text)
  1. void det_max(int yards,int max)
  2. {
  3. if (yards >= max)
  4. max = yards;
  5. }
How is the new max supposed to be communicated back to the caller?

Review your notes on "pass by reference".

3) Look at variable "count". That's what's stopping you from getting 2nd and subsequent RB's data. What state is it left in when the first RB's data has all been entered? What state should it be in for the next guy?

4) Put a test before the max yards output.
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Oct 28th, 2008
0

Re: C++ Function Homework

the code you have there asks for the name, week and yardage twice: once in the main function and a second time in the showRByards before showing the stars... maybe you should try removing it from the main function.
Featured Poster
Reputation Points: 424
Solved Threads: 57
Posting Virtuoso
Nichito is offline Offline
1,594 posts
since Mar 2007
Oct 28th, 2008
0

Re: C++ Function Homework

you should make your det_max function an int instead of a void fucntion, returning the max value and assigning it to the max variable in yourmain function... like this:
c++ Syntax (Toggle Plain Text)
  1. int det_max(int yards, int max)
  2. {
  3. if(yards>=max)
  4. return max;
  5. else
  6. return yards;
  7. }
  8. ...
  9. int main()
  10. {
  11. ...
  12. max=det_max(yards,max);
  13. ...
  14. }
Last edited by Nichito; Oct 28th, 2008 at 2:36 am.
Featured Poster
Reputation Points: 424
Solved Threads: 57
Posting Virtuoso
Nichito is offline Offline
1,594 posts
since Mar 2007

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: Permutations of a String
Next Thread in C++ Forum Timeline: Building a better rand()





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


Follow us on Twitter


© 2011 DaniWeb® LLC