Extend part 1 such that the name, and the corresponding sales amount, of the salesman who sold the highest sales are displayed for each quarter.
You must not use global variables.
Here is a sample output:
Enter in the name for salesman 1: Leo[Enter]
Now enter in the sales for each quarter for Leo
Enter in data for quarter 1: 23001[Enter]
Enter in data for quarter 2: 82002[Enter]
Enter in data for quarter 3: 56003[Enter]
Enter in data for quarter 4: 95004[Enter]
Enter in the name for salesman 2: Jane[Enter]
Now enter in the sales for each quarter for Jane
Enter in data for quarter 1: 93011[Enter]
Enter in data for quarter 2: 37012[Enter]
Enter in data for quarter 3: 81013[Enter]
Enter in data for quarter 4: 94014[Enter]
Enter in the name for salesman 3: Judy[Enter]
Now enter in the sales for each quarter for Judy
Enter in data for quarter 1: 43710[Enter]
Enter in data for quarter 2: 83721[Enter]
Enter in data for quarter 3: 92732[Enter]
Enter in data for quarter 4: 92177[Enter]
Salesman Jane had the highest sales for quarter 1 with $93011.00
Salesman Judy had the highest sales for quarter 2 with $83721.00
Salesman Judy had the highest sales for quarter 3 with $92732.00
Salesman Leo had the highest sales for quarter 4 with $95004.00
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
string names[3];
double sales[3][4], total = 0;
for(int i = 0 ; i <3; i++)
{
cout << "Enter the name of the salesman " << i + 1 << ": ";
cin >> names[i];
cout << "Now enter in the sales for each quarter for " << names[i] << endl;
for(int j = 0; j < 4; j++)
{
cout << "Enter the data for quarter " << j + 1 << ": ";
cin >> sales[i][j];
}
cout << endl;
}
cout << endl;
double max;
string name;
int i;
for(int j = 0; j < 4; j++)
{
max = sales[0][j];
for(i = 0; i < 3; i++)
{
name = names[i];
if(max < sales[i][j])
{
max = sales[i][j];
name = names[i];
}
}
cout << "Salesman " << name << " had the highest sale for the quarter " << j + 1 << " with $" << max << ".00" << endl;
}
cout << endl;
return 0;
}
And the output I get is
Enter the name of the salesman 1: Leo
Now enter in the sales for each quarter for Leo
Enter the data for quarter 1: 23001
Enter the data for quarter 2: 82002
Enter the data for quarter 3: 56003
Enter the data for quarter 4: 95004
Enter the name of the salesman 2: Jane
Now enter in the sales for each quarter for Jane
Enter the data for quarter 1: 93011
Enter the data for quarter 2: 37012
Enter the data for quarter 3: 81013
Enter the data for quarter 4: 94014
Enter the name of the salesman 3: Judy
Now enter in the sales for each quarter for Judy
Enter the data for quarter 1: 43710
Enter the data for quarter 2: 83721
Enter the data for quarter 3: 92732
Enter the data for quarter 4: 92177
Salesman Judy had the highest sale for the quarter 1 with $93011.00
Salesman Judy had the highest sale for the quarter 2 with $83721.00
Salesman Judy had the highest sale for the quarter 3 with $92732.00
Salesman Judy had the highest sale for the quarter 4 with $95004.00
All of the names are Judy when the first one should be Jane and the fourth one should be Leo. Can anyone help me with this?
You are overwriting name in line 39 each time through the loop. You should instead initialize max to 0.0 and name to "" in line 25 and then only update them if max is superceded.
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
string names[3];
double sales[3][4], total = 0;
for(int i = 0 ; i <3; i++)
{
cout << "Enter the name of the salesman " << i + 1 << ": ";
cin >> names[i];
cout << "Now enter in the sales for each quarter for " << names[i] << endl;
for(int j = 0; j < 4; j++)
{
cout << "Enter the data for quarter " << j + 1 << ": ";
cin >> sales[i][j];
}
cout << endl;
}
cout << endl;
double max;
string name;
int i;
for(int j = 0; j < 4; j++)
{
max = sales[0][j];
for(i = 0; i < 3; i++)
{
if(max < sales[i][j])
{
max = sales[i][j];
name = names[i];
}
}
cout << "Salesman " << name << " had the highest sale for the quarter " << j + 1 << " with $" << max << ".00" << endl;
}
cout << endl;
return 0;
}
Here is my output
Enter the name of the salesman 1: Leo
Now enter in the sales for each quarter for Leo
Enter the data for quarter 1: 23001
Enter the data for quarter 2: 82002
Enter the data for quarter 3: 56003
Enter the data for quarter 4: 95004
Enter the name of the salesman 2: Jane
Now enter in the sales for each quarter for Jane
Enter the data for quarter 1: 93011
Enter the data for quarter 2: 37012
Enter the data for quarter 3: 81013
Enter the data for quarter 4: 94014
Enter the name of the salesman 3: Judy
Now enter in the sales for each quarter for Judy
Enter the data for quarter 1: 43710
Enter the data for quarter 2: 83721
Enter the data for quarter 3: 92732
Enter the data for quarter 4: 92177
Salesman Jane had the highest sale for the quarter 1 with $93011.00
Salesman Judy had the highest sale for the quarter 2 with $83721.00
Salesman Judy had the highest sale for the quarter 3 with $92732.00
Salesman Judy had the highest sale for the quarter 4 with $95004.00
I get the right output for quarter 1-3 but on quarter 4 it should be Leo.
You should instead initialize max to 0.0 and name to "" in line 25...
.
In your defense, there was a typo in this. It should have said line 35. (I think my eyes are going bad, and it's hard for me to distinguish 3s and 2s when printed in gray font.)
What's happening is you are initializing max to Leo's value for every loop in line 35. In line 41, Leo's value will never be less than itself, so you never drop into the body of the if statement. You need to reset max to 0.0 (or a negative value it you want).
The 4th time through the loop, name is already set at "Judy", and you set max to Leo's value in line 35. Lines 43-45 never get executed, and you get what you see.
By the way, do you have access to a debugger? Many IDEs (like CodeBlocks) have them built in, and you can step through your code to see what's happening.