Hi, I'm hoping someone can help me figure this out. I have to write a program to average scores and print grades using structs and arrays. It's a problem for my class and I think I'm close to finishing but I'm just missing something...maybe a couple of things.

I'm getting this error right now - (33) : error C2275: 'std::ifstream' : illegal use of this type as an expression.

Any hints, tips, or suggestions would be great. Thank you!

#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>
#include <string>

using namespace std;

int openFile(ifstream &rss, string file, string grades);
void displayHeader();
void readStuData(ifstream &rss, int scores[], int id[], int &count, bool &tooMany);
float getAverage(int scores[], int count);
void assignGrade(int oneScore, float average);
void reportResults(int id[], int scores[], int count);

const int MAX_SIZE = 10; 

int main()
{
        ifstream rss;
        string file; 
        string grades;
        int scores[MAX_SIZE];
        int id[MAX_SIZE];
        int i , count = 0, oneScore = 0;
        float avg = 0, average = 0;
        bool tooMany;

       
        cout << "Enter filename: ";
        cin >> file;
        
        openFile(ifstream &rss,file,grades);
        displayHeader();
        readStuData(rss,scores,id,count,tooMany);
        getAverage(scores,count);
        assignGrade(oneScore,average);
        reportResults(id,scores,count);

        if (tooMany = true)
        {
                cout << "Average score is: " << getAverage(scores,count) << "%" << endl;
                cout << "The array is filled. Remaining scores will not be stored.";
                cout << endl;
        }
        else 
                reportResults(id,scores,count);
}
void reportResults(int id[], int scores[], int count)
{
        int i;
        int oneScore;
        float average;
        string grades;

        for (int i = 0; i < MAX_SIZE; i++)
        {       
                assignGrade(oneScore,average);
                cout << id[i] << setw(17) << scores[i] << setw(17) << grades << endl;
        }
}

void assignGrade(int oneScore, float average)
{
        string grades, file;
        ifstream rss;
        int scores[MAX_SIZE];

        rss >> scores[oneScore];
        oneScore = 0;
        while (!rss.eof() && oneScore < MAX_SIZE)
        { 
                if (oneScore > average + 10)
                {
                        grades = "OUTSTANDING"; 
                }
                else if (oneScore < average - 10)
                        {
                                grades = "UNSATISFACTORY";
                        }
                        else
                        {
                                grades = "SATISFACTORY";
                        }       
                oneScore++;
                rss >> scores[oneScore];
        }
}

float getAverage(int scores[], int count)
{
        int id[MAX_SIZE];
        int sum = 0;
        float average;
        int i;

        for(int i = 0; i < MAX_SIZE; i++)
        {
                sum += scores[i];
                count++;
        }
        average = sum / MAX_SIZE;
        return average;
}

void displayHeader()
{
        //Output header
        cout << "\nStudent ID" << setw(13) << "Scores" << setw(13) << "Grades"<< endl;
}

void readStuData(ifstream &rss, int scores[], int id[], int &count, bool &tooMany)
{
        int tempID;
        int tempScore;

        //Get input
        rss >> tempID >> tempScore;
        while(!rss.eof() && (count < MAX_SIZE))
        {
                id[count] = tempID;
                scores[count] = tempScore;
                count++;
                rss >> tempID >> tempScore;

                if (count > MAX_SIZE)
                {
                        tooMany = true;
                }
                else 
                        tooMany = false;
        }
}

int openFile(ifstream &rss, string file)
{
        rss.open(file.c_str());
        if (rss.fail())
        {
      cerr << "ERROR: Cannot open file for input." << endl;
      return EXIT_FAILURE;      
        }
}

Recommended Answers

All 3 Replies

You can't use a type like that when you're calling a function. Just pass &rss and you should be fine. You only need the ifstream there when you're declaring an ifstream object or it's in the function definition or prototype.

You can't use a type like that when you're calling a function. Just pass &rss and you should be fine. You only need the ifstream there when you're declaring an ifstream object or it's in the function definition or prototype.

Thank you. I fixed this
Help with Code Tags
C++ Syntax (Toggle Plain Text)

1.
int openFile(ifstream &rss, string file, string grades)
2.
{
3.
rss.open(file.c_str());
4.
if (rss.fail())
5.
{
6.
cerr << "ERROR: Cannot open file for input." << endl;
7.
return EXIT_FAILURE;
8.
}

int openFile(ifstream &rss, string file, string grades) { rss.open(file.c_str()); if (rss.fail()) { cerr << "ERROR: Cannot open file for input." << endl; return EXIT_FAILURE; }
and it compiles now but it exits with a failure because it says 'average' and 'oneScore' are not initialized. what am i missing?

See your reportResults function. You're declaring both of these variables in that function, but are never initializing them or assigning any values to them. You then try pass them into assignGrade, but they're filled with garbage and C++ doesn't like garbage filled variables.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.