Program will not exit loop using Do-While

As the title says, I cannot get my loop to stop when it gets to the "yes or no" to continue at the end. Everything else seems to work fine. The project is to create a program that requests an integer from the user then displays whether the integer is positive, negative, even, or odd. However, once I get to the "Would you like to continue, yes or no?" that it all goes haywire.I cannot determine where I am going so wrong with it.

#include<iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main()
{
int useNuma;
char ynAns;
bool exitA = true;


while(exitA)
{
do{
cout<<"Please enter an integer: ";
cin>>useNuma;
cout<<endl;
if (!cin)
{cout << "Invalid number!" << endl << endl;
cin.clear();
cin.ignore(256, '\n');}

else if(useNuma>0)
{cout<<useNuma<<" is a positive integer"<<endl<<endl;}
else if(useNuma<0)
{cout<<useNuma<<" is a negative integer"<<endl<<endl;}
if(useNuma%2==0)
{cout<<useNuma<<" is an even integer"<<endl<<endl;}
else
{cout<<useNuma<<" is an odd integer"<<endl<<endl;}}

while
(cout<<"Continue?(Y/N)"<<endl);
cin >> ynAns;
cin.ignore();
ynAns = tolower(ynAns);
if
(ynAns == 'n' && ynAns == 'N');
if(ynAns =='n');
cin.ignore();
{exitA = !true; }

return 0;}}


Hey, Not sure if this helps but if it does here is a code similar to the one that you have written and it works perfectly and I thought you might want to check it out and see if you can use any of the code and use it in your own coding.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
bool isEven(int);
void getVal(double& );

int main()
{
	double num;
	getVal(num);

	cout << "Val from user =" << num << endl;
	if (isEven(num))

		cout << "Number is Even\n";
	else
		cout << "Number is Odd\n";

	_getch();
	return 0;
}

bool isEven(int val)
{

	if ((val % 2) == 0)
		return true;
	else
		return false;
	return status;

}

void getVal(int& x)
{
	cout << "Enter a Number\n";
	cin >> x;
}
You forgot the cin statement.
I think you intended to write something like this:
while (cout << "Continue?(Y/N)\n" && cin >> foo && (foo == 'Y' || foo == 'y'));

Last edited on
That was it, thank you very much.

1
2
3
4
5
6
7
8
9
                   while (cout << "Continue?(Y/N)\n" && cin>>ynAns && (ynAns == 'Y' || ynAns == 'y'));
                  //  cin >> ynAns;
                   // cin.ignore();
                    ynAns = tolower(ynAns);
                    // if
                   // (ynAns == 'n' && ynAns == 'N');
                    if(ynAns =='n');
                    cin.ignore();
                    {exitA = !true; } //I do not know why this isn't working.  My patience has grown very thin. 
Well, the answer to your initial frustration is that line 9 is an assignment statement, not an "is equal" evaluation. You need to change "=" to "==".

However, nothing from line 2 through 9 is doing anything. Once you get out of the loop (from the previous post) in line 1, you are done.

Look at the tutorial page discussing control structures ( http://www.cplusplus.com/doc/tutorial/control/ )
The do - while loop has the form:

1
2
3
4
do
{
    // do some stuff here
} while ( /* check for terminating condition here ); */


Once you get past the semicolon in line 4, you are no longer in the loop. So, if you are trying to exit the loop based on exitA, then your check on exitA has to be in the while(...) check.

Last edited on
Stop putting a semi-colon ; on the end of every line. They don't automatically go on the end of every line.

On the end of this line:
while (cout << "Continue?(Y/N)\n" && cin>>ynAns && (ynAns == 'Y' || ynAns == 'y'));
the semi-colon indicates the end of the while loop. So there is nothing in this while loop. This while loop does nothing.

On the end of this line:
if(ynAns =='n');
the semi-colon indicates the end of the if block. So there is nothing in this if block. This if block does nothing.
Topic archived. No new replies allowed.