在编译我的程序之前,我不确定如何解决我的两个错误

时间:2014-10-11 07:23:37

标签: c++

我已经被困在这个砖墙上几个小时了,我不知道如何处理我的代码来修复这些错误。我试图通过文本文件从给定的家庭作业得分中获得一个班级平均分。我能够让程序读取文本文件,但我无法获得计算平均值的程序。

以下是错误。

error: no matching function for call to 'readFile'
   readFile(showAverage);
   ^~~~~~~~

pa05.cpp:42:6: note: candidate function not viable: no known conversion from 'int (int, int, int)' to 'ifstream &' (aka 'basic_ifstream<char> &') for 1st argument
void readFile(ifstream &someFile)

到目前为止,这是我的代码:

档案:pa05.cpp

// @author Avery Baumann
// @version October 9 2014
// Programming Assignment 05
// This program calculates students' average
// homework scores and their grades and displays it in a nicely
// formatted table. The data is read from a file in order to display it.

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;

void readFile(ifstream&);       //Function prototype
int showAverage (int homeworkOne, int homeworkTwo, int homeworkThree);
int homeworkAverage;
int homeworkOne;
int homeworkTwo;
int homeworkThree;

int main()
{
    ifstream dataIn;

    dataIn.open("/home/shared/cs135/pa05data.txt");
    if (dataIn.fail())
        cout << "Error opening data file.\n";
    else
    {   readFile(dataIn);
        dataIn.close();
    }
    showAverage (homeworkOne, homeworkTwo, homeworkThree);
    return 0;
}

/*******************************************************
*                    readFile                          *
* This function reads and displays the contents of the *
* input file whose file stream object is passed to it. *
********************************************************/

void readFile(ifstream &someFile)
{
    int homeworkOne;
    int homeworkTwo;
    int homeworkThree;
    string student;

    while (someFile)
    {   someFile >> student >> homeworkOne >> homeworkTwo >> homeworkThree;
        cout << student << "  "
             << homeworkOne << "  "
             << homeworkTwo << "  "
             << homeworkThree << "  "
   }
}

/*******************************************************
*                      showAverage                     *
* This function calculates the average of the three    *
* homework scores for each student                     *
********************************************************/

int showAverage(int homeworkOne, int homeworkTwo, int homeworkThree)
{
   int hwAverage;
   readFile(showAverage);

   hwAverage = (homeworkOne + homeworkTwo + homeworkThree) / 2.0;
      cout << hwAverage << "  "
           << endl;

}

2 个答案:

答案 0 :(得分:0)

问题是你使用的函数void readFile(ifstream&);采用了fstream的对象,所以部分

{
    int hwAverage;   
    readFile(showAverage);

    hwAverage = (homeworkOne + homeworkTwo + homeworkThree) / 2.0;

    cout << hwAverage << "  "
         << endl;
}

查看函数readFile您要为其提供哪些参数。函数showAverage中的readFile(showAverage);是什么 您必须将ifstream对象传递给该函数。

答案 1 :(得分:0)

这是函数原型:

void readFile(ifstream&);

该函数接受ifstream类型的单个参数。你这样称呼它:

readFile(showAverage);

由于showAverage是函数而不是ifstream,因此代码无效。

您的readFile函数读取文件并将值输出到标准输出。但是当readFile返回时,读取的值将丢失。您无法再访问它们。

因此,您希望保留这些值。您应该将读取的值放入结构中并从readFile返回。这样,您就可以在readFile之外使用值。然后,您可以将该结构传递给showAverage