无法使用Fout创建和命名用户输入名称的文件
Not Able to Use Fout to Create and Name a file with a User Inputted Name
我制作了一个小程序,使用户输入文件的名称,然后是程序创建带有该名称的.doc文件的程序。然后,用户输入一些输入,并且显示在.doc文件中:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
cout << "nWhat do you want to name your file?nn";
string name = "";
char current = cin.get();
while (current != 'n')
{
name += current;
current = cin.get();
}
name += ".doc";
ofstream fout(name);
if (fout.fail())
{
cout << "nFailed!n";
}
cout << "Type something:nn";
string user_input = "";
char c = cin.get();
while (c != 'n')
{
user_input += c;
c = cin.get();
}
fout << user_input;
cout << "nnCheck your file system.nn";
}
我在创建文件的线路上收到错误:
ofstream fout(name);
我无法弄清楚问题是什么。name
是string
VAR,它是fout
对象的预期输入。
pass name.c_str(),ofStream没有一个构造函数,该构造函数符合std :: string,只有char const *,并且没有自动转换从std :: string到一个炭指针;
仅在C 11中引入了std::string
的std::ifstream
和std::ofstream
对象的能力。
如果您的编译器可以选择针对C 11标准进行编译,请打开该选项。如果这样做,您应该能够使用
ofstream fout(name);
例如,如果您使用的是g++
,则可以使用命令行选项-std=c++11
。
如果您的编译器不支持C 11标准,则需要使用
ofstream fout(name.c_str());
相关文章:
- C++如何通过用户输入删除列表元素
- 如何使用用户输入在C++中正确填充2D数组
- C++MySQL C api用户输入行
- 如何只允许用户输入正整数
- C++如何计算用户输入的数字中的偶数位数
- 通过for循环使用用户输入填充列表
- 在C++中,我如何接受不同于同一行的用户输入
- 如果用户输入无效,如何使用字符串变量-C++重复输入命令
- 显示基于用户输入的整数的字符
- 根据用户输入用字母填充矢量,并将"开始"和"结束"放在四肢
- 我试图制作一个程序,要求用户输入问题和答案,但程序循环不正确
- 打印时有二叉树问题.用户输入不打印任何内容
- C ++奇怪的数组由用户输入
- 如何允许大写和小写用户输入?
- C++读取用户输入而不按回车键(Mac OS X),与Turbo Pascal中的读取键相同
- 如何存储用户输入的所有数据,然后在他们想要查看所有数据时显示它们
- 如何使用用户输入变量制作二维数组?
- 用户输入字符串的文件附加问题..C++
- 遇到此问题时遇到困难:允许用户输入数组的值并使用 for,而循环也输出输入的最大数字
- 如何使用递归循环我的代码(当用户输入无效输入时,它会再次提示他们)?