char

This is a discussion on char within the C++ Programming forums, part of the General Programming Boards category; hey. could any of you tell me how to write like a simple question program like ask a question and ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    5

    char

    hey. could any of you tell me how to write like a simple question program like ask a question and answer yes, get one response and answer no and get another. I know it will have to look someting like

    char yesno;
    cout<<"this is a question?"<<endl;
    cin>>yes;
    if (yesno==yes)
    cout<<"this is an answer"<<endl;
    if (yesno==no)
    cout<<"this is a different answer"

    thanks a lot,
    max

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    The char datatype holds a single character. What you want is a string. Add #include <string> and use the C++ string class as your type for yesno. Then, compare yesno to a string literal in double quotes. For example, if (yesno == "yes"). Read up on strings in general, string literals, and the C++ string class.

  3. #3
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Use code tags... also:

    Code:
    char yesno;
    cout<<"this is a question?"<<endl;
    cin>>yes; 
    /* ERROR should be */
    cin>>yesno; 
    /* Because that is the variable you are checking for what it equals...*/
    if (yesno==yes)/* Also I think you were looking for if(yesno =="yes")
    cout<<"this is an answer"<<endl;
    if (yesno==no)/* Same here I think you were looking for if(yesno =="no")
    cout<<"this is a different answer"

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
       string answer; // creates a string called answer
       cout << "Answer the question yes/no *CASE SENSITIVE*" << endl;
       cout << "Did you understand the directions? ";
       cin >> answer; // this gets the first and only first word of the line and places it into the string
       if ( answer == "yes" )
       {
          cout << "You did understand? " << answer << endl;
       }
       else
       {
          cout << "You didn't understand? " << answer << endl;
       }
    }
    there is an example of how that code would look. That code will compile as well if you would like to use it as a base

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    Don't forget to #include <string>.

  6. #6
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    If you want to make it user-friendly...

    - Just check for the first letter 'Y' or 'N'.

    - An upper case or lower case letter should work.

    - Set up a default condition, so that the user can just hit ENTER. The default should be either the most common choice or the safe choice. For example, if the question is "Delete File?", if the user enters anything other than a word starting with 'y' or 'Y' the program should behave as if the user has entered "No".

    We have a bunch of text-based test programs at work. They all work that way. They only check for the first letter (upper or lower case) for the non-default condition. If the user enters anything else, program-flow takes the default path. I won't provide a code sample, because they are written in BASIC.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    >> Set up a default condition, so that the user can just hit ENTER.
    While this can be done, it is not easy with the normal iostream operations used by beginners because those skip whitespace automatically and will just sit there waiting for actual input.

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    I agree with you Doug but I was just making the code in the OP's post correct enough so it worked
    EDIT: Daved, I have never had to include <string> before, so I never do
    and also cant you just make a console message loop, and just wait for WM_COMMAND? and one case be the enter key?

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    Some compilers allow some code to work without #include <string>, but others don't, and you should never rely on it if yours does.

    There are even easier ways than a console message command (like cin.get() or getline()), but my point was just that simple beginner code shouldn't really try to deal with that special case because it takes a different approach.

  10. #10
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    I will keep that in mind, and also I get that you are saying keep it simple for beginners

  11. #11
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Quote Originally Posted by Raigne
    EDIT: Daved, I have never had to include <string> before, so I never do
    On some compilers, <iostream> itself includes <string>, but you can't guarantee that. For example on VC++ including <iostream> will define the string class but not all the associated operators like operator+(std::string, std::string).

    Everything that uses strings should include <string> to be sure.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  3. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  4. Passing structures... I can't get it right.
    By j0hnb in forum C Programming
    Replies: 6
    Last Post: 01-26-2003, 10:55 AM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 12:18 AM

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