如何允许用户为创建的文本文件命名
How to allow user to name text file that is created c++
我正在尝试创建一个用户输入名称的文本文件。它应该只是创建文本文件,然后关闭它,然后通过终端反馈是否成功创建。如果我硬编码文件名,我能够弄清楚如何创建文件,但现在我正在尝试使用用户输入,我无法获得要创建的文件。现在它甚至不让我输入任何东西,它只在我调用函数时结束。谢谢你的帮助。
编辑:我不确定是否因为我的getline调用只是得到一个空行,然后结束,或者为什么它不会让我cin任何东西。
EEDIT:这是我的完整代码,我想我认为不工作的部分是,所以希望有人能告诉我为什么我的代码只是结束后,我选择1为我的开关情况。显然我的代码不完整,但这是我所拥有的一切。
#include <iostream>
#include <fstream>
#include <stdio.h>
using namespace std;
void createDB() {
ofstream db;
string filename;
cout << "Enter the name of the database you want to create: n";
getline (cin, filename);
db.open(filename.c_str());
if(!db) { // checking if the file could be opened
cout << "nCould not create databasen";
}// add more checks to make sure file doesn't have same name
else {
cout << "nYour database " << filename << " was created successfullyn";
}
db.close();
}
void openDB() { // just opens hard coded file for now
// need to add check to see if one is already open
cout << "Enter the name of the database you want to open: ";
string line;
//ifstream file (
}
void closeDB() {
}
void display() {
}
void update() {
}
void create() {
}
void add() {
}
void del() {
}
int menu() {
cout << "Enter the number of the operation you wish to perform (1-9)n"
<< "1. Create new databasen"
<< "2. Open databasen"
<< "3. Close databasen"
<< "4. Display recordn"
<< "5. Update recordn"
<< "6. Create reportn"
<< "7. Add a recordn"
<< "8. Delete a recordn"
<< "9. Quitn";
int sel = 0;
cin >> sel;
switch (sel) {
case 1: createDB();
//menu(); // after creating file go back to list of options
break;
case 2: openDB();
break;
case 3: closeDB();
break;
case 4: display();
break;
case 5: update();
break;
case 6: create();
break;
case 7: add();
break;
case 8: del();
break;
case 9: return 0;
break;
default: cout << "Please try again and enter a valid numbernn";
menu();
break;
}
return true; // to avoid error saying control may reach end of non-void function
}
int main() {
menu();
return 0;
}
您发布的代码片段实际上有效,但由于您之前的代码,似乎getline跳过了输入。所以如果你把剩下的代码贴出来会很有帮助的。
因此,因为您在getline
之前使用cin
,您应该在cin >> sel;
之后添加std::cin.ignore(std::numeric_limits<std::streamsize>::max(), 'n');
。
所以你的菜单功能应该是
int menu() {
cout << "Enter the number of the operation you wish to perform (1-9)n"
<< "1. Create new databasen"
<< "2. Open databasen"
<< "3. Close databasen"
<< "4. Display recordn"
<< "5. Update recordn"
<< "6. Create reportn"
<< "7. Add a recordn"
<< "8. Delete a recordn"
<< "9. Quitn";
int sel = 0;
cin >> sel;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), 'n');
switch (sel) {
case 1: createDB();
//menu(); // after creating file go back to list of options
break;
case 2: openDB();
break;
case 3: closeDB();
break;
case 4: display();
break;
case 5: update();
break;
case 6: create();
break;
case 7: add();
break;
case 8: del();
break;
case 9: return 0;
break;
default: cout << "Please try again and enter a valid numbernn";
menu();
break;
}
return true; // to avoid error saying control may reach end of non-void function
}
相关文章:
- 文本文件中的单词链表
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 如何将内容数组写入文本文件?
- 无法通过空白将文本文件行分隔为矢量
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- C++将文本文件中的数据读取到结构数组中
- 在指针的帮助下,文本文件中单词的频率
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 将值从二维数组输出到文本文件
- 如何在c++中从文本文件中逐行读取整数
- 从文本文件中读取时钟时间和事件时间并进行处理
- 如何从文本文件中读取值和数组
- 如何在C++中确定文本文件中的元素是字符还是数字
- C++试图读取一个文件并输出到另一个文本文件
- 如何通过套接字将文本文件的内容从服务器发送到客户端
- 已修改的LinkedList未在文本文件本身中更新
- C++文本文件的获取线
- 为什么C++的文件 I/O 在读取文本文件时忽略初始空行?我怎样才能让它不这样做?
- 打印逐行存储的文本文件
- C++文本文件输入