【发布时间】:2011-07-24 06:29:58
【问题描述】:
这是我当前的 C++ 代码。我想知道如何编写一行代码。我还会使用cin.getline(y) 还是其他的?我检查过,但找不到任何东西。
当我运行它时,它工作得很好,除了它只输入 one 单词而不是我需要它输出的完整行。这是我需要帮助的。我已经在代码中对其进行了概述。
感谢您的帮助
#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;
}
}
【问题讨论】:
-
您可能想让您的标题更好地反映您的问题。另外,你应该澄清你的问题,你问的不是很清楚。
-
问题是
cin >> y只存储用户键入的行的第一个单词,提问者想知道如何将整行存储在y中,这样file << y会写完整文件的行。