Thread: Detecting blank line when string is input

  1. #1
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193

    Question Detecting blank line when string is input

    Code:
    getline(cin, g[i].fullname)
    Good evening. Getline is ignoring the input. What should I do?

    Code:
     
    #ifndef GOLF_H_
    #define GOLF_H_
    
    const int LEN = 40; 
    
    namespace sport { 
    
      struct golf { 
        
        std::string fullname; 
        int handicap;    
      };     
    
        // non-interactive version: 
        // function sets golf structure to provided name, handicap
        // using values passed as arguments to the function
        void setgolf(golf & g, const std::string name, int hc);
        // interactive version:
        // function solicits name and handicap from user
        // and sets the members of g to the values entered
        // returns 1 if name is entered, 0 if name is empty string
        int setgolf(golf & g);
        // function resets handicap to new value
        void handicap(golf & g, int hc);
        // function displays contents of golf structure
        void showgolf(const golf & g);
    }    
    
    #endif
    Code:
     
    #include <iostream> 
    #include <string>
    #include <cstring>
    #include "../Headers/golf.h"
    
    namespace sport { 
        
      void setgolf(golf &g, const std::string name, int hc)
      { 
         g.fullname = name;
         g.handicap = hc;      
      }
      
      int setgolf(golf &g)
      { 
        using std::string;
        using std::cout;
        using std::cin; 
        using std::strlen;
        
        cout << "Please enter full name: "; 
        cin >> g.fullname;
        cout << "Please enter handicap: ";
        cin >> g.handicap;  
        if(g.fullname.length() == 0)
         return 0; 
        else 
         return 1;    
      }
      
      void handicap(golf &g, int hc) 
      { 
        g.handicap = hc;  
      }     
      
      void showgolf(const golf &g)
      { 
         using std::cout; 
         using std::endl; 
         
         cout << "Your name: " <<  g.fullname << endl;   
         cout << "Handicap: " << g.handicap << endl; 
      }                           
    }
    Code:
     
    #include <iostream> 
    #include <string>
    #include <cstring>
    #include "../Headers/golf.h"
    
    int main() 
    { 
       using std::string;
       using std::cout; 
       using std::cin; 
       using std::strlen;
       using sport::golf; 
       
       int players; 
       
       cout << "How many players are playing? "; 
       while(!(cin >> players))
       { 
          cin.clear(); 
          while(cin.get() != '\n') 
           continue; 
          cout << "Please enter how many players are playing: ";
       }
       golf *g = new golf[players];
       for(int i = 0; i < players; i++) 
       { 
          cout << "Enter your full name: "; 
          getline(cin, g[i].fullname);
          if(g[i].fullname.empty())
            break; 
       }           
       delete [] g;             
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    A good stopgap for now is to follow up cin extractions with ignore(). So it would look something like this:
    Code:
    cin >> anything;
    cin.ignore();
    This isn't perfect. For example if there is nothing to ignore, this will cause the computer to read more input interactively, and ignore part of it.

    As you get better with C++ you should be learning to use std::getline all the time and convert the text you read.

  3. #3
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193
    Edit: well, I tried to fix it

    Code:
     
    int main() 
    { 
       using std::cout; 
       using std::cin; 
       using std::strlen;
       using sport::golf; 
       
       int players; 
       
       cout << "How many players are playing? "; 
       while(!(cin >> players).get())
       { 
          cin.clear(); 
          while(cin.get() != '\n') 
           continue; 
          cout << "Please enter how many players are playing: ";
       }
       golf *g = new golf[players];
       for(int i = 0; i < players; i++) 
       { 
          cout << "Enter your full name: "; 
          if(cin.get() == '\n') 
           break; 
          getline(cin, g[i].fullname); 
       }           
       delete [] g;             
    }
    My apologies, I didn't explain what I intend to do. I would like to break the for loop when the user input a blank line for his full name (as a string).
    Last edited by thames; 12-20-2012 at 07:16 AM.

  4. #4
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193
    edit: fixed

    I knew the responsible for letting the buffer keep being filled was the '\n' then I wondered if I could do this:

    Code:
     
     golf *g = new golf[players];
       cin.get();
       for(int i = 0; i < players; i++)
    and bang! it worked.



    but now I can't enter again the number of players if it's necessary. The code snippet below became ineffective.

    Code:
     
      cin.clear();
      while(cin.get() != '\n') 
        continue; 
      cout << "Please enter how many players are playing: ";
    for that reason, I reduced the code:

    Code:
     
    #include <iostream> 
    #include <string>
    #include <cstring>
    #include "../Headers/golf.h"
    
    int main() 
    { 
       using std::cout; 
       using std::cin; 
       using std::strlen;
       using sport::golf; 
       
       int players; 
       
       cout << "How many players are playing? "; 
       (cin >> players).get();
       golf *g = new golf[players];
       for(int i = 0; i < players; i++) 
       { 
          cout << "Enter your full name: "; 
          if(cin.get() == '\n') 
           break; 
          getline(cin, g[i].fullname);
       }           
       delete [] g;             
    }
    Last edited by thames; 12-20-2012 at 07:27 AM.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    When I said to follow up cin extractions with ignore(), I meant all of them in the whole program. You use cin >> stuff in more places than just in main() after all. You really did not have to go through such drastic changes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Terminating input upon encountering a blank line
    By thames in forum C++ Programming
    Replies: 7
    Last Post: 12-21-2012, 12:00 PM
  2. Search for Blank Line
    By uconnhuskies in forum C Programming
    Replies: 9
    Last Post: 07-21-2011, 08:09 PM
  3. multiline string, endl, \n, blank line space
    By jackson6612 in forum C++ Programming
    Replies: 9
    Last Post: 04-20-2011, 08:50 AM
  4. Reading a file containing a blank line
    By maniac123 in forum C++ Programming
    Replies: 8
    Last Post: 01-27-2011, 11:42 AM
  5. testing for blank line
    By rippascal in forum C++ Programming
    Replies: 3
    Last Post: 03-20-2002, 09:50 PM

Tags for this Thread