Intro c++ Flag controlled loops

I have been working on this for about 8 hours now and cant grasp what it is or why its not working, task 3 is my big issuee the rest works,
on the age entered.
//----------------------------------------------------------------------------
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>


using namespace std;

// name course exercise
const string NAME = "Cole Williams";
const string COURSE = "CST 113";
const string EXERCISE = " Exercise 7";
const int SCREEN_WIDTH = 80;
//task 2-----
const char sentinel = '#';
// end task 2---

int main(void)
{
//task 1 --------------------------------------------------------
int filename;
int count = 0;
int startvalue = 0;
//---------------------------------------------------------------
//Input file name
ifstream in;
ifstream in2;
in2.open("ex7task4.txt");
//output file name
ofstream out;
//Calling the needed files
out.open("ex7out.txt");

// Name Exersice Course
out << setw(50) << NAME << endl;
out << setw(47) << COURSE << endl;
out << setw(48) << EXERCISE << endl;

//Divider
out << setfill('*') << setw(SCREEN_WIDTH) << " " << setfill(' ') << endl;

cout << "Select an the input file: " << endl;
cout << "1. ex7-1.txt" << endl;
cout << "2. ex7-2.txt" << endl;
cout << "3. ex7-3.txt" << endl;
cout << "Enter the file name you want to open: ";
cin >> filename;
cout << setfill('*') << setw(SCREEN_WIDTH) << " " << setfill(' ') << endl;

//task 0----------------------------------------------------------------------
switch (filename)
{
case 1:
in.open("ex7-1.txt");
break;
case 2:
in.open("ex7-2.txt");
break;
case 3:
in.open("ex7-3.txt");
break;

default:
out << "This is not a valid input file number" << endl;

}



// task 1 counting while loop---------------------------------------------------------
out << "Task 1 counting while loop" << endl;

in >> startvalue;

out <<"The start value is: " << startvalue << endl;
while (count < 100)
{


out << setw(5) << startvalue;
count ++;
startvalue += 4;
if ( count % 10 == 0)
{
out << endl;
}





}
out << setfill('*') << setw(SCREEN_WIDTH) << " " << setfill(' ') << endl;

// end task 1 -----------------------------------------------------------------------
// start task 2 ---------------------------------------------------------------------
//task 2
char ch;
int acount;
int ecount;
int icount;
int ocount;
int ucount;
//end task 2
in.ignore(100,'\n');

in.get(ch);


ch=tolower(ch);

acount = 0;
ecount = 0;
icount = 0;
ocount = 0;
ucount = 0;

while(ch != sentinel)
{

if( ch == 'a')
{
acount++;
}



if ( ch == 'e')
{
ecount++;
}


if ( ch == 'i')
{
icount++;
}



if ( ch == 'o')
{
ocount++;
}


if ( ch == 'u')
{
ucount++;
}

out << ch;
in.get(ch);
ch=tolower(ch);


}
out << endl;
out << "How many A's there are in the joke " << acount << endl;
out << "How many E's there are in the joke " << ecount << endl;
out << "How many I's there are in the joke " << icount << endl;
out << "How many O's there are in the joke " << ocount << endl;
out << "How many U's there are in the joke " << ucount << endl;
out << setfill('*') << setw(SCREEN_WIDTH) << " " << setfill(' ') << endl;
in.ignore(100,'\n');
//end task 2 code---------------------------------------------------------------------
//start task 3 code-------------------------------------------------------------------
bool done;

int points;
int totalpoints;
string names;
done = true;
totalpoints = 0;

out << "Flag controlled loop" << endl;




while(!done)
{
getline(in, names);
in >> points;
in >> names;

if(points < 0)
{
done = false;
}
else
{


out << names << endl;

in >> points;
out << points << endl;


totalpoints += points;

}
out << totalpoints;

}














return 0;
}
"done = true;" will mean that "while (! done) {..." will never execute?

(I think you perhaps have your idea of the "done" flag reversed, compared to what the while() condition tests... done should be initialised to false, then set to true when you want to exit...).
Last edited on
Topic archived. No new replies allowed.