-2

I do not have any error but when I run the program sales name 1 not print out. I am using C++.

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;


void getData(string names[],double sales[3][4]); void
calTotalSales(string names[],double sales[3][4]);

int main() { double sales[3][4]; string names[3];

getData(names,sales);

calTotalSales(names,sales);    return 0; }

void getData(string names[],double sales[3][4]) {

   for(int i=0;i<3;i++)    {

       cout<<"Enter in 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 in the data for the quarter "<<j+1<<": ";
           cin>>sales[i][j];
       }
       cout<<endl;    } }


void calTotalSales(string names[],double sales[3][4]) {

   double max;    string name;    int i;



   cout << setprecision(2) << fixed;

       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 << "name is " << name << endl;

       cout << "Salesman " << name << " had the highest sale for the quarter " << j + 1 << " with $" << max << endl;    } }
halfer
  • 19,824
  • 17
  • 99
  • 186
jin kim
  • 29
  • 2
  • 3
    Consider using line-by-line debug with "watches" window opened in you favorite IDE. It's really good in solving this kind of problems – alexeykuzmin0 Dec 06 '16 at 11:09

1 Answers1

0

First some things, i assume that you are a beginner in c++ so i will give you some advice if you dont mind, avoid to use using namespace std try not to get used, here there is a explanation about why you should not use it,and this is just because i am a bit obsessive but try to format your code better it will make easier to understand every part.

Now the important part, your code works perfectly but "sales name 1" does not print because once you set max here : max = sales[0][j]; in case that this position is actually the max it will never enter in this if :

if(max < sales[i][j])
{
     max = sales[i][j];
     name = names[i];
}

So if it never enters in that if name will never be changed and thats why it does not print sales name 1 , so the solution is as simple as this changes where you set the max = sales[0][j] for this :

max = sales[0][j];
name = names[0];

Adding this last line will fix your problem.

Sorry if i didnt explain myself enough clearly (its my first answer :P)

Community
  • 1
  • 1
JLentubbi
  • 31
  • 4