#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    7
    Rep Power
    0

    Long input include spaces and different characters


    Hi,

    I'm in middle of programming right now and wanted to know i what i made wrong here..

    my variable for the itemName is string

    Code:
    cout << "Please enter the item name: ";
    cin.ignore(100,'\n');
    getline(cin, itemName);
    please note that the program works just fine.. but i can't move to the second step.. i'm writing discount program..

    Also the program prints only the first word of the item name.. i want my program to print whatever i put in there..

    The input for the item could be anything i want. for example..

    PROTAX 2.4 inch TFT 12MP Camera

    waiting for replay..

    thanks
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,407
    Rep Power
    1871
    Try this
    Code:
    int main ( ) {
        cout << "Please enter the item name: ";
        string itemName;
        getline(cin, itemName);
        cout << itemName << endl;
    }
    And type in your input line
    PROTAX 2.4 inch TFT 12MP Camera

    Now, what was that cin.ignore line for?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo