如何从用户使用cin读取完整的一行? [英] How to read a complete line from the user using cin?

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

问题描述

这是我目前的C ++代码。我想知道如何一行代码。我仍然会使用 cin.getline(y)或不同的东西?我检查过,但找不到任何东西。
当我运行它,它工作正常,除了它只是类型一个字,而不是我需要它输出的完整的行。这是我需要帮助。



感谢您的帮助

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

using namespace std;

int main()
{
char x;

cout<< 你想写一个文件吗? << endl;
cin>> X;
if(x =='y'|| x =='Y')
{
char y [3000]
cout<< 你想写什么。 << endl;
cin>> y;
ofstream file;
file.open(Characters.txt);
file<< strlen(y)< 字符。 << endl;
file<< endl;
file<< y; //< - HERE我如何写满线而不是一个字

file.close();


cout<< Done。\a<< endl;
}
else
{
cout< K,再见。 << endl;
}
}


解决方案

p> 字符串响应;
getline(cin,response);


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

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

解决方案

string response;
getline(cin, response);

这篇关于如何从用户使用cin读取完整的一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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