Connecting Tech Pros Worldwide Forums | Help | Site Map

storing input into arrays using a struct

Member
 
Join Date: Oct 2006
Posts: 43
#1: Apr 26 '07
Ok Im trying to use a struct to take in input from a file "grades.txt" which looks like:
Smith 9 9.33 8 10 5.5 8 10 20 47.5 47 45 47.5 48 83 87 100 98 96 100 98 92 88 96 92 86 92 94 100 96
Jones 9 8 6 6 8 7.5 8 20 35 40 42.5 40 44 62 72 100 88 86 90 92 92 88 86 88 86 94 88 86 90....etc i don't know how many students their will be.
And I need to store them in a structre like how I have in my code...Im unsure how to get all the input to go into each of these arrays . ANy help will be greatly appreciated..heres wha i have so far.
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <fstream>
  5. using namespace std;
  6.  
  7. //Structure for holding all the grades                                        
  8. struct Grades
  9. {
  10.     string student;
  11.     float quizzes[7];
  12.     float projects[6];
  13.     float exams[2];
  14.     float labs[14];
  15. };
  16.  
  17. int main()
  18. {
  19.     ifstream inFile;
  20.     inFile.open("grades.txt");
  21.     if (inFile.fail()) {
  22.         cerr << "unable to open file input.dat for reading" << endl;
  23.         exit(1);
  24.     }
  25.  
  26.  
  27. }
  28.  
the student name comes first then the quizzes (7) then the Projects (6) then exams (2) then labs (14) I dont know how I'm going to get the input to go into the right arrays...Also I don't know how I will be able to take all that input at once (defferent amounts of students and their scores and get them to go into seperate structs so that the one struct doesn't have more than 1 student in it....Im kinda lost and I need some direction any help will be great. In the future of this program im going to need to output a table like this if thats any help to you:
No. –Name-- --Quiz-- -Project- -Exam- -Lab- -Total- Grade
--- ----------- -------- ---------- ------ ----- ------- -----

1. Smith 90.55 94.44 85.00 94.86 91.41 A-
2. Jones 77.50 82.04 67.00 89.57 78.88 C+
.
.
.
--- ----------- -------- ---------- ------ ----- ------- -----
Avg: ?

Savage's Avatar
Expert
 
Join Date: Feb 2007
Posts: 1,737
#2: Apr 26 '07

re: storing input into arrays using a struct


Hi,lets see:

I think that nested loops are best solution to ur problem.


Outer while loop should loop until eof.(end of file).

Then we read in one line using fgets or any simmilar method.

Inner while loop should go trough that line until reaching specific character that u can specify or until reaching '\n' character in line

now:

if is alpha we put that char in temporary char array and later we asign it to the string in ur structure.
else read in specific amount of numbers and store it the same way to the floats and other variables in ur struct.


Savage
Member
 
Join Date: Oct 2006
Posts: 43
#3: Apr 26 '07

re: storing input into arrays using a struct


so will this work then...and then just start with some nested loops inside the while loop...
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <fstream>
  5. using namespace std;
  6.  
  7. //Structure for holding all the grades                                        
  8. struct Grades
  9. {
  10.     string student;
  11.     float quizzes[7];
  12.     float projects[6];
  13.     float exams[2];
  14.  
  15.     float labs[14];
  16. };
  17.  
  18. int main()
  19. {
  20.     string line;
  21.     ifstream myfile ("grades.txt");
  22.     if (myfile.is_open())
  23.     {
  24.         while (! myfile.eof() )
  25.         {
  26.             getline (myfile,line);
  27.         }
  28.         myfile.close();
  29.     }
  30.  
  31.     else cout << "Unable to open file";
  32.     }
  33.  
  34.  
  35. }
  36.  
Savage's Avatar
Expert
 
Join Date: Feb 2007
Posts: 1,737
#4: Apr 26 '07

re: storing input into arrays using a struct


Yes,that should do it.

Savage
Member
 
Join Date: Oct 2006
Posts: 43
#5: Apr 26 '07

re: storing input into arrays using a struct


Quote:

Originally Posted by Savage

Yes,that should do it.

Savage


how would i do an if is alpha loop for (int i = 0, line[i] == char, i++){
student = student + line[i];}

im not sure about that one ive never done it.
Savage's Avatar
Expert
 
Join Date: Feb 2007
Posts: 1,737
#6: Apr 27 '07

re: storing input into arrays using a struct


Quote:

Originally Posted by joestevens232

how would i do an if is alpha loop for (int i = 0, line[i] == char, i++){
student = student + line[i];}

im not sure about that one ive never done it.


This function is builded so that it returns 0 if the char is not a alpha.

So simple while loop like:

i=0;
while(isalpha(line[i])
{
//extract it to ur string in struct

}

should work.
Reply