C++ 如何使用cin从用户那里读取完整的一行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5455802/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:12:44  来源:igfitidea点击:

How to read a complete line from the user using cin?

c++iostream

提问by FuzionSki

Here is my current C++ code. I would like to know how to writea line of code. Would I still use cin.getline(y)or something different? I've checked, but can't find anything. When I run it, it works perfectly except it only types oneword instead of the full lines I need it to output. This is what I need help with. I've outlined it in the code.

这是我当前的 C++ 代码。我想知道如何一行代码。我还会使用cin.getline(y)或不同的东西吗?我已经检查过,但找不到任何东西。当我运行它时,它工作得很好,只是它只输入一个单词而不是我需要它输出的完整行。这就是我需要帮助的。我已经在代码中概述了它。

Thanks for helping

谢谢你的帮助

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <fstream>

using namespace std;

int main()
{
    char x;

    cout << "Would you like to write to a file?" << endl;
    cin >> x;
    if (x == 'y' || x == 'Y')
    {
        char y[3000];
        cout << "What would you like to write." << endl;
        cin >> y;
        ofstream file;
        file.open("Characters.txt");
        file << strlen(y) << " Characters." << endl;
        file << endl;
        file << y; // <-- HERE How do i write the full line instead of one word

        file.close();


        cout << "Done. \a" << endl;
    }
    else
    {
        cout << "K, Bye." << endl;
    }
}

回答by ybakos

The code cin >> y;only reads in one word, not the whole line. To get a line, use:

代码cin >> y;只读取一个单词,而不是整行。要获得一条线,请使用:

string response;
getline(cin, response);

Then responsewill contain the contents of the entire line.

然后response将包含整行的内容。

回答by hidayat

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <string>

int main()
{
    char write_to_file;
    std::cout << "Would you like to write to a file?" << std::endl;
    std::cin >> write_to_file;
    std::cin >> std::ws;
    if (write_to_file == 'y' || write_to_file == 'Y')
    {
        std::string str;
        std::cout << "What would you like to write." << std::endl;

        std::getline(std::cin, str);
        std::ofstream file;
        file.open("Characters.txt");
        file << str.size() << " Characters." << std::endl;
        file << std::endl;
        file << str;

        file.close();

        std::cout << "Done. \a" << std::endl;
    }
    else
        std::cout << "K, Bye." << std::endl;
}

回答by Yini Guo

string str;
getline(cin, str);
cin >> ws;

You can use getlinefunction to read the whole line instead of reading word by word. And The cin>>wsis there to skip white spaces. And you find some detail about it here : http://en.cppreference.com/w/cpp/io/manip/ws

您可以使用getline函数读取整行而不是逐字读取。而cin>>ws是用来跳过空格的。您可以在此处找到有关它的一些详细信息:http: //en.cppreference.com/w/cpp/io/manip/ws