2013-05-17 71 views
1

我试图从文本文件中获取输入,并使用getline()将数据放入四个向量中。该文件包含两个字符串,一个double和一个整数,全部位于不同的行上,每个组由一个空行分隔。getline()和向量问题(C++)

if (userChoice == 1) // Load 
      { 
       in_stream.open("Lab11.txt"); 
       if (in_stream.fail()) 
       { 
        cerr << "File does not exist" << endl; 
        system("PAUSE"); 
        exit(1); 
       } 
       index = 0; 
       do 
       { 
        getline(in_stream, itemNumb[index]); 
        getline(in_stream, itemName[index]); 
        getline(in_stream, itemCost[index]); 
        getline(in_stream, itemQuant[index]); 
        index++; 
       } while (! in_stream.eof()); 
       in_stream.close(); 
       itemStored = 0; 
       cout << "Items stored: " << itemStored << endl; 
      } 

itemNumb和itemName是字符串矢量,itemCost是double,itemQuant是整数。字符串的行不会给出错误,但double和整数的行会给出相同的错误,并且类型会根据哪个向量发生更改。

错误:

no matching function for call to getline(std::ifstream&, double&)'|

任何帮助将不胜感激! 编辑:全码

#include <iostream> 
    #include <cstdlib> 
    #include <fstream> 
    #include <vector> 
    #include <string> 
    #include <sstream> 

    using namespace std; 

    void Add(vector<string>& itemNumb, vector<string>& itemName, vector<double>& itemCost, vector<int>& itemQuant, string name, string numb, double cost, int quant, int length, int index); 

    void Search(vector<string>& itemNumb, vector<string>& itemName, vector<double>& itemCost, vector<int>& itemQuant); 

    void List(vector<string> itemNumb, vector<string> itemName, vector<double> itemCost, vector<int> itemQuant, int length, int index); 

    int main() 
    { 
     ifstream in_stream; 
     ofstream out_stream; 
     vector<string> itemName, itemNumb; 
     vector<double> itemCost; 
     vector<int> itemQuant; 
     string name = "000", numb = "000"; 
     double cost = 0.0; 
     int quant = 0, length = 0, index = 0; 
     int userChoice, itemStored; 
     do 
     { 
      cout << "===========================" << endl << "1) Load" << endl << "2) Add" << endl << 
    "3) Search" << endl << "4) List" << endl << "5) Save" << endl << endl << "0) Exit" << endl; 
      cout << "Choose an option" << endl; 
      cin >> userChoice; 
      if (userChoice == 1) // Load 
      { 
       in_stream.open("Lab11.txt"); 
       if (in_stream.fail()) 
       { 
        cerr << "File does not exist" << endl; 
        system("PAUSE"); 
        exit(1); 
       } 
       index = 0; 
       do 
       { 
        getline(in_stream, itemNumb[index]); 
        getline(in_stream, itemName[index]); 
        in_stream >> itemCost[index]; 
        in_stream >> itemQuant[index]; 
        index++; 
       } while (! in_stream.eof()); 
       in_stream.close(); 
       itemStored = itemNumb.size(); 
       cout << "Items stored: " << itemStored << endl; 
      } 
      else if (userChoice == 2) // Add 
      { 
       Add(itemNumb, itemName, itemCost, itemQuant, name, numb, cost, quant, length, index); 
       itemStored++; // Function seems to sort oddly when strings of varying lengths are compared 
       cout << endl; 
       cout << itemStored << " items stored" << endl; 
       cout << endl; 
      } 
      else if (userChoice == 3) // Search 
      { 
       Search(itemNumb, itemName, itemCost, itemQuant); 
      } 
      else if (userChoice == 4) //List 
      { 
       List(itemNumb, itemName, itemCost, itemQuant, length, index); 
      } 
      else if (userChoice == 5) //Save 
      { 
       out_stream.open("Lab11.txt", ios::app); 
       if (out_stream.fail()) 
       { 
        cerr << "File does not exist" << endl; 
        system("PAUSE"); 
        exit(1); 
       } 
       index = 0; 
       length = itemNumb.size(); 
       while (index != length) 
       { 
        out_stream << endl; 
        out_stream << itemNumb[index] << endl; 
        out_stream << itemName[index] << endl; 
        out_stream << itemCost[index] << endl; 
        out_stream << itemQuant[index] << endl; 
        index++; 
       } 
       out_stream.close(); 
      } 
     } 
     while (userChoice != 0); 
    } 

编辑文件IO的线,导致运行错误

do 
    { 
     getline(in_stream, tempstr1); 
     itemNumb.push_back(tempstr1); 
     getline(in_stream, tempstr2); 
     itemNumb.push_back(tempstr2); 
     in_stream >> tempdoub; 
     itemCost.push_back(tempdoub); 
     in_stream >> tempint; 
     itemQuant.push_back(tempint); 
     index++; 
    } while (! in_stream.eof()); 
    in_stream.close(); 

回答

1

std::getline第二个参数可能是唯一std::basic_string<...>。要阅读intdouble你应该使用重载operator>>这样的:

in_stream >> itemCost[index] >> itemQuant[index]; 
+0

我把它改成你放什么,但现在,如果语句导致程序崩溃。 –

+0

你能发布完整的代码吗?也许,向量中没有足够的空间? – soon

+0

所有这一切都不会,但过去应该不会对它产生影响。 –