I want the user to input any number and after that the program will tell if the number given by the user will be a positive, negative or both (0/zero). User can continue and input another number if he/she types Y/y character. My problem is that what if the user wants to stop or types N/n character? Help me please. :"(
#include <iostream>
usingnamespace std;
int main(){
int n;
char choice = 'Y' || 'y';
do {
cout<<"Enter #: ";
cin>>n;
if (n<0){
cout<<"You entered # "<<n<<"and it is a negative #. "<<endl;
}
elseif (n>0){
cout<<"You entered # "<<n<<"and it is a positive #. "<<endl;
}
else {
cout<<"You entered # "<<n<<"and it could be a positive nor negative #. "<<endl;
}
cout<<"Do you want to enter another #? (Y/N): ";
cin>>choice;
} while (choice == 'Y' || 'y');
if (choice == 'N' || 'n'){
break;
}
}
2) Your thread title mentions a "problem", and yet, strangely, there is no mention of any problem in your post. Were you at some point going to tell us what the problem is? Or are we just supposed to guess?
Remove the OR operator from your variable assignment.
But also i'm just randomly guessing at your problem, because as stated above by Mikey you didn't specify the issue.
Seriously, if you're going to put no effort at all into communicating with us about what the problem you're seeing is, how can you expect anyone else to put in any effort in helping you?
Line 7: You can't initialize a variable like that. Well, you can, but it's not going to give you the result you expect. choice is going to be initialized to 1 (true), which is the result of ('Y' OR 'y'). This is somewhat irrelevant, since you wipe out the value at line 25.
Line 27, 29: Your conditionals are bogus. C++ does not support implied left hand side in conditionals. You must fully specify the conditions.
1 2 3
while (choice == 'Y' || choice =='y');
if (choice == 'N' || choice =='n')
if (ans == 'Y' || 'y') evaluates as if (ans == ('Y' || 'y'))
('Y' || 'y') evaluates to 1 (true), before being compared to ans.
#include <iostream>
usingnamespace std;
int main(){
int n;
char choice;
do {
cout<<"Enter a number: ";
cin>>n;
if (n<0){
cout<<"You entered number "<<n<<" and it is a negative #. "<<endl;
}
elseif (n>0){
cout<<"You entered number "<<n<<" and it is a positive #. "<<endl;
}
else {
cout<<"You entered number "<<n<<" neither positive nor negative. "<<endl;
}
cout<<"Do you want to enter another number? (Y(yes)/any letter(no)): ";
cin>>choice;
} while (choice == 'Y' || choice == 'y');
}