Thread: Return key as whitespace?

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    12

    Return key as whitespace?

    In the program I am working on one of the first things I need it to do is to read whitespace and the return key all into the same string. For instance, if I were to type(the dots represent spaces because I couldnt get the spaces to show properly by themselves):
    ...Happy
    ......Valentines day,
    It would read the 3 spaces before happy, the enter key as a white space, the six spaces before Valentines day. So it would then come out like this.
    "...Happy.......Valentines day" yes I did forget the comma on purpose, that is another part of the assignment in which I am supposed to seperate each string with a comma but I will not bother with that here.

    As far as my code goes:

    Code:
    #include "std_lib_facilities.h" //a header made by Dr. Stroustrup for my class
    
    int main()  {
       string string1;
       cout << "Please enter a string: ";
       getline(cin, string1);
       cout << string1;
       keep_window_open();
    }
    All it does now is accepts a string and reads all the whitespace and words you can put on one line. All I want it to do is to be able to hit enter and continue reading whitespace and words.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    When do you want to stop reading whitespace and words?

    Anyway, if you want a statement or group of statements to be repeated, then you need to enclose those statements in a loop structure. You have three to choose from: while, do-while, and for. Which one you choose depends on how long you want to loop for (see above).

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    12
    I want it to stop reading into the current and start reading into the next when a comma is typed. I know about the whole loop thing, I am just trying to get the return key as whitepspace thing to work first. Something like this
    ...Happy
    ......Valentines day,
    ...Have fun
    ......all night,
    will be:
    ...Happy.......Valentines day
    ...Have fun.......all night

    This is only to show what my seperator key (in this case ',') is supposed to do, its not part of my problem. I am just trying to get the first half of the above to print.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    [s]Well, fgets() leaves the \n in the buffer, I suppose you could use that.[/s] (Sorry, I thought there was a version of fgets that took a std::string. I was wrong. And apparently I don't have strikethrough.)

    But why wouldn't getline(cin, string, ',') work? You'd still have to manually change the \n into a space, there's no way to do that in the input command.
    Last edited by tabstop; 02-10-2008 at 01:53 PM.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    12
    It does work how you described it. I never thought that by addressing one problem would I figure out the other. In all the stuffs I saw on getline() I never saw anything about a 3 argument function.

    Thanks, now Ill figure out how to make the '\n' into a space.

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    when you include <string> the string class has its own getline function. which is where the 3 arguments come into play. getline's third argument is also defaulted to '\n'

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Alegro closes out on me
    By campsoup1988 in forum C++ Programming
    Replies: 8
    Last Post: 04-03-2006, 10:40 AM
  3. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 10:00 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 05:20 PM