#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
usingnamespace std;
//-----------------------------------------------------------------------------
struct studentInfo
{
string info[11][3]; // Multidimensional Array
}student;
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void readFile(ifstream& input_file)
{
string line, name, major; // Local variables
string grade;
istringstream iss; // Initialize string stream
while(getline(input_file, line)) // Obtain lines from file
{
iss.clear();
iss.str(line);
cout << line << endl;
if(iss >> name >> grade >> major) // Stringstream tokens
{
for(int i = 0; i < 11; i++) // Place variables into multi. dim. array
{
student.info[i][0] = name; // Names
student.info[i][1] = grade; // Grades
student.info[i][2] = major; // Majors
}
}
else
{
cout << "Error getting lines!" << endl;
}
}//end if
}//end void readFile
//-----------------------------------------------------------------------------
void printList(studentInfo student)
{
for(int i = 0; i < 11; i++)
{
cout << i;
cout << student.info[i][0];
cout << " ";
cout << student.info[i][1];
cout << " ";
cout << student.info[i][2] << endl;
}
}//end void printList
//-----------------------------------------------------------------------------
int main()
{
cout << "Opening file..." << endl;
ifstream input_file("data.txt", ios::in|ios::beg); // Opens input for reading
if(!input_file.is_open()) // If input file doesn't open
{
cout << "Error: Unable to open input file\n" << endl;
system("pause"); // Microsoft only pause
return(1);
}
else // File is open
{
cout << "File is open" << endl;
readFile(input_file); // Read from file
cout << "\nPrinting list..." << endl << endl;
printList(student);
}
//-----------------------------------------------------------------------------
cout << endl;
system("pause"); // Microsoft only pause
return 0; // Success
}//end main
Output:
Opening file...
File is open
Tom 99 CS
Clair 56 MAT
Sue 96 CS
Casey 88 MAT
James 45 CS
Frank 78 GS
Ben 52 ARCH
Laren 62 CS
Harry 78 IS
Sam 100 CS
Jennifer 68 MAT
Printing list...
0Jennifer 68 MAT
1Jennifer 68 MAT
2Jennifer 68 MAT
3Jennifer 68 MAT
4Jennifer 68 MAT
5Jennifer 68 MAT
6Jennifer 68 MAT
7Jennifer 68 MAT
8Jennifer 68 MAT
9Jennifer 68 MAT
10Jennifer 68 MAT
Press any key to continue . . .
Honestly I am not that experienced with most of the code you are using but my guess would be that the input variable types are not all strings, so you cannot store them all in a string array... idk... thats just my guess :/
WAIT!!! lol I see the problem... On line 36 Your program takes the first input and stores it into your array, and then it runs the while again and rewrites the whole array with the next inputed data, that is why your array contains the last line of data only
while(getline(input_file, line)) // Obtain lines from file <---------------------------------HERE
{
iss.clear();
iss.str(line);
cout << line << endl;
if(iss >> name >> grade >> major) // Stringstream tokens
{
for(int i = 0; i < 11; i++) // Place variables into multi. dim. array <------------------HERE
{
student.info[i][0] = name; // Names
student.info[i][1] = grade; // Grades
student.info[i][2] = major; // Majors
}
}
else
{
cout << "Error getting lines!" << endl;
}
}//end if
So, now the issue is to change it so that I can put each line into the multidimensional array in different rows with each variable in a different column..... hmmmm
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
usingnamespace std;
//-----------------------------------------------------------------------------
struct studentInfo
{
string info[11][3]; // Multidimensional Array
}student;
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void readFile(ifstream& input_file)
{
string line, name, major; // Local variables
string grade;
istringstream iss; // Initialize string stream
int i = 0;
while(getline(input_file, line)) // Obtain lines from file
{
iss.clear();
iss.str(line);
cout << line << endl;
if(iss >> name >> grade >> major) // Stringstream tokens
{
if(i < 11) // Place variables into multi. dim. array
{
student.info[i][0] = name; // Names
student.info[i][1] = grade; // Grades
student.info[i][2] = major; // Majors
i++;
}
}
else
{
cout << "Error getting lines!" << endl;
}
}//end if
}//end void readFile
//-----------------------------------------------------------------------------
void printList(studentInfo student)
{
for(int i=0; i < 11; i++)
{
cout << student.info[i][0];
cout << " ";
cout << student.info[i][1];
cout << " ";
cout << student.info[i][2] << endl;
}
}//end void printList
//-----------------------------------------------------------------------------
int main()
{
cout << "Opening file..." << endl;
ifstream input_file("data.txt", ios::in|ios::beg); // Opens input for reading
if(!input_file.is_open()) // If input file doesn't open
{
cout << "Error: Unable to open input file\n" << endl;
system("pause"); // Microsoft only pause
return(1);
}
else // File is open
{
cout << "File is open" << endl;
readFile(input_file); // Read from file
cout << "\nPrinting list..." << endl << endl;
printList(student);
}
//-----------------------------------------------------------------------------
cout << endl;
system("pause"); // Microsoft only pause
return 0; // Success
}//end main
Opening file...
File is open
Tom 99 CS
Clair 56 MAT
Sue 96 CS
Casey 88 MAT
James 45 CS
Frank 78 GS
Ben 52 ARCH
Laren 62 CS
Harry 78 IS
Sam 100 CS
Jennifer 68 MAT
Printing list...
Tom 99 CS
Clair 56 MAT
Sue 96 CS
Casey 88 MAT
James 45 CS
Frank 78 GS
Ben 52 ARCH
Laren 62 CS
Harry 78 IS
Sam 100 CS
Jennifer 68 MAT
Press any key to continue . . .
That's better, lol.
Now I have to:
-Return the average grade for the class and the student with the highest and
the lowest grade in your output.
-Next you will tell me the average from each distinct major in the output.