Problem with While loop

I have this program due soon but im having trouble adding a "Are you done? (Y/N)" loop in it... i just dont know where exactly to put it

here's the program:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <conio.h>

using namespace std;

void getData(int &h, int &mh, int &a, int &ma, char &g, char e[], char &s);

bool testGen(char g, char fileGen);
bool testHeight(int h, int mh, int fileHeight);
bool testAge(int a, int ma, int fileAge);
bool testColor(char eye[], char fileColor[]);
bool testSmoking(char s, char smokingPref);


ifstream infile;

char fileName[12];
int fileHeight, fileAge;
char fileGen, fileColor[6], smokingPref;

int main()
{

int match = 0;
int noMatchs = 0;
int recordsRead = 0;
int height, maxh, age, mage;
char gen, eye[6], smoke;
bool matchedGen, matchedHeight, matchedAge, matchedColor, matchedSmoker;



getData(height, maxh, age, mage, gen, eye, smoke);
infile.open("Program3.dat");

while (! infile.eof())

{

++recordsRead;
match = 0;
infile >> fileName >> fileHeight >> fileAge >> fileGen >> fileColor >> smokingPref;

matchedGen = testGen(gen, fileGen);
matchedHeight = testHeight(height, maxh, fileHeight);
matchedAge = testAge(age, mage, fileAge);
matchedColor = testColor(eye, fileColor);
matchedSmoker = testSmoking(smoke, smokingPref);



if (matchedGen)
{
if (matchedHeight)
++match;

if (matchedAge)
++match;

if (matchedColor)
++match;

if (matchedSmoker)
++match;

}
else
{
match = 0;
}
if (match == 4)
{
cout << fileName << " is a PERFECT match" << endl;

}
else if (match == 3)
{
cout << fileName << " is a PARTIAL match" << endl;

}
else
{
match = 0;
++noMatchs;
}





}
cout << "There were " << noMatchs << " non-matches out of " << recordsRead
<< " records read." << endl;



getch();
return 0;
}


void getData(int &h, int &mh, int &a, int &ma, char &g, char e[], char &s )
{
cout << "Enter a minimum height in inches: ";
cin >> h;
cout << "Enter a maximum height in inches: ";
cin >> mh;
cout << "Enter a minimum age in years: ";
cin >> a;
cout << "Enter a maximum age in years: ";
cin >> ma;
cout << "Enter a gender (M/F): ";
cin >> g;
cout << "Enter eye color: ";
cin >> e;
cout << "Enter smoking preference (Y/N): ";
cin >> s;
}


// Bools

bool testGen(char g, char fileGen)
{
if (g == fileGen)
return true;
else
return false;
}

bool testHeight(int h, int mh, int fileHeight)
{

if (fileHeight >= h && fileHeight <= mh)
return true;
else
return false;
}

bool testAge(int a, int ma, int fileAge)
{
if (fileAge >= a && fileAge <= ma)
return true;
else
return false;
}

bool testColor(char eye[], char fileColor[])
{
if (strcmp(eye, fileColor) == 0)
return true;
else
return false;
}

bool testSmoking(char s, char smokingPref)
{
if (s == smokingPref)
return true;
else
return false;
}



i know i have to add it to the while loop but i dont know how to add another function right next the one there... do i have to make a seperate loop? please help!
In main, assign variables in a different set of lines, after that, encapsulate all of main (including assignments) but your variable declarations in a do-while loop (if for no other reason than it's good form to have a pretest loop for something like this). Once that's done, you tag on a little bit on the end (inside the loop) asking if the user is finished, if so, complete the loop requirement, and viola; if not, clear everything out, and start again.

I'm a little green with C++, but that's how I'd do it in other languages, and I think the principle stays the same.
Can i see an example Garady? i would greatly appreciate it =]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main()
{
int variable;
char answer;
bool done = false; //declare this outside, because if it is true, the program will end anyway

do
{
variable = 0; //each time the loop starts anew, this clears out your variables

/* your code here */

cout << "Done(y/n)?";
cin >> answer;

if(answer == 'y')
done = true;
}
while(done != true); //keeps the loop up until the user returns a 'y'
return 0;
}


I'm pretty sure that's how it would work... Personally, I'd put some sort of validation on it, to keep the user from feeding something invalid into it. On that note, your code seems to lack validation for all of it's inputs, or I'm missing something... Just a concern.
you dont actually need to declare the boolean before the loop. a do-while will always execute once, then check the condition.
closed account (z05DSL3A)
You do need to declare the Boolean, as all names (identifiers) must be declared before they are used, you could get away without initialising it but it is safer to initialise variables to a safe value when (or soon after) they are declared.

bool done; //Declaration (also definition)

bool done = false; // Declaration and initialisation
Last edited on
Topic archived. No new replies allowed.