Thread: Question about new lines and lists

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    110

    Question about new lines and lists

    Hello all,
    So I have a question...So suppose I create a program that simply takes in my input and stores it into a list and then I output the list. Normally for my compiler (Vistual Studio), in order to output it, after typing in my input I have to press ctrl+z. So even if i typed in a sentence and pressed enter, I could still keep typing in more input on a new line until I press ctrl+z. My question is..whether I am giving the list a new line everytime I press enter?

    For example
    [code]
    1 2 3 4 <I press enter right after this so my next input goes onto a new line in the command prompt>
    5 6 7 8 <CTRL+Z>
    [code]

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    The enter key will flush the input buffer so that it can be read by your application. Ctrl-Z is not needed for this.

    To verify, run this:
    Code:
    #include <iostream>
    
    int main()
    {
        char buffer[100];
        std::cin.getline(buffer, sizeof(buffer));
        std::cout << buffer;
    }

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The behaviour you're seeing is not related to using lists.

    It is related to the specific techniques you're using to read them. For example, if you loop until an end of file is detected, the user will need to enter CTRL-Z under windows, because CTRL-Z is the end of file marker. (Different operating systems and I/O libraries involve different end of file markers though).

    In terms of getting a new list every time the user hits the enter key .... again, that depends on what techniques you're using to read data. However, as a rough rule, if you're asking this question you will not be populating a new list each time the user hits the enter key - it usually takes deliberate usage of particular techniques to achieve that with the C and C++ standard libraries so, if your code was doing that, it would be because you deliberately made it happen.

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    110
    Ok I understand now, Thanks!...so with a code like this

    Code:
    	//the containers
    	list<list<string>> sentences;
    	list<string> sentence;
    	string x;
    	
    	//adds user input to container
    	while(cin >> x){
    		sentence.push_back(x);
    		if(x == "\n"){
    			sentences.push_back(sentence);
    			sentence.erase(sentence.begin(), sentence.end());
    		}
    		}
    	sentences.push_back(sentence);
    
    ....other code...
    How would I do it so that I can add new lines to a list of lists of strings?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. counting number of lines in my output file
    By zesty in forum C Programming
    Replies: 34
    Last Post: 12-25-2007, 09:15 PM
  2. Merging two lists as one
    By Lone in forum C++ Programming
    Replies: 1
    Last Post: 03-17-2005, 03:59 PM
  3. Link Lists
    By Loki in forum C Programming
    Replies: 2
    Last Post: 06-07-2003, 09:21 PM
  4. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM
  5. Simple typedef question
    By Nutshell in forum C Programming
    Replies: 17
    Last Post: 06-27-2002, 02:56 PM