How to use the getline command in c++?

前端 未结 5 1339
难免孤独
难免孤独 2021-02-03 14:22

I\'m trying to turn a cout command into a getline command in c++.

This is my code that I\'m trying to changes....

for (int count=0; count < numberOfEm         


        
5条回答
  •  梦谈多话
    2021-02-03 15:01

    istream& getline (char* s, streamsize n ); istream& getline (char* s, streamsize n, char delim ); Get line from stream

    Extracts characters from the input sequence and stores them as a c-string into the array beginning at s.

    Characters are extracted until either (n - 1) characters have been extracted or the delimiting character is found (which is delim if this parameter is specified, or '\n' otherwise). The extraction also stops if the end of file is reached in the input sequence or if an error occurs during the input operation.

    If the delimiter is found, it is extracted and discarded, i.e. it is not stored and the next input operation will begin after it. If you don't want this character to be extracted, you can use member get instead.

    The ending null character that signals the end of a c-string is automatically appended to s after the data extracted.

    The number of characters read by this function can be obtained by calling to the member function gcount.

    A global function with the same name exists in header . This global function provides a similar behavior, but with standard C++ string objects instead of c-strings: see getline (string).

    Parameters s A pointer to an array of characters where the string is stored as a c-string. n Maximum number of characters to store (including the terminating null character). This is an integer value of type streamsize. If the function stops reading because this size is reached, the failbit internal flag is set. delim The delimiting character. The operation of extracting successive characters is stopped when this character is read. This parameter is optional, if not specified the function considers '\n' (a newline character) to be the delimiting character.

    Return Value The function returns *this.

    MSDN EXAMPLE:

    // istream getline
    #include 
    using namespace std;
    
    int main () {
      char name[256], title[256];
    
      cout << "Enter your name: ";
      cin.getline (name,256);
    
      cout << "Enter your favourite movie: ";
      cin.getline (title,256);
    
      cout << name << "'s favourite movie is " << title;
    
      return 0;
    }
    

    And for your situation with structure object, u have to use the objectname.datamember to access objects data:

    for (int count=0; count < numberOfEmployees; count++)
        {
            cout << "Name: ";
           cin.getline (employees[count].name,100); 
    
            cout << "Title: ";
            cin.getline (employees[count].title,100); 
    
            cout << "SSNum: ";
            cin.getline (employees[count].SSNum); 
    
            cout << "Salary: ";
            cin.getline( employees[count].Salary);
    
            cout << "Withholding Exemptions: ";
           cin.getline (employees[count].Withholding_Exemptions);
        }
    

提交回复
热议问题