Ok im making my new program and i have it set to prompt the user if they want to continue(Y) or no(N).
Heres what I got.
And im using char Y; and char N; and if they choose no it still goes to yes. Anyone help?Code:if(Y) { } else { if(N) }
Ok im making my new program and i have it set to prompt the user if they want to continue(Y) or no(N).
Heres what I got.
And im using char Y; and char N; and if they choose no it still goes to yes. Anyone help?Code:if(Y) { } else { if(N) }
Can you post a little bit more of code, particulary the declaration and assigmnet of Y and N, and the reading or the input?
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Code:#include <iostream.h> int password; main() { int name; char Y; char N; cout << " --------------------------------\n"; cout << " Password Program!\n"; cout << " --------------------------------\n"; cout << "\n"; cout << " blah blah blah.........!\n"; cout << " A password is required to continue\n"; cout << " If the password is right you may continue to see the content\n"; cout << "\n"; cout << " -----------------------------------------\n"; do { cout << " Enter the password:"<<endl; cout << " --------------------------\n"; cin>>password; } while (password != 100); cout<< " Password Correct."<<endl; cout << " Press <Y> to enter or <N> to leave.\n"; cin >> Y, N; if(Y) { cout << "blah.\n"; } else { if(N) cout << "blah.\n"; } system("pause"); }
This reads in a character into Y. Nothing happens with N.Code:cin >> Y, N;
This checks to see whether the variable Y is not 0. (Since Y is a character, we then mean (ASCII) character number 0, not the character '0'.) If it is anything else, it evaluates as true.Code:if (Y)
If you want to check whether a variable equals something else, you need to explicitly compare them using ==.
Right, "cin >> Y,N" doesn't do even remotely what [I think] you expect it to do. N is never set a value here. Y is set to whatever you input, e.g. 'y' or 'a' or something.
Later when you do "if (Y)" you are essentially saying "if (Y != 0)", which if you didn't happen to press CTRL-@ [1], will be true.
My proposal would be to have a single char varable, (call it ch for example), and then compare that with the constant 'y' and 'n' for the respective cases.
[1] And assuming that the input functions allow this character to be passed through - which may or may not be the case.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
No, the usage of chars here is wrong. You have the wrong idea with regard to variables.
To simplify this, think of a char in C++ as space in computer memory to store any character. Memory locations are handled by an address. This could get quite messy if you had to remember memory location 1,435,742 for the place where your char is located at. So this is why we have variables. You name the variable, and the compiler/linker/runtime (depending on a bunch of things) will handle how the addressing is handled.
In your case, you have two variables named Y and N. They do not correspond to the characters 'Y' or 'N'. They are just named Y and N. They each can be used independently to store any character.
This will read one character, and store the result in Y. From there, you can test upon what character it is.Code:cin >> Y;
This is why your choice of a variable name of Y and N is not good in this context. It can get kind of confusing.Code:if(Y == 'Y') { /* do something here */ }
Instead, as your condition you have this:
This is wrong because, in C++, boolean expressions are resolved nunerically based upon the following rules: Any non-zero result in C++ is true; zero is false. Therefore your condition is still true unless Y happens to hold a character numerical value of 0 (not to be confused with a character representation of 0, which is written in C++ as '0'. We're talking more about '\0'.).Code:if(Y)
BTW, <iostream.h> should be <iostream> unless you're using an ancient compiler, in which case, you need to update. Also, you still have name as an int. I'm pretty sure you were told it should be std::string. If you do change that, you should include <string>.
Hope this helps.
Also
>#include <iostream.h> // decepricated header. use <iostreeam>
>int password; // try to always avoid global variables. Make them local
>main() // main returns an int. Use int main() or int main ( void )
Double Helix STL
You need to learn how to indent properly. Each } must be on the same level as the {, and you should NOT just include an additional tab (or a sets of spaces) where you feel like it. They should be reserved for new blocks.
And in a block, the following lines should NOT be the same level as the block element itself, as can be seen from if (N). cout << "blah.\n"; under if (N) should be indented further.
Here's how it should look like:
That looks much better, doesn't it?Code:if(Y) { cout << "blah.\n"; } else { if(N) cout << "blah.\n"; }
It also lets me easier see that you can just as well do an if else:
OF course, the code is flawed, but the logic behind the indentation fixes and if/else is sound and true.Code:if(Y) { cout << "blah.\n"; } else if (N) { cout << "blah.\n"; }
Not necessary in C++...