trouble with getline, fin, 2d array, for loop

Hi all, first time poster here. My problem is either my output file is repeating the same line for every array or the array is not incrementing correctly with each line.

[input file]

Nancy Peterson
A00591342
510 Tumble Dr, San Gabriel, TX 57981
(666)759-2249
492-35-5984
CS1428
CS2308
CS3377

[input file]

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
  #include <iostream>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;

int main()
{
    //declare constant sizes for arrays
    const int stdntrow = 3;
    const int stdntcolumn = 9;

    
    //place size contants into arrays
    string nonNumeric [stdntrow][stdntcolumn];
   
    int num_students;
    string line;
    //validate number of students
    cout << "Enter the number of students: ";
    cin >> num_students;
    
    while(num_students < 3 || num_students > 3)
    {
        cout << "Error. The number should be exactly 3. Enter the number again";
        cin >> num_students;
        cout << endl;
    }
    
    //open files
    ifstream fin;
    fin.open("Project4_A04408358_inputfile.txt");
    ofstream fout;
    fout.open("Project4_A04408358_outputfile.txt");
    
    if(!fin)
    {
        cout << "Error - file not found" << endl;
        return 1;
    }
    //header
    fout << "Student Grade Sheet, Department of Computer Science, Texas State University" << endl << endl;
    
    //outerloop for students
    for(int row = 0; row < stdntrow; row++)
    {
        for(int col = 0; col < stdntcolumn; col++)
        {
            //read information from inputfile for student names, addresses, etc.
                getline(fin, nonNumeric[row][col], '\n');
          
        }
    }
    
    for(int i=0; i< stdntrow; i++)
    {
        for(int j = 0; j < stdntcolumn; j++)
        {
            fout << setw(36) << "Name of Student:\t" << nonNumeric[i][j] <<endl;
            fout << setw(36) << "Student ID:\t" << nonNumeric[i][j] << endl;
            fout << setw(36) << "Address of the Student:\t" << nonNumeric[i][j] << endl;
            fout << setw(36) << "Telephone Number:\t" << nonNumeric[i][j] << endl;
            fout << setw(36) << "Student Soc. Security #:\t" << nonNumeric[i][j] << endl;
            
        }
    }
    
    fin.close();
    return 0;
}

        


[output file]

Student Grade Sheet, Department of Computer Science, Texas State University

Name of Student: Nancy Peterson
Student ID: Nancy Peterson
Address of the Student: Nancy Peterson
Telephone Number: Nancy Peterson
Student Soc. Security #: Nancy Peterson
Name of Student: A00591342
Student ID: A00591342
Address of the Student: A00591342
Telephone Number: A00591342
Student Soc. Security #: A00591342
Name of Student: 510 Tumble Dr, San Gabriel, TX 57981
Student ID: 510 Tumble Dr, San Gabriel, TX 57981
Address of the Student: 510 Tumble Dr, San Gabriel, TX 57981
Telephone Number: 510 Tumble Dr, San Gabriel, TX 57981
Student Soc. Security #: 510 Tumble Dr, San Gabriel, TX 57981

etc.


[output file]

I know I'm completely overlooking something and I've spent the past hour and a half looking for answers around the web and nothing I've tried has worked. Help me internet, you're my only hope.

 
while(num_students < 3 || num_students > 3)

Why not like this:
 
while (num_students != 3)


Perhaps you can try reading all the different data in different arrays.
You will get an array containing all the names, one array containing all the addresses, one for the ID's, etc.

Hope this helps you out!
Thanks! Unfortunately our professor is requiring us to use a single array to store all the data :/
You cannot use a loop on line 58. Instead use the index directly:
1
2
3
4
5
6
7
8
9
10
    for(int i=0; i< stdntrow; i++)
    {
        for(int j = 0; j < stdntcolumn; j++)
        {
            fout << setw(36) << "Name of Student:\t" << nonNumeric[i][0] <<endl; // Note: 0 for name
            fout << setw(36) << "Student ID:\t" << nonNumeric[i][1] << endl; // Note: 1 for id
// etc.
...            
        }
    }
@CalvinProg

A few small problems. One, your input file is only for one student, so the rest of the array will be blank, as there was nothing else to read.
Two, you only 8 lines of data in the input file, but you try to read 9. Three, when you send data to the output file, each of the lines are the same, as you don't increase the array line to be saved. Try this way instead.
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
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;

int main()
{
	//declare constant sizes for arrays
	const int stdntrow = 3;
	const int stdntcolumn = 8; // Only has 8 data statements in the file


	//place size contants into arrays
	string nonNumeric [stdntrow][stdntcolumn];

	int num_students;
	string line;
	//validate number of students
	cout << "Enter the number of students: ";
	cin >> num_students;

	while(num_students != 3)
	{
		cout << "Error. The number should be exactly 3. Enter the number again";
		cin >> num_students;
		cout << endl;
	}

	//open files
	ifstream fin;
	fin.open("Project4_A04408358_inputfile.txt");
	ofstream fout;
	fout.open("Project4_A04408358_outputfile.txt");

	if(!fin)
	{
		cout << "Error - file not found" << endl;
		return 1;
	}
	//header
	fout << "Student Grade Sheet, Department of Computer Science, Texas State University" << endl << endl;

	//outerloop for students
	for(int row = 0; row < stdntrow; row++)
	{
		for(int col = 0; col < stdntcolumn; col++)
		{
			//read information from inputfile for student names, addresses, etc.
			getline(fin, nonNumeric[row][col]);
			cout << "Read as " << nonNumeric[row][col] << endl;
            // Above line just to show what was read in. It can be removed.
		}
	}
	int j;
	for(int i=0; i< stdntrow; i++)
	{
		j=0;    
		fout << setw(36) << "Name of Student:\t" << nonNumeric[i][j] <<endl;
		fout << setw(36) << "Student ID:\t" << nonNumeric[i][++j] << endl;
		fout << setw(36) << "Address of the Student:\t" << nonNumeric[i][++j] << endl;
		fout << setw(36) << "Telephone Number:\t" << nonNumeric[i][++j] << endl;
		fout << setw(36) << "Student Soc. Security #:\t" << nonNumeric[i][++j] << endl;
	}
	fin.close();
	return 0;
}


The input file..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Nancy Peterson
A00591342
510 Tumble Dr, San Gabriel, TX 57981
(666)759-2249
492-35-5984
CS1428
CS2308
CS3377
Alice Peterson
A00591342
510 Tumble Dr, San Gabriel, TX 57981
(666)759-2249
492-35-8459
CS1428
CS2308
CS3377
Jack Peterson
A00591342
510 Tumble Dr, San Gabriel, TX 57981
(666)759-2249
492-35-5849
CS1428
CS2308
CS3377
Last edited on
1
2
3
4
5
6
7
8
9
        cout << "Enter the number of students: ";
	cin >> num_students;

	while(num_students != 3)
	{
		cout << "Error. The number should be exactly 3. Enter the number again";
		cin >> num_students;
		cout << endl;
	}


Since num_students is not used anywhere but here there is really no need for any of this code.


1
2
3
	//open files
	ifstream fin;
	fin.open("Project4_A04408358_inputfile.txt");

You really should start getting used to using constructors whenever possible instead of calling ancillary functions to do the job of the constructor.

1
2
	//open files
	ifstream fin("Project4_A04408358_inputfile.txt");

And you really don't need to call the close method in this program, let the destructor do it's job.

Thanks! Unfortunately our professor is requiring us to use a single array to store all the data :/

Have you studied structures or classes yet? This program is screaming for the use of a UDT for holding the information for each "student".
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
struct Student
{
   std::string name;
   std::string id;
   std::string address;  
   std::string telephone;
   std::string other_number;
   std::vector<std::string> classes; // Or use an array if you can't use a vector.
};

...

// In main().
   std::vector<Student> students; // Or use an array if you can't use a vector.


Last edited on
Topic archived. No new replies allowed.