new line

This is a discussion on new line within the C++ Programming forums, part of the General Programming Boards category; Hello! I just started using c++ and I have a problem with an exercise that I am working on. What ...

  1. #1
    Unregistered
    Guest

    Exclamation new line

    Hello!

    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
    Posts
    1,065
    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
    Unregistered
    Guest

    new line

    Thanks,

    in my code, i have this:

    .....

    string strLine;

    cin.getLine(strLine, 10);

    but there is a big error:
    main.cc: In function `int main(int, char **)':
    main.cc:38: no matching function for call to `_IO_istream_withassign::getline (s
    tring &, int)'
    /.software/arch/gcc-2.95.2/distribution/lib/gcc-lib/sparc-sun-solaris2.8/2.95.2/
    ../../../../include/g++-3/iostream.h:129: candidates are: class istream & istrea
    m::getline(char *, int, char = '\n')
    /.software/arch/gcc-2.95.2/distribution/lib/gcc-lib/sparc-sun-solaris2.8/2.95.2/
    ../../../../include/g++-3/iostream.h:131: class istream & istrea
    m::getline(unsigned char *, int, char = '\n')
    /.software/arch/gcc-2.95.2/distribution/lib/gcc-lib/sparc-sun-solaris2.8/2.95.2/
    ../../../../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
    Posts
    35
    Maybe you have to declare strLine like this:

    Code:
    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, 10: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, 08:17 PM
  4. Printing Length of Input and the Limited Input
    By dnguyen1022 in forum C Programming
    Replies: 33
    Last Post: 11-29-2008, 03: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

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