Thread: New line help

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    11

    New line help

    I'm working on an update function for my program. Here's how the function works out-

    The user enters in a record they want to update, each record contains 3 entries. The user is presented with the first entry in that record. If they want to change the entry, they type it in and press enter. If they do not want to change the entry, they simply press enter. They will then be presented the 2nd entry, where they have the same options; either enter in the new information and press enter, or simply press enter and it will skip to the 3rd entry and leave the 2nd one intact.

    I'm looking for a way to detect if just the new line character was entered in and nothing else. To do this I had a string named "buffer" that was initialized to empty. At each of the 3 instances to input, I did a getline( cin , buffer ). Then I checked if( buffer != "" ), then execute what I need to (or in other words update the information).

    Here's a simple sample of what I'm trying to do-
    Code:
    string str1 = "", str2 = "", str3 = "";
    string buffer = "";
    
    cout << "Enter a new str1 value: ";
    getline( cin , buffer );
    if( buffer != "" )
    {
        //update
        buffer = "";
    }
    
    cout << "Enter a new str2 value: ";
    getline( cin , buffer );
    if( buffer != "" )
    {
        //update
        buffer = "";
    }
    
    cout << "Enter a new str3 value: ";
    getline( cin , buffer );
    if( buffer != "" )
    {
        //update
        buffer = "";
    }
    Again this code doesn't seem to work for me; it is just a simple example. Any help would be appreciated, thanks.

  2. #2
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    How about using the string class member function? I.e.

    Code:
    if (!buffer.empty()) {
        // update your field
    }
    I dont think operator!=() is defined for the string class, so maybe your buffer != "" is comparing the address of buffer with the address of the literal "", which are not the same.
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    11
    Ok, I threw that in. Now it seems to be skipping something, in the wrong way.

    After I enter in what record to update, it should display the first entry in that record to update. However, it is displaying the 1st entry, then the 2nd entry, then allowing input from the user. I have to type in two entries and press enter in order for it to move on. When it moves on it displays the 3rd entry but simply skips over it entirely, not allowing me to input anything for it.

    So the output looks like (in relation to the program above), bold is user input:

    Code:
    Enter a new str1 value: Enter a new str2 value:  <enter>
    <enter>
    Enter a new str3 value:
    Then it will kick out of the function without allowing me to enter anything for that 3rd entry. So it looks like my buffer is getting filled with something.. Either way the entire function is still not updating anything, no matter what I enter.
    Last edited by Mr Moe; 02-22-2005 at 05:30 PM.

  4. #4
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    Maybe related to the fact that getline(...) will leave the newline character on the stream. So the next time you call getline(...) it finds the "\n" and returns right away. Try adding cin.ignore() after each call to getline(...).
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by Mr Moe
    I'm looking for a way to detect if just the new line character was entered in and nothing else. To do this I had a string named "buffer" that was initialized to empty. At each of the 3 instances to input, I did a getline( cin , buffer ). Then I checked if( buffer != "" ), then execute what I need to (or in other words update the information).

    Again this code doesn't seem to work for me; it is just a simple example. Any help would be appreciated, thanks.
    If your code implements what you are describing, it should work.

    Here is a complete example. Tell me if it doesn't work for you. Also tell what compiler/operating system you are using. It should report empty strings as well as anything else you enter on any given line. (It should keep asking for strings until you tell it to quit.)

    Code:
    #include <iostream>
    #include <string>
    
    using std::cin;
    using std::cout;
    using std::endl;
    using std::flush;
    using std::string;
    
    int main()
    {
      string str1, str2, str3;
    
      while (1) {
        cout << "Enter str1: " << flush;
        getline(cin, str1);
        if (str1 == "") {
          cout << "You entered a newline (only) for str1" << endl;
        }
        else {
          cout << endl << "You entered str1 <" << str1 << ">" << endl << endl;
        }
        if (str1 == "quit") {
          break;
        }
    
        cout << "Enter str2: " << flush;
        getline(cin, str2);
        if (str2 == "") {
          cout << "You entered a newline (only) for str2" << endl;
        }
        else {
          cout << endl << "You entered str2 <" << str2 << ">" << endl << endl;
        }
        if (str2 == "quit") {
          break;
        }
    
        cout << "Enter str3: " << flush;
        getline(cin, str3);
        if (str3 == "") {
          cout << "You entered a newline (only) for str3" << endl;
        }
        else {
          cout << endl << "You entered str3 <" << str3 << ">" << endl << endl;
        }
        if (str3 == "quit") {
          break;
        }
      }
    
      return 0;
    }
    [edit] getline() works wonderfully well by itself. Comparing strings with "" works wonderfully well. The problems that people usually have is when they use cin>> and getline() alternately. If you have some cin>> integer or cin>> character stuff between the calls to getline(), then you will have to empty the input buffer.
    [/edit]

    Regards,

    Dave
    Last edited by Dave Evans; 02-22-2005 at 06:18 PM.

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