Problem With Code

C

colo.avs96

Hello All,

I am a noob with programming and just starting to learn. My teacher
wants us to make a program that will keep asking the user if they want
to quit. Below is my code so far. My problem is the loop will repeat
with any valuse given. If anyone could help it would be greatly
appreciated. Thank you


#include <iostream>

using namespace std;

char main()
{
char ans;
do
{
cout << "Would you like to quit? (y/n)" << endl;
cin >> ans;
}
while ((ans = 'n')&&(ans = 'N'));
return 0;
}
 
K

Kai-Uwe Bux

Hello All,

I am a noob with programming and just starting to learn. My teacher
wants us to make a program that will keep asking the user if they want
to quit. Below is my code so far. My problem is the loop will repeat
with any valuse given. If anyone could help it would be greatly
appreciated. Thank you


#include <iostream>

using namespace std;

char main()
{
char ans;
do
{
cout << "Would you like to quit? (y/n)" << endl;
cin >> ans;
}
while ((ans = 'n')&&(ans = 'N'));

Note "=" vs "==".

Once this is fixed, you will find that the loop always exits regardless of
the value of ans. Fixing that is left as an exercise.
return 0;
}


Best

Kai-Uwe Bux
 
A

Alf P. Steinbach

* (e-mail address removed):
Hello All,

I am a noob with programming and just starting to learn. My teacher
wants us to make a program that will keep asking the user if they want
to quit. Below is my code so far. My problem is the loop will repeat
with any valuse given. If anyone could help it would be greatly
appreciated. Thank you


#include <iostream>

using namespace std;

char main()

This must be 'int main'.

{
char ans;
do
{
cout << "Would you like to quit? (y/n)" << endl;
cin >> ans;
}
while ((ans = 'n')&&(ans = 'N'));
return 0;
}

Well, first of all the comparision operator is '==', not '=' (which is
assignment). In the current form the condition therefore always yields 'true',
the last assignment result converted to type 'bool'.

To have comparision you'd have to write

while( ans == 'n' && and == 'N' );

Now, && means logical AND.

And it can never be the case that ans is the character 'n' AND it is also the
character 'N', because it's just one character.

And so while the original incorrect condition always yielded 'true', this
corrected condition always yields 'false'.

So what is that you must then fix?

Check your textbook about boolean operators.


Cheers & hth.,

- Alf
 
J

James Kanze

(e-mail address removed) wrote:

Just a nit, but main must return an int, not a char.
Note "=" vs "==".
Once this is fixed, you will find that the loop always exits
regardless of the value of ans. Fixing that is left as an
exercise.

Always, or never.

One sometimes wonders about the courses some of the posters are
taking. I can understand someone not immediately grasping the
implications of boolean operations and such, but if the poster
has written "char main()", he's certainly seen it somewhere.
And it would be a strange course that didn't explain the
difference between == and =, and warn about that problem.
 
K

Kai-Uwe Bux

James said:
(e-mail address removed) wrote: [snip]
{
char ans;
do
{
cout << "Would you like to quit? (y/n)" << endl;
cin >> ans;
}
while ((ans = 'n')&&(ans = 'N'));
Note "=" vs "==".
Once this is fixed, you will find that the loop always exits
regardless of the value of ans. Fixing that is left as an
exercise.

Always, or never.
[snip]

Well, it' always left as an exercise:) As for the loop, I think, it would
never exit with a repeat until construct as in Pascal.


Best

Kai-Uwe Bux
 
J

James Kanze

James said:
(e-mail address removed) wrote: [snip]
{
char ans;
do
{
cout << "Would you like to quit? (y/n)" << endl;
cin >> ans;
}
while ((ans = 'n')&&(ans = 'N'));
Note "=" vs "==".
Once this is fixed, you will find that the loop always
exits regardless of the value of ans. Fixing that is left
as an exercise.
Always, or never.

Well, it' always left as an exercise:) As for the loop, I
think, it would never exit with a repeat until construct as in
Pascal.

Yah. I got confused over what was always, and what never. the
condition is always false (once the = are changed to ==), so you
never loop (and always exit the loop).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,162
Messages
2,570,896
Members
47,434
Latest member
TobiasLoan

Latest Threads

Top