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.