Stupid C++ program...

idNut

Diamond Member
Jun 9, 2002
3,219
0
0
What is wrong with this program? I want it to loop until the user enters Yy for yes and Nn for no. I can't figure it out.

int Ask()
{
char cAnswer = 0;
int iCount = 0; //Counts which number cell user is on
while (cAnswer!='y'||cAnswer!='Y') //Run while loop until n or N
{
cout<<"Do you wish to enter a number? (Y/N): ";
cin>>cAnswer;
if (cAnswer=='y'||cAnswer=='Y')
{
iCount++;
Number(iCount);
}
if (cAnswer=='n'||cAnswer=='N')
{
return 1;
}
else
{
cout<<"Please only enter y or n "<<endl;
}
}
}


I'm just talking about the while loop. Any help is appreciated. I'm actually a pretty programmer but the post don't seem to show tabs.
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
Shouldn't this line:

while (cAnswer!='y'||cAnswer!='Y')

be

while (cAnswer != 'y' && cAnswer != 'Y')
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
What exactly does Number(iCount) do?

Anyways, I fixed what I thought could be a problem. Fixed code is below. Is that what you wanted?

int Ask()
{
char cAnswer = 0;
int iCount = 0;

while (cAnswer!='y' && cAnswer!='Y')
{
cout << "Do you wish to enter a number? (Y/N): ";
cin >> cAnswer;

if (cAnswer=='y'||cAnswer=='Y')
{
iCount++;
Number(iCount);
}
else if (cAnswer=='n'||cAnswer=='N')
{
return 1;
}
else
{
cout<<"Please only enter y or n "<<endl;
}
}// end while

return 0;
}
 

idNut

Diamond Member
Jun 9, 2002
3,219
0
0
Nope, still didn't work. When they enter Y it should execute the function Number which passes iCount. iCount counts how many times that function was executed so that the array in the Number function can be increased. Just this Yy and Nn is pissing me off.
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
Originally posted by: idNut
Nope, still didn't work. When they enter Y it should execute the function Number which passes iCount. iCount counts how many times that function was executed so that the array in the Number function can be increased. Just this Yy and Nn is pissing me off.

Remember that iCount is a local variable that is destroyed when the function returns. You can make it static (by declaring the variable as: static int iCount = 0). The static keyword will preserve the value between function calls.
 

idNut

Diamond Member
Jun 9, 2002
3,219
0
0
Well that's not the issue now man. If you have VC, run this or make something small to demonstrate it. It's really strange.
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
Originally posted by: idNut
Well that's not the issue now man. If you have VC, run this or make something small to demonstrate it. It's really strange.

I did. Below is the sample program and further below is the output.

Code:
#include <iostream.h>

void Number(int count)
{
cout << "Called " << count << " Times\n";
}


int Ask()
{
static
int iCount = 0;
char cAnswer = 0;

while (cAnswer!='y' && cAnswer!='Y')
{
cout << "Do you wish to enter a number? (Y/N): ";
cin >> cAnswer;

if(cAnswer=='y'||cAnswer=='Y')
{
iCount++;
Number(iCount);
}
else if (cAnswer=='n'||cAnswer=='N')
{
return 1;
}
else
{
cout<<"Please only enter y or n "<<endl;
}
}// end while

return 0;
}


void main(void)
{
while(!Ask());
}

Output
Do you wish to enter a number? (Y/N): y
Called 1 Times
Do you wish to enter a number? (Y/N): Y
Called 2 Times
Do you wish to enter a number? (Y/N): y
Called 3 Times
Do you wish to enter a number? (Y/N): Y
Called 4 Times
Do you wish to enter a number? (Y/N): Y
Called 5 Times
Do you wish to enter a number? (Y/N): N

What is the problem?
 

idNut

Diamond Member
Jun 9, 2002
3,219
0
0
Here's the whole program, put this into a workspace and try it out.

#include <iostream.h>

int Ask();
void Number(int);

int main()
{
Ask();
return 0;
}

int Ask()
{
char cAnswer = 0;
static int iCount = 0; //Counts which number cell user is on
while (cAnswer!='y'&&cAnswer!='Y') //Run while loop until n or N
{
cout<<"Do you wish to enter a number? (Y/N): ";
cin>>cAnswer;
if (cAnswer=='y'||cAnswer=='Y')
{
iCount++;
Number(iCount);
}
if (cAnswer=='n'||cAnswer=='N')
{
return 1;
}
else
{
cout<<"Please only enter y or n "<<endl;
}
}
return 0;
}

void Number(int iCounter)
{
int iCell[101];
iCounter = 0;
while(iCell[iCounter]>10&&iCell[iCounter]<100)
{
cout<<"A whole number from 10 to 100 are the only valid "<<endl;
cout<<"set of numbers. Please enter in number "<<iCounter<<" : ";
cin>>iCell[iCounter];
if (iCell[iCounter]<10||iCell[iCounter]>100)
{
cout<<"A number from 10 to 100 please. "<<endl;
}
}
Ask();
}
 

mcveigh

Diamond Member
Dec 20, 2000
6,457
6
81
while (cAnswer!='y'&&cAnswer!='Y') //Run while loop until n or N


shouldn't that be
while (cAnswer=='y' || cAnswer=='Y')
 

idNut

Diamond Member
Jun 9, 2002
3,219
0
0
Alright, someone just write me a while loop that asks the user to enter yY or nN to enter a number. Display an error for anything but yY or nN. This is driving me nuts.
 

GiLtY

Golden Member
Sep 10, 2000
1,487
1
0
In your number function, you will always set iCounter to zero, did you do it on purpose?
 

idNut

Diamond Member
Jun 9, 2002
3,219
0
0
I got it. It was a while loop in another function that was being skipped and constantly running the Ask function. Now I need to figure out this agorithm which will take a while. I'll make a new post if I need help on that one.
 

GiLtY

Golden Member
Sep 10, 2000
1,487
1
0
I'd be glad to help you (though I might not be a big of a help), but in order to help you I have to understand the purpose of the program and the algorithm you are using to construct the program. PM me for more detail or gimme your AIM/ICQ and we'll talk about it :)
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
Originally posted by: idNut
I got it. It was a while loop in another function that was being skipped and constantly running the Ask function. Now I need to figure out this agorithm which will take a while. I'll make a new post if I need help on that one.

LOL you've got mutual recursion going on there :D