2017-03-07 9 views
-3

getlineを関数内で使用できない理由がわかりませんが、cinを使用します。関数内に入力を取得できませんC++

#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; 

} 
+2

問題は何ですか? – msrd0

+0

'std :: getline(myfile、line)'はありますか? –

+0

''をインクルードする必要がありますか? (あなたが実際に*必要としていなくても、それは 'getline()'関数の正規のヘッダなので、含めるべきです) –

答えて

0

次のコードは、

using std::getline; 

Live Demoをしてくださいを参照してくださいすることを修正する必要があります: は、ここでは、コードです。


はあるいは

while (std::getline (myfile,line)) 
    // ^^^^^ 

も明示的stringヘッダを含むと考えライト。必要なのはdocumentationです

少なくとも

std::string line; 

の使用は文字列ヘッダの一部である

#include <string> 
0

std::getlineする必要があります。

#include <string> should fix the issue. 
+0

OPは第1位の 'std'名前空間を参照していません。 –

+0

@πάνταῥεῖ:ADLはそれを世話しませんか? –

+0

@Fred明らかにOPのケースではありません。 –

関連する問題