First program using arrays

I am trying to write a program that reads from an external text file and stores the data (ss number, name and GPA) in three separate arrays. Therefore, shouldn't the ones I have listed in the program be the one identified as arrays? I then need to reverse the order and at the end give the total number of students and the average GPA (with a function). I am not trying to get an assignment done for me, I am just frustrated that I have not figured this out and thought that asking an expert might shed some light on the subject.Here is my code so far. I have no idea how to proceed on the function. I know I am off base. I am entirely self taught online. I would greatly appreciate your help. Thanks

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

int main()
{
const int MAX=100;
int ss_num[MAX];
int num_students;
string name[MAX];
float gpa[MAX], avg_gpa;
ifstream student_in;

student_in.open("student.dat");
assert(!student_in.fail());

student_in >> num_students;

for(int i=0; i<MAX; i++)
student_in >> ss_num[i];

while (!student_in.eof())
{
student_in.ignore(80, '\n');
getline(student_in, name);

for(int i=0; i<MAX; i++)
student_in >> gpa[i];

for(i=MAX-1; i>=0; i--)
cout << gpa[i] << name[i] << ss-num[i] << endl;

student_in >> ss_num;
}
student_in.close();
return 0;
}

Here are my errors:

pass8.cpp: In function `int main()':
pass8.cpp:28: invalid conversion from `void*' to `char**'
pass8.cpp:28: cannot convert `std::string*' to `size_t*' for argument `2' to `
__ssize_t getline(char**, size_t*, FILE*)'
pass8.cpp:33: name lookup of `i' changed for new ISO `for' scoping
pass8.cpp:30: using obsolete binding at `i'
pass8.cpp:34: `ss' undeclared (first use this function)
pass8.cpp:34: (Each undeclared identifier is reported only once for each
function it appears in.)
pass8.cpp:34: `num' undeclared (first use this function)
pass8.cpp:36: no match for `std::ifstream& >> int[100]' operator
/usr/include/c++/3.2.3/bits/istream.tcc:83: candidates are:
std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT,
_Traits>::operator>>(std::basic_istream<_CharT,
_Traits>&(*)(std::basic_istream<_CharT, _Traits>&)) [with _CharT = char,
_Traits = std::char_traits<char>]
1
2
for(int i=0; i<MAX; i++)
student_in >> ss_num[i];


What is the point of reading in the number of students if you are just going to loop over the entire array regardless?

What compiler do you use? If it is a modern compiler (not visual c++ 6.0) you'll have to redefine the i parameter in each for loop. Some compilers have settings that allow you to use "old" for loop scoping. That might explain one of the messages but the error message looks kind of strange to me.

I'm not looking at anything else until you edit your post, indent properly, and use code tags.
I thought that I had to read in the number of students first (make it a header in the external file. I use the g++ compiler. Very new to this, so what do you mean by edit your post, indent properly, and use code tags? Thanks in advance for any help.
Last edited on
There is a # button to the right of the edit box. Highlight your code and press that button. When you submit the edits the formatted code should look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
while (!student_in.eof())
{
   student_in.ignore(80, '\n');
   getline(student_in, name);

   for(int i=0; i<MAX; i++)
      student_in >> gpa[i];

   for(i=MAX-1; i>=0; i--)
      cout << gpa[i] << name[i] << ss-num[i] << endl;

   student_in >> ss_num;
}


Of course, you have to add the spacing so that the lines are indented within brackets. Your IDE should do that for you and then you can copy and paste that into here.
I thought that I had to read in the number of students first (make it a header in the external file.


Yes that sounds like a good idea. The problem is that once you have read this information from the file you don't ever use it again. You need it to regulate the for loops rather than using MAX which could result in trying to read information from the file that isn't there.

Can you see the syntax error in this line? Hint: the pointer notation is missing something. Are you using an ide which takes you to the line of the error when you click on it? If not, you probably should be since you are a beginner. It'll help you out a lot to have an IDE with a pleasant user interface such as visual studio express. If you are running on windows that is one that I would recommend.
 
cout << gpa[i] << name[i] << ss-num[i] << endl;
Now can you take a look please? I am lost. I do not know where to go with the function.

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
#include<iomanip>
#include<iostream>
#include<cassert>
#include<string>
#include<fstream>
using namespace std;

int main()
{
   const int MAX=100;
   int ss_num[MAX];
   int num_students;
   string name[MAX];
   float gpa[MAX], avg_gpa;
   ifstream student_in;

   student_in.open("student.dat");
   assert(!student_in.fail());

   student_in >> num_students;

   for(int i=0; i<MAX; i++)
      student_in >> ss_num[i];

   while (!student_in.eof())
   {
   student_in.ignore(80, '\n');
   getline(student_in, name);

   for(int i=0; i<MAX; i++)
      student_in >> gpa[i];

   for(i=MAX-1; i>=0; i--)
      cout << gpa[i] << name[i] << ss_num[i] << endl;

   student_in >> ss_num;
}
   student_in.close();
return 0;
}


Could you post an example of a .dat file? I'd like to see the format of the file that you are reading in.
Yes sir,

1
2
3
525-58-5478 //does this need dashes? data type int
Mark Wilson //data type string
3.54 //data type float 
Is "525-58-5478" supposed to be an expression? If it isn't, then it should be a string rather than an int shouldn't it?,
I do not believe it needs the dashes. So just 525585478. Int data type
Thanks, but that doesn't answer my question. How can you explain this line of code?
student_in >> num_students;

Or this one?
student_in.ignore(80, '\n');

Doesn't the file start with an integer stating the number of students in the file? The while loop makes no sense. First you read the number of students. Then you read 100 student ids, successively regardless of the previous number (all prior to the while loop). Then you read one student name, 100 gpas, and print all of the arrays during every single iteration of the while loop. That's inconsistent with the file contents that you posted. Within the while loop, you should be reading 3 lines. (id, name, and gpa) one time each. You shouldn't be printing the arrays until after the while loop ends.

I don't understand how this compiles. ss_num is the name of the array, not an element of the array. Regardless I don't understand why this statement is coded the way it is.
student_in >> ss_num;
Topic archived. No new replies allowed.