C++ Function Homework

Reply

Join Date: Oct 2008
Posts: 17
Reputation: StainlessSteelR is an unknown quantity at this point 
Solved Threads: 0
StainlessSteelR StainlessSteelR is offline Offline
Newbie Poster

C++ Function Homework

 
0
  #1
Oct 28th, 2008
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.

  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. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,818
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 213
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: C++ Function Homework

 
0
  #2
Oct 28th, 2008
1) You ask for the name in main( ), and again in the showRByards( ) function. Pick one.

2)
  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.
Don't own a gun but I'm going to join the NRA.
I figure anything that irritates liberals is worth my support.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 1,441
Reputation: Nichito is an unknown quantity at this point 
Solved Threads: 32
Featured Poster
Nichito's Avatar
Nichito Nichito is offline Offline
Nearly a Posting Virtuoso

Re: C++ Function Homework

 
0
  #3
Oct 28th, 2008
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.
-->sometimes i wanna take my toaster in a bath<--
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 1,441
Reputation: Nichito is an unknown quantity at this point 
Solved Threads: 32
Featured Poster
Nichito's Avatar
Nichito Nichito is offline Offline
Nearly a Posting Virtuoso

Re: C++ Function Homework

 
0
  #4
Oct 28th, 2008
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:
  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 1:36 am.
-->sometimes i wanna take my toaster in a bath<--
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: 388 | Replies: 3
Thread Tools Search this Thread



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

©2003 - 2010 DaniWeb® LLC