newb question

Discussion in 'C++' started by Mitch, Jan 4, 2005.

  1. Mitch

    Mitch Guest

    when using CIN
    How do i make it so user input can be 5 or 6 letters
    and
    how do i put input into an if else statement ?
    Example, i want the user to be able to input a word then program to decide
    what word to spit out.

    char input;
    cout<<"Hi ";
    cin>>input
    if (input= = "Hello" ){
    cout<<"Hello\n";
    }
    else{
    cout<<"goodbye\n";
    }
    ?
    TIA
     
    Mitch, Jan 4, 2005
    #1
    1. Advertising

  2. Mitch wrote:
    > when using CIN
    > How do i make it so user input can be 5 or 6 letters
    > and
    > how do i put input into an if else statement ?
    > Example, i want the user to be able to input a word then program to decide
    > what word to spit out.
    >


    The IMHO best way to do this is to use the getline function defined in
    <string> and teh strings themselves, consider the following code (untested)

    #include <iostream>
    #include <string>

    using std::cin;
    using std::cout;
    using std::endl;

    using std::string;
    using std::getline;

    int main(int argc, char *argv[])
    {
    string input;

    cout << "Hi " << endl;
    getline(cin, input);
    if(input == "Hello"
    cout << "Hello" << endl;
    else
    cout << "Bye!" << endl;
    return(0);
    }

    HTH && Kind regards,
    Nicolas
     
    Nicolas Pavlidis, Jan 4, 2005
    #2
    1. Advertising

  3. Mitch

    Mitch Guest

    thank you so much
    I apreciate it..

    "Nicolas Pavlidis" <> wrote in message
    news:...
    > Mitch wrote:
    > > when using CIN
    > > How do i make it so user input can be 5 or 6 letters
    > > and
    > > how do i put input into an if else statement ?
    > > Example, i want the user to be able to input a word then program to

    decide
    > > what word to spit out.
    > >

    >
    > The IMHO best way to do this is to use the getline function defined in
    > <string> and teh strings themselves, consider the following code

    (untested)
    >
    > #include <iostream>
    > #include <string>
    >
    > using std::cin;
    > using std::cout;
    > using std::endl;
    >
    > using std::string;
    > using std::getline;
    >
    > int main(int argc, char *argv[])
    > {
    > string input;
    >
    > cout << "Hi " << endl;
    > getline(cin, input);
    > if(input == "Hello"
    > cout << "Hello" << endl;
    > else
    > cout << "Bye!" << endl;
    > return(0);
    > }
    >
    > HTH && Kind regards,
    > Nicolas
     
    Mitch, Jan 4, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. :\\\\derian

    newb question

    :\\\\derian, Oct 17, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    354
    Marina
    Oct 17, 2003
  2. xerj
    Replies:
    4
    Views:
    358
  3. =?Utf-8?B?RXJpYw==?=

    Newb textbox question

    =?Utf-8?B?RXJpYw==?=, Jan 31, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    399
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Jan 31, 2006
  4. NewB Insert Question

    , Mar 6, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    390
  5. JavaNewb
    Replies:
    4
    Views:
    1,010
    Lasse Reichstein Nielsen
    Oct 28, 2004
Loading...

Share This Page