inputting from a text file

Hello,

I need some help getting my program to read from a text file. Everything seems to be compiling fine but Im getting an error as soon as the program runs because, i believe, the information is not being inputted properly. Thanks for any help!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>

using namespace std; 

struct studentInfo
{
int id;
int score;
char grade;
};
const int MAX_NUM = 50;
studentInfo data[MAX_NUM];
bool successful = false;
bool readStuData(ifstream&, bool&, studentInfo data[]);
float getAverage(studentInfo data[], float& mean);
void assignGrades(studentInfo data[], float);
void reportResults(studentInfo data[]);

int main(int argc, char *argv[], float average)
{
ifstream inData("C:\Users\Owner\Desktop\scores.txt", ios::in);
readStuData(inData, successful, data);
getAverage(data, average);
cout<<"Student "<<data[0].id<<"'s test score is "<<data[0].score<<endl;
assignGrades(data, average);
reportResults(data);
cin.ignore();
cin.get();
return 0;
}

bool readStuData(ifstream& inData, bool& successful, studentInfo data[])
{

int number;


if(inData.fail())
{
cerr << "Error: Program cannot be run." << inData << endl;
successful = false;
return successful;
return EXIT_FAILURE;
}
else
{
successful = true;
return successful;
inData >> data[0].id >> data[0].score;
number = 0;
while (!inData.eof())
{
if (number=0, number<MAX_NUM, number++)
{ 
inData >> data[number].id >> data[number].score;
number++;
}
}
}
inData.close();
return successful;
}
float getAverage(studentInfo data[], float& mean)
{
float sum = 0.0;
int i;

for (i=0; i<MAX_NUM; i++)
{
sum = data[i].score + sum;
}
mean = sum / MAX_NUM;

return (mean);
}
void assignGrades(studentInfo data[], float average)
{
int i;
for (i=0; i<MAX_NUM; i++)
{
if ((data[i].score <= average + 10) || (data[i].score >= average - 10))
{
data[i].grade = 'S';
}
if (data[i].score > average + 10)
{
data[i].grade = 'O';
}
if (data[i].score < average - 10)
{
data[i].grade = 'U';
}
}
}
void reportResults(studentInfo data[])
{
ofstream out("C:\Users\Owner\Desktop\report.txt");
out << "STUDENT SCORES"<< setw(4) <<"Student ID #"<< setw(10)<<"Score"<< setw(14)<<"Grade"<< endl;
for (int i=0;i<MAX_NUM; i++)
out << setw(8) << data[i].id << setw(14)<< data[i].score << setw(14)<< data[i].grade << endl;
}
closed account (S6k9GNh0)
Do you code like that? That's just torturous....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>

using namespace std;

struct studentInfo
{
    int id;
    int score;
    char grade;
};
const int MAX_NUM = 50;
studentInfo data[MAX_NUM];
bool successful = false;
bool readStuData(ifstream&, bool&, studentInfo data[]);
float getAverage(studentInfo data[], float& mean);
void assignGrades(studentInfo data[], float);
void reportResults(studentInfo data[]);

int main(int argc, char ** argv)
{
    ifstream inData("C:\\Users\\Owner\\Desktop\\scores.txt", ios::in);
    readStuData(inData, successful, data);
    getAverage(data, average);
    cout<<"Student "<<data[0].id<<"'s test score is "<<data[0].score<<endl;
    assignGrades(data, average);
    reportResults(data);
    cin.ignore();
    cin.get();
    return 0;
}

bool readStuData(ifstream& inData, bool& successful, studentInfo data[])
{

    int number;


    if(inData.fail())
    {
        cerr << "Error: Program cannot be run." << inData << endl;
        successful = false;
        return successful;
        return EXIT_FAILURE;
    }
    else
    {
        successful = true;
        return successful;
        inData >> data[0].id >> data[0].score;
        number = 0;
        while (!inData.eof())
        {
            if (number=0, number<MAX_NUM, number++)
            {
                inData >> data[number].id >> data[number].score;
                number++;
            }
        }
    }
    inData.close();
    return successful;
}
float getAverage(studentInfo data[], float& mean)
{
    float sum = 0.0;
    int i;

    for (i=0; i<MAX_NUM; i++)
    {
        sum = data[i].score + sum;
    }
    mean = sum / MAX_NUM;

    return (mean);
}
void assignGrades(studentInfo data[], float average)
{
    int i;
    for (i=0; i<MAX_NUM; i++)
    {
        if ((data[i].score <= average + 10) || (data[i].score >= average - 10))
        {
            data[i].grade = 'S';
        }
        if (data[i].score > average + 10)
        {
            data[i].grade = 'O';
        }
        if (data[i].score < average - 10)
        {
            data[i].grade = 'U';
        }
    }
}
void reportResults(studentInfo data[])
{
    ofstream out("C:\\Users\\Owner\\Desktop\\report.txt");
    out << "STUDENT SCORES"<< setw(4) <<"Student ID #"<< setw(10)<<"Score"<< setw(14)<<"Grade"<< endl;
    for (int i=0; i<MAX_NUM; i++)
        out << setw(8) << data[i].id << setw(14)<< data[i].score << setw(14)<< data[i].grade << endl;
}


Your very first problem is that int main has two arguments. The fact that it compiles with three arguments is complete and utter bullshit.
Topic archived. No new replies allowed.