yes or no help

This is a discussion on yes or no help within the C++ Programming forums, part of the General Programming Boards category; Ok im making my new program and i have it set to prompt the user if they want to continue(Y) ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    5

    yes or no help

    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.
    Code:
    if(Y)
         {
              }
              else
              {
                  if(N)
    
                  }
    And im using char Y; and char N; and if they choose no it still goes to yes. Anyone help?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    5
    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");
    }

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Code:
    cin >> Y, N;
    This reads in a character into Y. Nothing happens with N.

    Code:
    if (Y)
    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.
    If you want to check whether a variable equals something else, you need to explicitly compare them using ==.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    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.

    Code:
    cin >> Y;
    This will read one character, and store the result in Y. From there, you can test upon what character it is.

    Code:
    if(Y == 'Y')
    {
        /* do something here */
    }
    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.

    Instead, as your condition you have this:

    Code:
    if(Y)
    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'.).

    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.

  7. #7
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    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 )
    I'm just trying to be a better person - My Name Is Earl

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,014
    Quote Originally Posted by Matt51723 View Post
    Code:
         if(Y)
         {
              cout << "blah.\n";
              }
              else
              {
                  if(N)
                  cout << "blah.\n";
                  }
    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:
    Code:
    	if(Y)
    	{
    		cout << "blah.\n";
    	}
    	else
    	{
    		if(N)
    			cout << "blah.\n";
    	}
    That looks much better, doesn't it?
    It also lets me easier see that you can just as well do an if else:
    Code:
    	if(Y)
    	{
    		cout << "blah.\n";
    	}
    	else if (N)
    	{
    		cout << "blah.\n";
    	}
    OF course, the code is flawed, but the logic behind the indentation fixes and if/else is sound and true.

    Quote Originally Posted by swgh View Post
    int main ( void )
    Not necessary in C++...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21