space problem with user input

This is a discussion on space problem with user input within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, I'm kind of new to c++ but I've made a few things. I have a problem and was ...

  1. #1
    That makes two of us
    Join Date
    Apr 2005
    Posts
    6

    space problem with user input

    Hello everyone,


    I'm kind of new to c++ but I've made a few things. I have a problem and was hoping someone could help.

    when I have a piece of code like

    PHP Code:
    cout<< "Whats the food?";
    cin>> food
    if the user types spaces the program will just shut down. like

    program: "whats the food? "
    user: "pizza and salad"

    the program would just shut off

    however
    program: "whats the food?"
    user: "pizza.and.salad"

    works fine

    any solution would be great
    thanks everyone!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    Read about
    cin.getline
    cin.ignore

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    113
    cin takes whitespace as termination.For reading whole sentence use getline
    Code:
    cin.getline(varname,length);
    Now getline will read till length-1 characters or when user press enter(whichever is earlier)

    If you want to enter a paragraph then use
    Code:
    cin.getline(varname,length,'Terminating char');
    eg
    char a[50];
    cin.getline(a,50,'$')
    Now getline will read till 49 chars or till user enters $ whichever is earlier.

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    Quote Originally Posted by vaibhav
    If you want to enter a paragraph then use
    Code:
    cin.getline(varname,length,'Terminating char');
    eg
    char a[50];
    cin.getline(a,50,'$')
    Now getline will read till 49 chars or till user enters $ whichever is earlier.
    Just to let you know it won't stop read if you type a $ it will continue to grab input until you press enter. I am not sure if you know this vaibhav, but to the OP.
    Last edited by prog-bman; 01-08-2006 at 02:06 AM.
    Woop?

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    113
    I think you don't know it prog-bman
    Prototype of getline is as under
    Code:
    getline& getline(char*,int,char='\n')
    So,if you don't provide third argument by default it will take it as newline character

    Below code proves my point
    Code:
    #include<iostream>
    #include<conio.h>
    
    using namespace std;
    int main()
    {
        char a[100];
        cin.getline(a,100,'$');
        cout<<a;
        getch();
        return(0);
    }
    A sample run of above code
    Code:
    hello
    how
    are
    you $ I
    hello
    how
    are
    you
    Here '$' is treated as terminating character and if you will cout the var you will find that char I is not accepted.

  6. #6
    That makes two of us
    Join Date
    Apr 2005
    Posts
    6
    Hey thanks guys

    I looked up cin.getline and my program works great now

    thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 04-03-2008, 09:07 PM
  2. Problem grabbing k/b input
    By falcon9 in forum C Programming
    Replies: 2
    Last Post: 10-28-2007, 11:47 AM
  3. User determined input question
    By lyoncourt in forum C Programming
    Replies: 8
    Last Post: 09-30-2007, 06:10 PM
  4. Input statement problem
    By une in forum C Programming
    Replies: 3
    Last Post: 05-29-2007, 11:16 PM
  5. Ending user input with # character
    By jowatkins in forum C++ Programming
    Replies: 2
    Last Post: 04-27-2004, 10:41 AM

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