something simple??
Oct 15, 2014 at 12:55am Oct 15, 2014 at 12:55am UTC
Am I using the Multidimensional array wrong?
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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
using namespace 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 . . .
Oct 15, 2014 at 1:03am Oct 15, 2014 at 1:03am UTC
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 :/
Oct 15, 2014 at 1:05am Oct 15, 2014 at 1:05am UTC
That shouldn't matter, it is printing out the 68 now as a string in Jennifer 68 MAT. It's got to be something with my insert I think.
Oct 15, 2014 at 1:07am Oct 15, 2014 at 1:07am UTC
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
Oct 15, 2014 at 1:11am Oct 15, 2014 at 1:11am UTC
Get rid of your for loop and put int i = 0; outside of the while loop then do
if(iss >> name >> grade >> major)
{
student.info[i][0] = name; // Names
student.info[i][1] = grade; // Grades
student.info[i][2] = major; // Majors
}
else
error
and then incement i at the end of the loop
Oct 15, 2014 at 1:16am Oct 15, 2014 at 1:16am UTC
Ah I see here:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
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
Oct 15, 2014 at 1:23am Oct 15, 2014 at 1:23am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
int i = 0;
while (getline(input_file, line)) // Obtain lines from file <---------------------------------HERE
{
iss.clear();
iss.str(line);
cout << line << endl;
if (iss >> name >> grade >> major) // Stringstream token
{
student.info[i][0] = name; // Names
student.info[i][1] = grade; // Grades
student.info[i][2] = major; // Majors
}
else
{
cout << "Error getting lines!" << endl;
}
i++;
}//end if
Oct 15, 2014 at 1:26am Oct 15, 2014 at 1:26am UTC
Here we are:
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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
using namespace 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.
Topic archived. No new replies allowed.