使用std :: getline()读取一行? [英] Using std::getline() to read a single line?

查看:479
本文介绍了使用std :: getline()读取一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是提示用户输入消息/句子,然后使用getline()将其打印在屏幕上.以下是我尝试过的两种不同的尝试.

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.

第一次尝试:

#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;

   }
 }

第二次尝试:

#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

   }
 }

第一手尝试,代码只是打印出今天您的消息是什么?"并退出.我根本没有机会输入任何字符串.对于第二次尝试,它一直要求我输入消息.每次当我用"\ n"输入内容时,它将在屏幕上显示我输入的内容.我使用Control + c中断正在运行的进程以使其停止.

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.

编辑:为了澄清和解释,我从更长的代码中提取了第一次尝试,如下所示.

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.

推荐答案

您对此过于复杂,只需使用 std::string (它是事实上的C ++字符串),并在不使用循环的情况下调用该方法.

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;
}

输出(输入:"Hello Stack Overflow!"):

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

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


PS:正如@fredLarson所评论的那样,如果在第一个示例中将chat更改为char,它应该可以工作.但是,该代码与C有很多共同点.


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.

这篇关于使用std :: getline()读取一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆