program not looping

not sure if i got something wrong. program is ending after first input and when i enter 'Y' for another record for the text file. am i not suppose to use text file to build array later or not sure how to setup array to serial search the data on the file.


#include <iostream>
#include <fstream>
#include <string>
using namespace std;
ifstream fin;
ofstream fout;

double studentid, students, gpa, row, maxsize;
char reply, match, morerecords, moresearch;
string firstname, lastname;


void build();
void setup();

void sucessful();
void unsucessful();

void output();
int main()
{
system("cls");
cout << " Student Academic Standing " << endl;
cout << "Is this the correct program? (Y/N) ";
cin >> reply;
reply = toupper(reply);
while (reply != 'Y' && reply != 'N')
{
cout << " Error:" << endl;
cout << " Please Enter Y or N";
cin >> reply;
reply = toupper(reply);
}
if (reply == 'Y')
{
cout << " Does a file need to be built? " << endl;
cin >> reply;
reply = toupper(reply);
while (reply != 'Y' && reply != 'N')
{
cout << " Error:" << endl;
cout << " Please Enter Y or N";
cin >> reply;
reply = toupper(reply);
}
if (reply == 'Y')
{

build();


}

}

return 0;


}
void build()
{

fout.open("E:studentidarray.txt");
while (morerecords != 'Y')
{

cout << " Please Enter your Student ID ";
cin >> studentid;
cout << " Please Enter your Last Name ";
cin >> lastname;
cout << " Please Enter your First Name ";
cin >> firstname;
cout << " Please Enter Your GPA ";
cin >> gpa;
while (gpa <= 0 && gpa >= 4.0)
{
cout << " Error ";
cout << " Please enter a gpa between 0 - 4.0 ";
cin >> gpa;

}

fout << studentid << " " << lastname << " " << firstname << " " << gpa << " ";
cout << " Another Record? ";
cin >> morerecords;
morerecords = toupper(morerecords);
while (morerecords != 'Y' && morerecords != 'N')
{
cout << " Error ";
cout << " Please enter Y or N as a response ";
cin >> morerecords;
morerecords = toupper(morerecords);
}



}
fout.close();
}
What while loop are you specificaly having problems with? Also, next time it helps if you put the code in between the CODE labels. Which you can find to the right of this message. It makes it difficult to understand the loops if its in it current format. And adding some indentation would help. But just glancin over it as is, Im guessing when you ask at the end of the while loop for the next reply, you use morerecords. You should ask if they would like to enter another response but put it into the same variable you used at the top of the while loop, which is reply. I maybe mistaken because it was hard to follow without indents or documentation.

1
2
3
4
5
6
7
8
9
10
11
12
cout << "Enter reply.";
cin >> reply;

while (reply == "Y")
{
          //do whatever it is you want to do in the while loop


          cout << Wuuld you like to reply again";
          cin >> reply;
}
 
Last edited on
sorry about that. to be honest im just confused. im trying to build a program to build a file with student id, last name , first name, and gpa. and make an array to be serial searched. but i cant get the program to let me enter a second record when it prompts another record it ends reguarless Y or N
and i feel like maybe im doing what im trying to achieve completly wrong for some reason
The problem is in this condition. while (morerecords != 'Y') If I want another record and enter 'Y' the loop terminates.

Also the correct path would be fout.open ("E:\\studentidarray.txt");
ok so how do i make the data in the text file an array ?
Topic archived. No new replies allowed.