2016-12-06 32 views
-2

我没有任何错误,但是当我运行程序销售名称1时未打印出来。我正在使用C++。当我运行这个C++程序时,销售名称1不打印

#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; } } 
+3

考虑使用行由行调试,在你最喜欢的IDE中打开“手表”窗口。解决这类问题真的很不错 – alexeykuzmin0

回答

0

一是一些事情,我认为你是在C初学者++,所以我会给你一些建议,如果你不介意,避免使用using namespace std尽量不要习惯,这里有一个关于why you should not use it解释,这只是因为我有点迷恋,但试着更好地设置你的代码的格式,这会让你更容易理解每​​个部分。

现在的重要组成部分,你的代码工作完美,但“销售名称1”不打印,因为一旦您在此处设置最大值:max = sales[0][j];的情况下,这一立场实际上是它永远不会在此输入的最高如果:

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

所以,如果它从未进入,如果名字将永远不会被改变,这就是为什么它不打印的销售名称1,那么,你为这个设置max = sales[0][j]的解决方案很简单,只要这改变:

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

添加最后一行将解决您的问题。

很抱歉,如果我没有解释自己不够明确(其我的第一个答案:P)