| | |
C++ Function Homework
![]() |
•
•
Join Date: Oct 2008
Posts: 17
Reputation:
Solved Threads: 0
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.
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)
#include <iostream> #include <string> using namespace std; void det_max(int yards,int max) { if (yards >= max) max = yards; } void showRByards(string playername, int weekno, int yards, int stars) { cout << "Input a RB Name: (or type x to stop)" << playername << endl; cout << "Input Week " << weekno <<"'s yardage : "<< yards << endl; cout << "Yards for the week: " << yards << endl; cout << playername << "'s rushing yardage for week " << weekno << ": " ; stars = yards/10; int j = 0; while (j<stars) { cout << "*"; j++; } cout << endl; } int main () { int max = 0; int count = 0; int weekno = 1; string name; string playername = " "; // Variable for player names int weeks, yards, stars; cout << "Input the number of weeks: "; cin >> weeks; if (weeks>0 && weeks<7) { while (playername != "x") { cout << "Input the RB name (or type x to stop): "; cin >> playername; while (count<weeks) { count++; cout << "Input Week " <<weekno<<"'s yardage : "; cin >> yards; det_max(yards,max); showRByards(playername,weekno,yards,stars); weekno++; } } cout << playername<<"'s best week was with "<<max << "yards."; } else cout <<"Invalid number of weeks." <<endl; return 0; }
1) You ask for the name in main( ), and again in the showRByards( ) function. Pick one.
2)
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.
2)
C++ Syntax (Toggle Plain Text)
void det_max(int yards,int max) { if (yards >= max) max = yards; }
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.
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.
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)
int det_max(int yards, int max) { if(yards>=max) return max; else return yards; } ... int main() { ... max=det_max(yards,max); ... }
Last edited by Nichito; Oct 28th, 2008 at 1:36 am.
-->sometimes i wanna take my toaster in a bath<-- ![]() |
Similar Threads
- Function help/homework (C++)
- Deleting characters function ( with just one parameter) (C)
- Dynamic memory allocation homework (C++)
- Function that returns void (C++)
- Homework Help!! Priority Queue ?? (Computer Science)
Other Threads in the C++ Forum
- Previous Thread: Permutations of a String
- Next Thread: Building a better rand()
Views: 388 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C++
algorithm api array arrays assignment beginner binary browser c++ c++borland c/c++ calculator char class classes code compile compiler constructor conversion convert count delete display dll dynamic encryption error file files filestream fstream function functions game givemetehcodez graph graphics gui homework i/o iamthwee input int integer lazy library linker list loop loops math matrix memory newbie news number object objects opengl output parameter pointer pointers problem program programming project random read reading recursion recursive reference return sort sorting spoonfeeding string strings struct student studio template templates text time tree url variable vc++ vector video visual visualstudio win32 window windows winsock xml






