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.

#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)

void det_max(int yards,int max)
{
        if (yards >= max)
                max = yards;
}

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.

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.

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:

int det_max(int yards, int max)
{
   if(yards>=max)
      return max;
   else
     return yards;
}
...
int main()
{
...
max=det_max(yards,max);
...
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.