2015-11-27 417 views
0

如果我將代碼拆分爲兩個單獨的程序,它的工作原理與我希望的一樣(在創建文件時將separation3部分分開,並在第2部分我嘗試以用戶的身份訪問該文件);但只要將代碼放入單個程序中(請參見下文),我無法使用getline()方法從標準輸入中收集輸入;該程序只是在沒有停止在getline方法位於源代碼中的位置收集用戶輸入的情況下犁到頭。無法使用std :: getline()與ifstream和ofstream一起工作

我讀了一堆回答問題,並試圖無濟於事如下:

  1. 打字#include <string>
  2. 打字outFile.clear();
  3. 打字inFile.clear();
  4. 我試圖在過去3小時其他事情,作爲註釋的代碼部分,看看我能否指出問題。

該程序的目的是創建一個文本文件,然後從'老師'獲得2個等級並將其放入文本文件。第二部分要求用戶輸入文件的路徑;那麼代碼將提供文件中的平均分數。問題是下面的代碼永遠不會停止以允許用戶輸入文件的路徑。

#include <iostream> 
#include <fstream> //file steam for access to objects that work with files 
#include <cstdlib> 

//using namespace std; 

int main() 
{ 
//****************PART #1 - CREATE A FILE & ADD DATA ************************************************* 
std::cout << "Part #1 - create file & put data into it.\n" << std::endl; 
//create an object called "outFile" of type ofstream (output file stream) 
// arg #1 - path and file name 
// arg #2 - constant that represents how we want to work with file (output stream in this case) 
std::ofstream outFile("/home/creator/Desktop/creation.txt", std::ios::out); 

//get user to enter 5 numbers: 
int userGrade; 
for(int i = 0; i < 2; i++) 
{ 
    std::cout << "Enter grade number " << (i+1) << ": "; 
    std::cin >> userGrade; //collect a grade from the user 
    outFile << userGrade << std::endl; //write data to file (each number on it's own line) 
} 

outFile.close();//close the stream 
std::cout << "All is well and good - file is created and data is populated" << std::endl; 

//****************PART #2 - READ & MUNIPILATE DATA FROM FILE***************************************** 
std::cout << "\nNext, lets read the data from the file we created." << std::endl; 
std::cout << "please enter the path to the file: (ex: /home/creator/Desktop/creation.txt)" << std::endl; 
std::string fileName; //the path to the file we want to read. 
std::getline(std::cin, fileName);//<<< THIS IS MY QUESTION/PROBLEM 
std::ifstream inFile(fileName.c_str(), std::ios::in); 

if(!inFile) 
{ 
    std::cout << "File not found!" << std::endl; 
    exit(1); 
} 

double grade = 0;//this holds the data we retrieve from file 
double total = 0; //get the sum of all the grades as a total 
double average = 0; //get the average of all the grades 
int numberOfGrades = 0; //number of grade values in file 

//retreive and munipilate the data from the file. 
while(!inFile.eof()) 
{ 
    inFile >> grade; 
    total = total + grade; 
    numberOfGrades++; 
    std::cout << grade << std::endl; 
} 

average = total/numberOfGrades; 
std::cout << "The average of the grades in the file is: " << average << std::endl; 
inFile.close(); 

return 0; 
} 

輸出作爲代碼的圖片只是犁過:

enter image description here

回答

0

的問題是,以前輸入迴路的葉子在輸入緩衝器中的最後一個換行,這下一次調用std::getline讀爲空行。

在閱讀成績後,可以通過ignoring輕鬆修復其餘部分。

從鏈接參考的例子:

std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); 

在一個不相關的音符,不要使用while (!inFile.eof()) { ... },因爲你希望它不會工作。相反,在你的情況下,你應該做while (inFile >> grade) { ... }

+0

非常感謝。在收集第二個輸入之前,我添加了'std :: cin.ignore();'。再次感謝! – Mike