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;}}
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.
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'));
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.
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.