Using std::getline() to read a single line?

♀尐吖头ヾ 提交于 2020-02-05 04:24:45

问题


My goal is to prompt user to enter a message / sentence and then print it out on the screen, using getline(). The following is two different attempts I have tried out.

First Attempt:

#include <iostream>
#include <iomanip>
#include <cstring>
using namespace std;
int main(){

    chat message[80];
    cout << "\n what is your message today?" << endl;

    cin.getline( message, 80); // Enter a line with a max of 79 characters.
  if( strlen( message) > 0)  // If string length is longer than 0.
    {

      for( int i=0; message[i] != '\0'; ++i)
          cout << message[i] << ' ';
      cout << endl;

   }
 }

Second Attempt:

#include <iostream>
#include <iomanip>
#include <cstring>
using namespace std;
int main(){

    string a = "a string";
    cout << "\n what is your message today?" << endl;
    while(getline(cin,a))
        cout << a;
    cout<<endl

   }
 }

For the fist attempt, the code simply print out "what is your message today?" and quit. I do not have a chance to enter any string at all. For the second attempt, it keeps asking me enter the message. Each time, when I enter something with the "\n", it would display what I entered on the screen. I use control + c to interrupt the running process to make it stop.

EDIT: To clarify and explain on my side, I extract the first attempt from a longer code, which is as the following.

#include <iostream>
#include <iomanip>
#include <cstring>
using namespace std;

char header[] = "\n *** C Strings ***\n\n";  // define a c string 
int main()
{
  char hello[30] = "Hello ", name[20], message[80];  // define a c string hello, declare two other c strings name and message
  string a="fivelength";
  cout << header << "Your first name: ";
  cin >> setw(20) >> name;      // Enter a word.


  strcat( hello, name);      // Append the name.
  cout << hello << endl;
  cin.sync();                // No previous input.
  cout << "\nWhat is the message for today?"
       << endl;

  cin.getline( message, 80); // Enter a line with a max of 79 characters.
  if( strlen( message) > 0)  // If string length is longer than 0.
    {

      for( int i=0; message[i] != '\0'; ++i)
          cout << message[i] << ' ';
      cout << endl;

   }
return 0;
}

For the above code, it does not give me a chance to enter a message on the screen. I will put it as another question.


回答1:


You are overcomplicating this, you can simply use std::string, which is the de-facto C++ string, and call the method, without using a loop.

You don't need a loop, since you are not going to repeatedly read lines, but only want to read one line, so no loop is needed.

#include <iostream>
#include <string> // not cstring, which is the C string library

using namespace std;

int main(void)
{
    string message; // it can be an empty string, no need to initialize it
    cout << "What is your message today?" << endl;
    getline(cin, message);
    cout << message;
    cout<<endl;

    return 0;
}

Output (Input: "Hello Stack Overflow!"):

What is your message today?
Message: Hello Stack Overflow!

PS: As @fredLarson commented, if you change chat to char in your first example, it should work. However, that code has a lot of commonalities with C.



来源:https://stackoverflow.com/questions/58649067/using-stdgetline-to-read-a-single-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!