2016-11-10 98 views
-1

它是我的输入输出还是其他导致问题的东西。我对C++非常陌生,所以如果事情很简单,我很抱歉。如果您需要更多信息,我已经在代码中包含了更多关于该问题的细节。当我尝试输出二维数组时,它输出错误的数字。

#include <iostream> 
#include <iomanip> 

using namespace std; 

void input(string names[],int grades[][3]){ 
    for(int n=0;n<3;n++){//Enter 3 into the grade book 
     cout<<"Please enter student name "<<n+1<<endl; 
      cin>>names[n]; 
} 
    for(int r=0;r<3;r++){// Enter 4 grades for each of the 3 students you added  to the grade book 
     cout<<"Please enter 4 grades for "<<names[r]<<endl; 
     for(int c=0;c<4;c++){ 
      cout<<"Grade "<<c+1<<": "; 
       cin>>grades[r][c]; 
     } 
     cout<<endl; 
    } 
} 

//ignore this it wil;l be used to average the grades later 
void math(int grades[][3], double average[]){ 

} 
//If I input these number just to test the program 
//student 1: 1,1,1,1 
//student 2: 2,2,2,2 
//student 3: 3,3,3,3 
//it give me 
//student 1: 1,1,1,2 
//student 2: 2,2,2,3 
//student 3: 3,3,3,3 
void output(string names[],int grades[][3],double average[],char letter[]){ 
    for(int r=0;r<3;r++){ 
     cout<<names[r]<<" "; 
     for(int c=0;c<4;c++){ 
      cout<<grades[r][c]<<" "; 
     } 
      cout<<endl; 
    } 
} 

int main(){ 

int grades[2][3]; 
double average[2]; 
char letter[2]; 
string names [2]; 

input(names,grades); 
math(grades,average); 
output(names,grades,average,letter); 


return 0; 
} 
+1

当'grades'被声明为'int grades [] [3]''时,您不能使用(不读取或写入)'grades [r] [3]',因为它超出范围。 – MikeCAT

回答

0

在C++中,您在数组声明中使用的数字不是最大索引,而是元素数量。

你的所有数组声明的缺乏每一个元件,所以对于grades分配一个以上元件(二者渔政,包括参数),averageletternames

0

看起来我的经验不及您,但我最近才被介绍给功能。

您将下标值传递给grade参数,而不是仅将两个下标值留空。尝试从grades参数的括号中取出3。运行代码,看看会发生什么。