We have project that reads a file that a user inputs and then fills an array from the inputted file and then prints out the array. I am having trouble getting this all to work with a struct, array, and for loop. It is printing two random numbers at the end and I'm not sure why. Here is the program:
The file that is inputted is here:Code:// This program takes an array from a file and allows the user to manipulate // the data in the file. #include <iostream> #include <fstream> #include <iomanip> #include <string> using namespace std; struct studentType { string Name; int ID; int score; }; int main() { int counter; ifstream inFile; string inputFile; studentType studentList[40]; cout << "Enter the name of the input file: "; cin >> inputFile; inFile.open(inputFile.c_str()); if(!inFile) { cout << "Invalid file name" << endl; system("pause"); return 1; } inFile >> studentList[0].Name >> studentList[0].ID >> studentList[0].score; for (counter = 1; inFile; counter++) { inFile >> studentList[counter].Name >> studentList[counter].ID >> studentList[counter].score; cout << studentList[counter].Name << " "; cout << studentList[counter].ID << " "; cout << studentList[counter].score << " " << endl; } inFile.close(); system("pause"); }
Code:Alex 1220 62 Betsy 1121 70 Candace 2221 83 Don 3324 90 Earl 4678 51 Francesca 5555 86 Guiles 6666 83 Henrietta 7777 73 Ignacius 8888 63 Joliet 9999 81 Kris 1010 66 Lenny 1111 76 Mark 1221 50 Noah 1331 92 Oscar 1441 73 Prince 1442 75 Queenie 7778 73 Rob 2222 83



LinkBack URL
About LinkBacks



