重新定义形式参数C ++

时间:2014-02-17 23:58:24

标签: c++

让我尝试改变我的方法。我有以下代码,我很想转换成函数。但是,每当我尝试使用函数结构时,我的变量就会开始生成错误。感谢所有迄今为我提供支持的人。

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


using namespace std;
void names(string names[9]);
void grades(double scores[10][5]);
void getData(double scores[10][5]);


void getData(int scores[10][5])
{

}

void names(string names[9])
{


}

void grades(double scores[10][5])
{

}

int main()
{
//create a string of names to run parallel to thestring containg the students average
string names[9] = { "Johnson", "Aniston", "Cooper", "Blair",
    "Clark", "Kennedy", "Bronson", "Sunny", "Smith" };

int scores[10][5] = { { 85, 83, 77, 91, 76 },
{ 80, 90, 95, 93, 48 },
{ 78, 81, 11, 90, 73 },
{ 92, 83, 30, 69, 87 },
{ 23, 45, 96, 38, 59 },
{ 60, 85, 45, 39, 67 },
{ 77, 31, 52, 74, 83 },
{ 93, 94, 89, 77, 97 },
{ 79, 85, 28, 93, 82 },
{ 85, 72, 49, 75, 63 } };


// Calculate the grades per student and store to the "grades" string
double sum = 0;
double avg = 0;
double grades[10];
//start at the first student.Then move to the next after the average has been computed. 
for (int row = 0; row < 11; row++)
{

    for (int col = 0; col < 5; col++)
    {
        //calculate the sum so it can bedevided by 5
        sum += scores[row][col];
    }
    avg = sum / 5;
    //load the students average into an array
    grades[row] = avg;
    //reset the average so that it does not compound
    sum = 0;
    avg = 0;
}

//print names
for (int i = 0; i < 9; i++)
{

    cout << names[i] << endl;
}


//print grades

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

    cout << grades[i] << endl;
}
system("pause");
}

2 个答案:

答案 0 :(得分:2)

在这些功能中

void getData(int scores[10][5])
{
int scores[10][5] = { { 85, 83, 77, 91, 76 },
//...

 void names(string names[9])
{
string names[9] = { "Johnson", "Aniston", "Cooper", "Blair",
    "Clark", "Kennedy", "Bronson", "Sunny", "Smith" };
//...

您在函数体的最外层范围内声明局部变量的方式与其参数的名称相同。 (例如,在第一个函数中,参数名称是分数,局部变量也被定义为具有相同的名称分数)C ++标准将此类代码视为格式错误。

还不清楚为什么要重新定义这些函数参数。

首先,您将函数名声明为具有int数组作为其参数

void names(int names[9]);

以下,您将其定义为具有字符串数组作为其参数。

void names(string names[9])

同样在主要内容中,名称成绩均未定义。

你的代码毫无意义。

至少我认为你必须在函数main中定义一个具有此名称的数组,而不是函数名。

答案 1 :(得分:0)

也许这样的事情会更好。 首先创建一个名为class的学生(不要与一类学生混淆!) 然后继续定义可用于访问类中私有值的函数,以及三个构造函数。

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

using namespace std;

class student
{
    private:
    std::string name_;
    int scores_[5]; // A vector here might be more useful, if you might add more scores later.
    // eg.
    // std::vector<int> scores_;

    double grade_;

    void storeGrade()
    {
         int sum = 0;
         for (int i=0; i<5; i++) // if using a vector, you could write this with iterators,
         // using scores_.begin(), scores_.end etc
         {
              sum+=scores_[i];
         }
         grade_=(sum/5); // using a counter within the loop would be better if you might have different numbers of scores
     }

     public:
     std::string getName()
     {
          return name_;
     }

     double getGrade()
     {
          return grade_;
     }

     void setScore(int score, int position)
     {
          // with vectors, I think you can use score_.push_back(score) here
          // using instead an array of ints, you need the second parameter, position
          score_[position] = score;
     }

     // Constructors, taking three different sets of parameters.

    student(std::string name)
    {
        name_=name;
    }

    student(std::string name, int score)
    {
        name_=name;
        score_[0]=score;
    }

    student(std::string name, int score[5])
    {
        name_=name;
        for (int i=0; i<5; i++) // Not sure if this is strictly necessary, but should work fine
        {
            score_[i]=score[i];
        }
    }
}

int main()
{
     student John(John, {95, 17, 20, 56, 28}) // not sure if this will pass as a score[], but if not, 
     //you can pass a real array, or use John(John); and then use John.setScore(...);
     John.storeGrade();
     double JohnsGrade = John.getGrade();
}
相关问题