Thread: new line

  1. #1

    Exclamation new line


    I just started using c++ and I have a problem with an exercise that I am working on.

    What I have to do is to read in two words separated by a space inputted by the user into two variables. Also, I need to check how many words the user has entered.

    For example:

    Let's say the two string variables are word1 and word2;

    >a b

    I need word1 = a and word2 = b

    I have tried using cin << word1 << word1; and it works for two variables.

    However, the user might only input 1 word and so I need to detect it.

    My question is: Is there a way to detect the number of words the user has inputted?

    Thanks a lot!

  2. #2
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    You can use cin.getline() to load whatever the user enters into a string variable then parse the string variable to detect/count spaces. Based on the number of spaces you can determine the number of words and then input what you need into the proper variables.

  3. #3

    new line


    in my code, i have this:


    string strLine;

    cin.getLine(strLine, 10);

    but there is a big error: In function `int main(int, char **)': no matching function for call to `_IO_istream_withassign::getline (s
    tring &, int)'
    ../../../../include/g++-3/iostream.h:129: candidates are: class istream & istrea
    m::getline(char *, int, char = '\n')
    ../../../../include/g++-3/iostream.h:131: class istream & istrea
    m::getline(unsigned char *, int, char = '\n')
    ../../../../include/g++-3/iostream.h:136: class istream & istrea
    m::getline(signed char *, int, char = '\n')

    and then i tried using different numbers......but it still throwing the same error.

    Is there something wrong with my code?

  4. #4
    Registered User Ion Blade's Avatar
    Join Date
    May 2002
    Maybe you have to declare strLine like this:

    char strLine[10];
    and then it may work. I dunno if cin.getline likes the 'string' declaration...
    "Im going to have peaceful dreams of brackets and semicolons strapped on crucifixes, screaming for mercy" - Someone who doesn't like programming.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to do encryption in C
    By sankarv in forum C Programming
    Replies: 33
    Last Post: 12-28-2010, 11:01 AM
  2. Reading a file line by line
    By Raskalnikov in forum C Programming
    Replies: 8
    Last Post: 03-18-2009, 11:44 PM
  3. Pointer and Polymorphism help.
    By Skyy in forum C++ Programming
    Replies: 29
    Last Post: 12-18-2008, 09:17 PM
  4. Printing Length of Input and the Limited Input
    By dnguyen1022 in forum C Programming
    Replies: 33
    Last Post: 11-29-2008, 04:13 PM
  5. Finding carriage returns (\c) in a line
    By JizJizJiz in forum C++ Programming
    Replies: 37
    Last Post: 07-19-2006, 05:44 PM