我是C ++的新手,并在控制台上写了一些待办事项清单。我只能在文本文件中添加一行,但是当我尝试添加更多行时,它将不会出现在我的文本文件中。
请看一下我在做什么错
//output-file stream
ofstream file;
file.open("output.txt", std::ios_base::app); //append
bool isRunning = true;
while (isRunning) {
cout << "Please select an action:" << endl;
cout << "add - adding tasks to the list" << endl;
cout << "del - deleting tasks to the list" << endl;
cout << "list - show the list" << endl;
cout << "x - to exit program" << endl;
string input;
cin >> input;
string addedTask;
if (input == "add") {
cout << "Please enter a task you like to add: " << endl;
cin.ignore();
if (std::getline(std::cin, addedTask)) {
file << addedTask << "\n";
}
else {
cout << "Failed to read line" << endl;
}
}
为什么我只能添加一个字符串行?我仍然无法解决问题,或者我缺少什么?
尝试一下。输入“ x”终止程序。
#include <stdio.h>
#include <stdlib.h>
#include<string>
#include <cstring>
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
ofstream file;
file.open("output.txt", std::ios_base::app); //append
while (1) {
cout << "Please select an action:" << endl;
cout << "add - adding tasks to the list" << endl;
cout << "del - deleting tasks to the list" << endl;
cout << "list - show the list" << endl;
cout << "x - to exit program" << endl;
string input;
cin >> input;
string addedTask;
if (input == "add") {
cout << "Please enter a task you like to add: " << endl;
cin.ignore();
if (std::getline(std::cin, addedTask)) {
file << addedTask << "\n";
}
else {
cout << "Failed to read line" << endl;
}
}
else if (input == "x")
{
break;
}
}
file.close();
return 0;
}