Help please asap..

Hi everyone, i have an assignment about 2d array, i should read marks from a file with strings but whenever i run the program it show out only the first line of marks and the others are garabage,

1
2
3
4
5
6
7
8
  int const classSize=8;
  int const N=3;
 
  for(int i=0; i<classSize; i++)
  {
     for(int j=0; j<N; j++)
        inFile>>marks[i][j];
  }
Last edited on
would you mind supplying the file as well.
#include<iostream>
#include<string>
#include<fstream>

using namespace std;

const int classMax=20;
const int N=3;
int input(int[][N], string[], float&);
int calculateGrades(int, int[][N], int[], char[], int[]);

int main()
{
string names[classMax];
int marks[classMax][N], gradesFreq[5]={0};
char grades[classMax];
int total[classMax];
int maxindex, classSize;
float avg;

classSize=input(marks, names, avg);
cout<<"The average mark of all students is: "<<avg<<endl<<endl;
//maxindex=claculateGrades(classSize, marks, total, grades, gradedFreq);

//output(classSize, names, marks, total, grades, gradesFreq);

//cout<<"\nThe student that has the largest total is"<<names[maxindex]<<" with total equal to "<<total[maxindex]<<endl;

return 0;
}

int input(int marks[][N], string names[], float& avg)
{
ifstream inFile;
int classSize, midterm[classMax], work[classMax], Final[classMax];
int sum=0;

inFile.open("marks.txt");

inFile>>classSize;

for(int i=0; i<classSize; i++)
{
inFile>>midterm[i]>>work[i]>>Final[i];
getline(inFile, names[i]);
}

for(int i=0 ; i<classSize; i++)
{
sum+=midterm[i]+work[i]+Final[i];
}
avg=(sum/(classSize*N));

for(int i=0; i<classSize; i++)
{
for (int j=0; j<N; j++)
{
inFile>>marks[i][j];
}
}
inFile.close();
return classSize;
}
Last edited on
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
#include<iostream>
#include<string>
#include<fstream>

using namespace std;

const int classMax=20;
const int N=3;
int input(int[][N], string[], float&);
int calculateGrades(int, int[][N], int[], char[], int[]);

int main()
{
    string names[classMax];
    int marks[classMax][N], gradesFreq[5]= {0};
    char grades[classMax];
    int total[classMax];
    int maxindex, classSize;
    float avg;

    classSize=input(marks, names, avg);
    cout<<"The average mark of all students is: "<<avg<<endl<<endl;
    //maxindex=claculateGrades(classSize, marks, total, grades, gradedFreq);

    //output(classSize, names, marks, total, grades, gradesFreq);

    //cout<<"\nThe student that has the largest total is"<<names[maxindex]<<" with total equal to "<<total[maxindex]<<endl;

    return 0;
}

int input(int marks[][N], string names[], float& avg)
{
    ifstream inFile;
    int classSize, midterm[classMax], work[classMax], Final[classMax];
    int sum=0;

    inFile.open("marks.txt");

    inFile>>classSize;

    for(int i=0; i<classSize; i++)
    {
        inFile>>midterm[i]>>work[i]>>Final[i];
        getline(inFile, names[i]);
    }

    for(int i=0 ; i<classSize; i++)
    {
        sum+=midterm[i]+work[i]+Final[i];
    }
    avg=(sum/(classSize*N));

    for(int i=0; i<classSize; i++)
    {
        for (int j=0; j<N; j++)
        {
            inFile>>marks[i][j];
        }
    }
    inFile.close();
    return classSize;
}



Reposting - to add formatting.

Ok now all of your code is here, what is it that you need help with?

Edit: Also where is the text file you are trying to read?
Last edited on
The file:
8
26 21 41 john adam
28 24 40 sara henry
18 15 38 micheal adler
23 22 35 tyler harry
27 23 43 jerry lendley
12 19 17 emily slash
15 21 33 mike freddie
19 20 42 kylie m
Last edited on
My problem is whenever i want to store the marks into the 2d array it only reads the first line of marks and doesnt read the others..
Last edited on
Ok this makes more sense now. You see in your file...
1
2
3
4
5
6
7
8
9
8
26 21 41 john adam
28 24 40 sara henry
18 15 38 micheal adler
23 22 35 tyler harry
27 23 43 jerry lendley
12 19 17 emily slash
15 21 33 mike freddie
19 20 42 kylie m


You need to add logic to your program to deal with the first line.

let me take another look at everything now.
Last edited on
So I am not sure how to fix your code specifically, but try running this to see if you can see how to fix your error.

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
/*
 http://www.cplusplus.com/doc/tutorial/files/
 */

#include <iostream>
#include <fstream>
#include <string>


using namespace std;

int main(int argc, const char * argv[])
{


    ifstream inFile;
    inFile.open("marks.txt", ifstream::in);
    if (!inFile) {
        cerr << "Error: file could not be opened" << endl;
        return -1;
    }

    char c;
    while (inFile.get(c)){  // loop getting single characters
        std::cout << c;
        // add code here
 }
    if (inFile.eof())                      // check for EOF
        std::cout << "[EoF reached]\n";
    else
        std::cout << "[error reading]\n";

    inFile.close();                        // close file



    return 0;
}
Last edited on
You need to put all the for loops in a while loop.
Otherwise you're only reading the first line.

So starting at line 40.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
infile >> classSize;
while (infile) // starts reading until end of file, might have to change this to (infile < classSize)
{
    for(int i=0; i<classSize; i++)
    {
        inFile>>midterm[i]>>work[i]>>Final[i];
        getline(inFile, names[i]);
    }

    for(int i=0 ; i<classSize; i++)
    {
        sum+=midterm[i]+work[i]+Final[i];
    }
    avg=(sum/(classSize*N));

    for(int i=0; i<classSize; i++)
    {
        for (int j=0; j<N; j++)
        {
            inFile>>marks[i][j];
        }
infile >> midterm; // not sure about this but you need to enter another read in statement for the while loop to continue
    }
Last edited on
Thanks to all of you, still didnt find the right answer so i'll just submit it as it's..
Last edited on
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
#include <iostream>
#include <string>
#include <fstream>

int main()
{
    //////////////////////////////////////////////////////////////
    // create the test file (this is just for testing)
    {
        std::ofstream( "marks.txt" ) <<
                                "8\n"
                                "26 21 41 john adam\n"
                                "28 24 40 sara henry\n"
                                "18 15 38 micheal adler\n"
                                "23 22 35 tyler harry\n"
                                "27 23 43 jerry lendley\n"
                                "12 19 17 emily slash\n"
                                "15 21 33 mike freddie\n"
                                "19 20 42 kylie m\n" ;
    }
    //////////////////////////////////////////////////////////////

    const int classMax = 20 ;
    const int N = 3 ;

    std::ifstream file( "marks.txt" ) ; // open the file for input
    int n_lines ;
    file >> n_lines ; // read the number of lines'

    if( n_lines > 0 && n_lines <= classMax )
    {
        int marks[classMax][N] = { {0} } ;
        std::string names[classMax] ;

        // read marks, name from eaxh line
        for( int i = 0 ; i < n_lines ; ++i )
        {
            // read the marks
            for( int j = 0 ; j < N ; ++j ) file >> marks[i][j] ;

            // read the rest of the line (ignoring leading whitespaces) as the name
            // skip leading whitespace: http://www.cplusplus.com/reference/istream/ws/
            // http://www.cplusplus.com/reference/string/string/getline/
            std::getline( file >> std::ws, names[i] ) ;
        }

        // verify that we have read everything correctly.
        for( int i = 0 ; i < n_lines ; ++i )
        {
            std::cout << i+1 << ". " << "name: '" << names[i] << "'  marks: " ;
            for( int j = 0 ; j < N ; ++j )std::cout << marks[i][j] << ' ' ;
            std::cout << '\n' ;
        }

        // TODO: process the data. find average etc.
    }
}

http://coliru.stacked-crooked.com/a/c0d6a8a27781a122
Topic archived. No new replies allowed.