how to stop loop and only loop specific info

Write your question here.
switch (option2)
{
case 1:
{
for (int i=0; i<=0; i++)
{

//to get user data and store it inside file
//ios::app it allow to store the next information of the student
//without ios::app it can only store 1 information and the next information of the student will not show on the file
ofstream record("student.txt",ios::app);

//register
//to get input from the user, student data is to link the header file
cout << "*******************************************" << endl;
cout << "\n REGISTER PAGE \n" << endl;
cout << " Student IC: "; cin >> studentRec[i].studentIC;//input for studentIC
cout << " Name: "; cin >> studentRec[i].name; //input name
cout << " Gender: "; cin >> studentRec[i].gender; //input gender
cout << " Date of Birth: "; cin >> studentRec[i].dob;//input date of birth
cout << " Country: "; cin >> studentRec[i].country; //input country
cout << " Year / semester of study: "; cin >> studentRec[i].year;//input year
cout << "\nTo go back to Main Menu enter 0 in the Student IC " << endl;
cout << "\n*******************************************" << endl;

//to put all the data inside the file
record <<studentRec[i].studentIC<<endl<<studentRec[i].name<<endl //store studentIC, name
<<studentRec[i].gender<<endl<<studentRec[i].dob<<endl //store gender, date of birth
<<studentRec[i].country<<endl<<studentRec[i].year<<endl; //store country, year

system ("pause"); //to pause the system
system("cls"); // clear system
main(); // go to main

}

}


case 2:
{
system ("cls"); // clear system
{
ifstream record2 ("student.txt"); //allow to read the file


for (int x = 0; x < 0 ; x++)

{

//file that need to be read
record2 >> studentRec[x].studentIC; //read studentIC
record2 >> studentRec[x].name; //read name
record2 >> studentRec[x].gender; //read gender
record2 >> studentRec[x].dob; //read date of birth
record2 >> studentRec[x].country; //read country
record2 >> studentRec[x].year; //read year


cout << "*******************************************\n" << endl;
cout <<"VIEW PAGE\n"<<endl;
cout << "Student IC : " <<studentRec[x].studentIC<<endl; //display studentIC
cout << "Name : " <<studentRec[x].name<<endl; //display name
cout << "Gender : " <<studentRec[x].gender<<endl; //display gender
cout << "Date of Birth : " <<studentRec[x].dob<<endl; //display date of birth
cout << "Country : " <<studentRec[x].country<<endl; // display country
cout << "Year / Semester of study : " <<studentRec[x].year<<endl;//display year

cout << "\n -To go back to main Menu enter 0" << endl;
cout << "\n*******************************************" << endl;


i want to stop the loop but cannot and it also include an empty information
PLEASE USE CODE TAGS SURROUNDING YOUR CODE: [code] [/code]

You can stop reading from the file once the file is out of information.

(ifstream >> object) returns a bool representing the success of the operation.
If it returns false, then the data retrieved is empty or not valid, so you exit the loop and don't print the information

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
ifstream record2 ("student.txt"); //allow to read the file

while ( record2 >> studentRec[x].studentIC >> studentRec[x].name >> studentRec[x].gender 
        >> studentRec[x].dob >> studentRec[x].country >> studentRec[x].year)
{
    cout << "*******************************************\n" << endl;
    cout <<"VIEW PAGE\n"<<endl;
    cout << "Student IC : " <<studentRec[x].studentIC<<endl; //display studentIC
    cout << "Name : " <<studentRec[x].name<<endl; //display name
    cout << "Gender : " <<studentRec[x].gender<<endl; //display gender
    cout << "Date of Birth : " <<studentRec[x].dob<<endl; //display date of birth
    cout << "Country : " <<studentRec[x].country<<endl; // display country
    cout << "Year / Semester of study : " <<studentRec[x].year<<endl;//display year
}
Last edited on
Topic archived. No new replies allowed.