strings

Write a C++ program tha tcreates studnet grade reports.

student Name :
Student Id Number:
__________________________________________
course code course credits Grade


I already created the file that im using, now i need to create a function that does read the file and displays the information.

I havent finished teh code but when im trying to check if my program works i get multiple errors. Can someone help me?? Thanks in advance.


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
74
75
76
77
78
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

using namespace std;
void getStudent ();	//Function Prototype, studentID, name,courseCode, courseCredits, courseGrade

int main ()
{

	cout << "This program gets student information from an existing File. " << endl;

	ifstream inFile;		//Callin input stream function

	getStudent ();			//Calling the Function




return 0;	
}

void getStudent()
{
	//This function reads students from file and prints to screen

	ifstream inFile;				//Filename
	string fileName, name, courseCode;
	int studentID, IDnum, courseCredits;
	char courseGrade;

	cout << "Please enter name of file you wish to open : ";
	getline (cin, fileName);

	//Open the file

	inFile.open(fileName.c_str());

	//Check if it opened
	
	if (!inFile)					//Check for successful Open
		cout << "Error! Failed to open file named " << fileName << endl;
	else 
	{
		cout << "Oppening successful" << endl;

		cout << "Please Enter Student ID : ";
		cin >> IDnum;
		
		inFile >> studentID;								//Find Student ID inside file


		while (IDnum = studentID)
		{
			getline(inFile, name);
			getline(inFile, courseCode);
			getline(inFile, courseCredits);
			getline(inFile, courseGrade);


			cout << "student identification Number :" << studentID << endl;
			cout << "Student's Name : " << name << endl;

			cout << "course Code  \t  Course Credits  \t  Course Grade " << endl;

			

			

		}
	}

	//Close file
	inFile.close();

}
	
What errors are they?

inFile on line 14 is not related to one on line 28 and serves no purpose.
= on line 54 is assignment and should instead be ==.
I see a bug in the while loop, which would cause a definite infinite loop assuming the condition is true.

while (IDnum = studentID)
The value of IDnum will never change, meaning it will loop again and again and again... Even if there is no more lines in the file to read.

If you want to loop through each every line.
while (!inFile.eof())

Saying, while the read position is not at the end of file, continue.
Hi guys, thanks for your suggestions.
I've been working on the code and i have changed it, now my code actualy read information from a txt file. I need my program be able to use the id number entered by the user and find it in my txt file, then i have to write a loop that displays the all the information available from that same id number.
Im not sure if i have t use arrays for that and if i do, how would i use arrays combined with strings?
Thanks for all your help

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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <iomanip>

using namespace std;
void getStudent ();	//Function Prototype, studentID, name,courseCode,
					//courseCredits, courseGrade, gpa

int main ()
{

	cout << "This program gets student information from an existing File. " << endl;

	getStudent ();								//Calling the Function




return 0;	
}

void getStudent()
{
	//This function reads students from file and prints to screen

	string fileName, name, courseCode, courseCredits;
	string studentID, courseGrade;

	ifstream inFile;

	cout << "Please enter name of file you wish to open : ";
	getline (cin, fileName);

	//Open the file

	inFile.open(fileName.c_str());				//This statement opens the file

	//Check if it opened
	
	 if (inFile.fail())							//Check for successful Open, .fail()
	 {
		cout << "Error! Failed to open file named " << fileName << endl;
	 }
	 else 
	 {
		 cout << "File " << fileName << " was successfuly opened" << endl;
		 cout << "_____________________________________________________________________" << endl;
		 cout << endl;
		 system("cls");							//Clear screen after succesful open of the file


		int idNum;
		 cout << "Enter Student Identification Number :";
		 cin >> idNum; 
		 cout << "Student ID  Number : " << idNum << endl;

		 system("cls");							//Clear screen


		 //Extracting information from file
		 getline(inFile, studentID);
		 getline(inFile, name);
		 getline(inFile, courseCode);
		 getline(inFile, courseCredits);
		 getline(inFile, courseGrade);


		
		 cout << "Student Name :" << name << endl;
		 cout << "Student ID Number : " << studentID << endl;
		 cout << endl;
		 cout << "________________________________________________________"<< endl;
		 cout << endl;
		 cout << "Course Code \t Course Credits    Course grade " << endl;
		 cout << "   " << courseCode << setw(16) << courseCredits << setw(18) << courseGrade << endl;


		
	
	
			//Close file
			inFile.close();
		

	 }
}
Topic archived. No new replies allowed.