flag controlled loop

input text looks like this

A John Doe
B Matt Marty

i need to have a bool flag controlled loop
if the grade is not an 'F' , it reads the name.
the process ends when the letter grade of 'F' is read.
im just not sure what my bool is or the format.

1
2
3
4
5
6
7
8
9
  const char SENTINEL_LETTER = 'F';
char grade;
string name;
   
  inFile >> grade;


  if(grade!=SENTINEL_VALUE)
   
Last edited on
do you want something like

while ( grade != 'F' )
?

Last edited on
You could set up a function to detect how many lines there are in the file then set up a for loop. like so...
1
2
3
4
5
6
7
8
 
for (a = 0; a < sizeofile/3; a++) 
{
if (grade !='f')
{
//print name
} 
} 


or if you want the program to terminate after finding the first 'f' just add a else statement like...

1
2
3
4
5
 
else 
{ 
a = size; // really lazy way to break out of loop
}
Last edited on
my output needs to look like this

-----------------------------------------------
NAME LETTER
----------------------------------------------
John Doe A
Matt Marty C


so im required to use a flag controlled loop with a constant
therefore i used const SENTINEL_LETTER.

Read the line, then associate the string that holds the line with a string stream, read the grade and the name into separate strings then output them. If the grade is a F then the while condition will be true and the loop will end and since the last grade was a F we have a if statement so it doesnt print it and you get the output you wanted..

1
2
3
4
5
6
7
string grade, name, temp;
while(getline(cin,temp) && grade != "F"){
         istringstream ss(temp);
         ss >> grade >> name;
         if(grade != "F")
         cout << name << " " << grade << endl;
         }



Anytime you write something like "F" as a string, you are creating a temporary const, in this case to compare it with grade, so no need to dclare a separate const.
Last edited on
Topic archived. No new replies allowed.