我无法找到为什么它不允许我在函数中使用getline
但它让我使用cin
。
这是代码:
#include <iostream>
#include <fstream>
void Read(),Write();
std::string line;
int main()
{
std::string choise;
std::cout << "Would you like to write or read the Journal?" <<
std::endl;
std::cin >> choise;
switch (choise[0]){
case ('w'):{
Write();
break;
}
case ('r'):{
Read();
break;
}
default :{
std::cout << "Something went wrong" << std::endl;
}
}
return 0;
}
void Read()
{
std::ifstream myfile("/home/turry/Documents/CPP/Journal/test.txt"); // Hardcoded address
std::cout << "\x1B[2J\x1B[H"; //clear the screen
while ( getline (myfile,line))
{
std::cout << line << std::endl;
}
}
void Write()
{
std::string lines;
std::cout << "\x1B[2J\x1B[H"; //clear the screen
std::cout << "Write your thoughts or/and what you did today :D" << std::endl;
std::ofstream myfile("/home/turry/Documents/CPP/Journal/test.txt",std::ios::app); // Hardcoded address
while (true) {
getline(std::cin, lines);
if (lines.empty()){
break;
}
std::cout << "DEBUG\n";
}
std::cout << lines << std::endl;
}
答案 0 :(得分:0)
以下代码应修复
using std::getline;
请见Live Demo。
或者写
while (std::getline (myfile,line))
// ^^^^^
还要考虑明确包含string
标头。从documentation所需要的。
至少使用
std::string line;
要求你
#include <string>
答案 1 :(得分:0)
std::getline
是字符串标题的一部分。
#include <string> should fix the issue.