Please Please Help

I have this lab that's got me stumped. The assignment reads
Write programs for the following and test to make sure they work. Please follow the book’s guidelines or my style guidelines to write code. Write a program that reads students’ names followed by their test scores from the given input file. The program should output to a file, output.txt, each student’s name followed by the test scores and the relevant grade.. Student data should be stored in a struct variable of type StudentType, which has four components: studentFName and studentLName of type string, testScore of type int and grade of type char. Suppose that the class has 20 students. Use an array of 20 components of type StudentType. Your program must contain at least the following functions: 1. A function to read the students’ data into an array. 2. A function to assign the relevant grade to each student. Your program should output each student’s name in this form: last name followed by a comma, followed by a space, followed by the first name; the name must be left justified. Moreover, other than declaring the variables and opening the input and output files, the function main should only be a collection of function calls.
This is my code thus far. It only indicates one person and the list has four.
the list is scores.txt and reads
Miller Andrew 98
Hooks Tony 87
Meso Alex 78
Smith Jing 88
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
  #include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
 
using namespace std;
 
struct studentType
{
    string studentFName;                            //Stores the student's first name
    string studentLName;                            //Stores the student's last name
    int testscore;                                  //stores the test score
    char grade;                                     //stores letter grade
};
 
studentType students[20];                           //initializes an array called students of the type studentType
 
void getData(studentType sList[], int listSize);
void calculateGrade(studentType sList[], int listSize);
int highestScore(const studentType sList[], int listSize);
void printResult(const studentType sList[],int listSize);
 
void StuRead(ifstream& indata, studentType students[])
{
    int x = 0;
 
    for (x = 1; x < 20; x++)
    {
        ifstream inFile;
        inFile >> students[x].studentLName            
               >> students[x].studentFName
               >> students[x].testscore;
    }
 
}
 
 
 
 
char AssignGrade(char LetterGrade, studentType students[])
{
    char grade = 0;                                     //variable to hold the students grade
 
    if(LetterGrade > 90)
        grade = 'A';
    else if (LetterGrade > 80)
        grade = 'B';
    else if (LetterGrade > 70)
        grade = 'C';
    else if (LetterGrade > 60)
        grade = 'D';
    else if (LetterGrade < 59)
        grade = 'F';
 
    return grade;
}
 
 
 
void Highest(studentType students[20])              
{
     int highTest = 0;
     int x = 0;
 
     for(x = 0; x < 20; x++)
        {
          if(x > highTest)
                highTest = x;
 
}
 
} 
int main ()
{
    ifstream inFile;
    inFile.open("scores.txt");
 
    ofstream outFile;   
    outFile.open("studentResults.txt");
 
 
    StuRead(inFile, studentType students[]);    //receive the following errors at this line: syntax error expected ')'
                                                //syntax error: missing ')' before identifier "students'
                                                //'studenType': illegal use of this type of expression
    return 0;
}
Last edited on
StuRead(inFile, studentType students[]);

Thats not how you send in an array.

https://www.youtube.com/watch?v=VnZbghMhfOY
Thats not how you send in an array.
That actually is perfectly fine. It is not the best practce as it is non-generic, but it works.
ow. Well then the problem is probably becuase you're not only sending in the array, but creating it aswell or something.

Try just doing this - StuRead(inFile, students);
Last edited on
@TarikNeaj I might misinterpret which line you were referring to in your post. If you were referring to line 82, then yes, it is wrong and solution in your second post is correct.

No errors after changing the one line to StuRead(inFile, students); but my output file is empty. Thanks TarikNeaj and MiiNiPaa
My code looks like this and I'm looking at the Youtube Video.
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
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
 
using namespace std;
 
struct studentType
{
    string studentFName;                            
    string studentLName;                            
    int testscore;                                  
    char grade;                                     
};
 
studentType students[20];                           
 
void getData(studentType sList[], int listSize);
void calculateGrade(studentType sList[], int listSize);
int highestScore(const studentType sList[], int listSize);
void printResult(const studentType sList[],int listSize);
 
void StuRead(ifstream& indata, studentType students[])
{
    int x = 0;
 
    for (x = 1; x < 20; x++)
    {
        ifstream inFile;
        inFile >> students[x].studentLName            
               >> students[x].studentFName
               >> students[x].testscore;
    }
 
}
 
 
 
 
char AssignGrade(char LetterGrade, studentType students[])
{
    char grade = 0;                                     
 
    if(LetterGrade > 90)
        grade = 'A';
    else if (LetterGrade > 80)
        grade = 'B';
    else if (LetterGrade > 70)
        grade = 'C';
    else if (LetterGrade > 60)
        grade = 'D';
    else if (LetterGrade < 59)
        grade = 'F';
 
    return grade;
}
 
 
 
void Highest(studentType students[20])              
{
     int highTest = 0;
     int x = 0;
 
     for(x = 0; x < 20; x++)
        {
          if(x > highTest)
                highTest = x;
 
}
 
} 
int main ()
{
    ifstream inFile;
    inFile.open("scores.txt");
 
    ofstream outFile;   
    outFile.open("studentResults.txt");
 
 
    StuRead(inFile, students);  
                                                
system("PAUSE");                                                
    return 0;
}
my output file is empty
You are never writing to it. So it will naturally be empty.

Also I suggest to look into your StuRead functon. If you were to compile with warnings turned on, you would see
main.cpp|23|warning: unused parameter 'indata' [-Wunused-parameter]|
Your indata parameter is not actually used inside so nothing is read from it.
I'm really very new to this. can you explain to me how to use this 'indata' parameter?
@MiiNiPaa I was actually referring to line 82 yeh :p

As far as why your output file is empty. I think its becuase you never output anything really. You use ifstream inFile;

1
2
3
4
5
6
7
for (x = 1; x < 20; x++)
    {
        ifstream inFile;
        inFile >> students[x].studentLName            
               >> students[x].studentFName
               >> students[x].testscore;
    }


But what part of your program actually outputs something?
to use this 'indata' parameter
Like any other variable.
1
2
3
4
5
6
7
8
9
void StuRead(ifstream& indata, studentType students[])
{
    for (int x = 1 0; x < 20; ++x)  {
        ifstream inFile; //Create new file stream not associated with anything
        inFile indata >> students[x].studentLName  //Read data from invalid stream, read fails and nothing is written
               >> students[x].studentFName
               >> students[x].testscore;
    }
 }
Last edited on
Topic archived. No new replies allowed.