Visual Studio 2015 C++ text file reading

Winston N

Commendable
Apr 14, 2016
4
0
1,510
0
I am creating a program to process student records, but when I choose to list all the student records, the list shown is incomplete. I have no idea what went wrong in my code. Any suggestions is highly appreciated. Thanks.

C++:
#include<iostream>
#include<iomanip>
#include<fstream>
#include<cstring>
#include<windows.h>
#include<cctype>

using namespace std;

typedef struct
{
	char name[50];
	char std_ID[7];
	char course[50];
	char phone[12];
	char address[100];
	char email[100];
	char subject1[50];
	char subject2[50];
	char subject3[50];
	char subject4[50];
	char subject5[50];
	char grade1[3];
	char grade2[3];
	char grade3[3];
	char grade4[3];
	char grade5[3];
}STUDENT_PROFILE;

void list(STUDENT_PROFILE stud[], int rows);
void update_grade(STUDENT_PROFILE stud[], int rows);
int  searchStudent(STUDENT_PROFILE stud[], int rows);
void show_user();

int main(void)
{
	ifstream inFile("studentrecords.txt");

	if (!inFile)
		cout << "Error opening input file\n";
	else
	{

		STUDENT_PROFILE students[300];
		int index = 0, choice;


		while (inFile)
		{

			if (inFile.peek() == '\n')
				inFile.ignore(256, '\n');
			inFile.getline(students[index].name, 50);
			inFile.getline(students[index].std_ID, 7);
			inFile.getline(students[index].course, 50);
			inFile.getline(students[index].phone, 12);
			inFile.getline(students[index].address, 100);
			inFile.getline(students[index].email, 100);
			inFile.getline(students[index].subject1, 50);
			inFile.getline(students[index].subject2, 50);
			inFile.getline(students[index].subject3, 50);
			inFile.getline(students[index].subject4, 50);
			inFile.getline(students[index].subject5, 50);
			inFile.getline(students[index].grade1, 3);
			inFile.getline(students[index].grade2, 3);
			inFile.getline(students[index].grade3, 3);
			inFile.getline(students[index].grade4, 3);
			inFile.getline(students[index].grade5, 3);
			// read next number
			index++;
		}
		inFile.close();
		cout << "Number of student record = " << index << endl;//check number of student
															   // menu starts
		do
		{
			cout << "1. List \n";
			cout << "3. Update student grade\n";
			cout << "4. Exit\n";
			cout << "Enter choice: ";
			cin >> choice;

			switch (choice)
			{
			case 1: list(students, index);
				break;
			case 2: show_user();
				break;
			case 3: update_grade(students, index);
				break;
			case 4: break;
			default: cout << "Invalid choice\n";
			}
		} while (choice != 4);

		ofstream outFile("studentrecords.txt");
		if (!outFile)
			cout << "Error opening output file, records are not updated.\n";
		else
		{
			for (int i = 0; i < index; i++)
			{
				outFile << students[i].name << endl;
				outFile << students[i].std_ID << endl;
				outFile << students[i].course << endl;
				outFile << students[i].phone << endl;
				outFile << students[i].address << endl;
				outFile << students[i].email << endl;
				outFile << students[i].subject1 << endl;
				outFile << students[i].subject2 << endl;
				outFile << students[i].subject3 << endl;
				outFile << students[i].subject4 << endl;
				outFile << students[i].subject5 << endl;
				outFile << students[i].grade1 << endl;
				outFile << students[i].grade2 << endl;
				outFile << students[i].grade3 << endl;
				outFile << students[i].grade4 << endl;
				outFile << students[i].grade5 << endl;
			}
			outFile.close();
		}
	}
	return 0;
}

void list(STUDENT_PROFILE stud[], int rows)
{
	system("cls");
	for (int i = 0; i < rows; i++)
	{
		cout << "Student name : " << stud[i].name << "\n" << "Student ID : " << stud[i].std_ID << "\n";
		cout << "Course : " << stud[i].course << "\n" << "Contect number : " << stud[i].phone << "\n";
		cout << "Address : " << stud[i].address << "\n" << "E- mail : " << stud[i].email << "\n";
		cout << "Subject" << setw(20) << "Grade" << endl;
		cout << "" << stud[i].subject1 << "\t" << setw(20) << "" << stud[i].grade1 << "\n"
			<< "" << stud[i].subject2 << "\t" << setw(20) << "" << stud[i].grade2 << "\n"
			<< "" << stud[i].subject3 << "\t" << setw(20) << "" << stud[i].grade3 << "\n"
			<< "" << stud[i].subject4 << "\t" << setw(20) << "" << stud[i].grade4 << "\n"
			<< "" << stud[i].subject5 << "\t" << setw(20) << "" << stud[i].grade5 << "\n"
			<< endl;
	}
	return;
}
void update_grade(STUDENT_PROFILE stud[], int rows)
{
	cout << "test update ok";
}

int  searchStudent(STUDENT_PROFILE stud[], int rows)
{
	int cc;
	cout << "test search ok";
	cc = 0;
	return 0;
}

void show_user()
{
	cout << "show user";
}
Here is my program output

Student name : Michael J. Fox
Student ID : 12022
Course : Software Engineering
Contect number : 012-4787527
Address : Amherst Street, West Bloomfield, MI
E- mail : mikej@gmail.com
Subject Grade
Basic Japanese A
Basic C++ Programming B
Database Systems Fundamenta A
Game Engine Programming A+
Statistics for Computing A

Student name : Oscar Lee
Student ID : 12044
Course : Software Engineering
Contect number : 402-555-013
Address :
E- mail :
Subject Grade


C++:
Here is my text file:
C++:
Michael J. Fox
12022
Software Engineering
012-4787527
Amherst Street, West Bloomfield, MI
mikej@gmail.com
Basic Japanese
Basic C++ Programming
Database Systems Fundamenta
Game Engine Programming
Statistics for Computing
A
B
A
A+
A
Oscar Lee
12044
Software Engineering
402-555-0139
512 Winding Way,Cranford, NJ 07016
oscarl@hotmail.com
Basic French
Basic C++ Programming
Database Systems Fundamentals
Game Engine Programming
Statistics for Computing
A
A
B
A+
A
 

Winston N

Commendable
Apr 14, 2016
4
0
1,510
0


The program debugs fine, but the problem only arises after the List option in the menu is chosen. It is supposed to list all the student records I have in the text file, however, in my program output, only 2 student records are listed. I can't seem to figure out what went wrong.
 

rgd1101

Polypheme
Moderator


I mean debug, and step thru your logic.
Give you a hint, look at the 2nd phone# on the output
 

Winston N

Commendable
Apr 14, 2016
4
0
1,510
0


Oh crap, I overlooked that. Thank you very much for your help.
 
Thread starter Similar threads Forum Replies Date
A Apps General Discussion 5
2 Apps General Discussion 1
A Apps General Discussion 1
T Apps General Discussion 1
J Apps General Discussion 4
D Apps General Discussion 2
arpitsri Apps General Discussion 3
A Apps General Discussion 10
A Apps General Discussion 5
M Apps General Discussion 4
R Apps General Discussion 4
F Apps General Discussion 1
Rainfire Apps General Discussion 3
M Apps General Discussion 4
M Apps General Discussion 2
S Apps General Discussion 4
Jaakey Apps General Discussion 2
R Apps General Discussion 3
P Apps General Discussion 2
O Apps General Discussion 1

ASK THE COMMUNITY

TRENDING THREADS