2013-10-23 27 views
1

我正在尝试编写一个代码,该代码需要输入文件并对其进行组织并计算一些值。但是,我的getName函数有问题,应该通过使用get成员函数来对输入文件中的字符进行求和(如果读取-1),通过引用参数分配第一个和最后一个名称。下面是代码中的错误论点:带有文件流错误的C++无效函数

main.cpp:7:14: error: variable or field 'getName' declared void 
main.cpp:7:14: error: 'ifstream' was not declared in this scope 
main.cpp:7:24: error: 'shoppingCartInput' was not declared in this scope 
main.cpp:7:43: error: 'string' was not declared in this scope 
main.cpp:7:51: error: 'firstname' was not declared in this scope 
main.cpp:7:62: error: 'string' was not declared in this scope 
main.cpp:7:70: error: 'lastname' was not declared in this scope 
main.cpp:102:50: error: 'getName' was not declared in this scope 

这里是代码:

#include <iostream> 
#include <string> 
#include <fstream> 
#include <cstdlib> 
#include <cctype> 

void getName(ifstream& shoppingCartInput, string& firstname, string& lastname); 

using namespace std; 
int main() 
{ 
    // declerations 
    string infile_name = ""; 
    string shopper = ""; 
    string firstname =""; 
    string lastname = ""; 
    int quantity; 
    double price = 0.0, leastExpPrice = 0, mostExpPrice = 0; 
    string item, leastExpItem, mostExpItem; 
    double totalCost = 0.0; 
    //1) Open Your File 
    cout << "please enter the name of the input file:"; 
    cin >> infile_name; 
    cout << "\n"; 
    ifstream shoppingCartInput; 
    ofstream shoppingCartOutput; 
    shoppingCartInput.open(infile_name.c_str()); 
    if(shoppingCartInput.fail()) { 
    cout << "Input file opening failed.\n"; 
    exit(1); 
    } 
    shoppingCartOutput.open("output_file.dat"); 
    if(shoppingCartOutput.fail()) { 
     cout << "Output file opening failed.\n"; 
     exit(1); 
    } 
    //2) Read the first line 
    shoppingCartInput >> quantity >> price >> item; 
    //printing the line to the console (properly organized) 
    shoppingCartOutput.setf(ios::fixed); 
    shoppingCartOutput.setf(ios::showpoint); 
    shoppingCartOutput.setf(ios::left); 
    shoppingCartOutput.width(15); 
    shoppingCartOutput << item << " "; 
    shoppingCartOutput.setf(ios::right); 
    shoppingCartOutput.width(3); 
    shoppingCartOutput << quantity << " "; 
    shoppingCartOutput.setf(ios::right); 
    shoppingCartOutput.width(6); 
    shoppingCartOutput.precision(2); 
    shoppingCartOutput << price << " "; 
    shoppingCartOutput.setf(ios::right); 
    shoppingCartOutput.width(7); 
    shoppingCartOutput.precision(2); 
    shoppingCartOutput << (quantity * price) << endl; 
    //set it as the most expensive 
    //set it as the least expensive 
    leastExpPrice = price; 
    leastExpItem = item; 
    mostExpPrice = price; 
    mostExpItem=item; 
    //initialize total cost to get the cost of the first line of data 
    totalCost = (quantity * price); 
    //3) Read until the end of the file 
    while(!(shoppingCartInput.eof())) { 
     while(quantity != -1) { 
      shoppingCartInput >> quantity >> price >> item; 
      //printing the line to the console (properly organized) 
      shoppingCartOutput.setf(ios::fixed); 
      shoppingCartOutput.unsetf(ios::right); 
      shoppingCartOutput.setf(ios::showpoint); 
      shoppingCartOutput.setf(ios::left); 
      shoppingCartOutput.width(15); 
      shoppingCartOutput << item << " "; 
      shoppingCartOutput.setf(ios::right); 
      shoppingCartOutput.width(3); 
      shoppingCartOutput << quantity << " "; 
      shoppingCartOutput.setf(ios::right); 
      shoppingCartOutput.width(6); 
      shoppingCartOutput.precision(2); 
      shoppingCartOutput << price << " "; 
      shoppingCartOutput.setf(ios::right); 
      shoppingCartOutput.width(7); 
      shoppingCartOutput.precision(2); 
      shoppingCartOutput << (quantity * price) << endl; 
      if(price < leastExpPrice) { 
       leastExpPrice = price; 
       leastExpItem = item; 
      } 
      if(price > mostExpPrice) { 
       mostExpPrice = price; 
       mostExpItem=item; 
      } 
      totalCost += (quantity * price); 
     } 
     if (quantity == (-1)) { 
      string first = ""; 
      string last = ""; 
      getName(shoppingCartInput, firstname, lastname); 
      shopper = firstname + " " + lastname; 
      shoppingCartOutput << "\ncheapest item = " << leastExpItem << 
         endl; 
      shoppingCartOutput << "most expensive item = " << mostExpIte << 
         endl; 
      shoppingCartOutput << "total cost = " << totalCost << endl; 
      shoppingCartOutput.unsetf(ios::right); 
      shoppingCartOutput.setf(ios::left); 
      shoppingCartOutput << "shopper = " << shopper; 
     } 
    } 
    shoppingCartInput.close(); 
    shoppingCartOutput.close(); 
    return 0; 
} 

void getName(ifstream& shoppingCartInput, std::string& firstname, std::string& 
lastname) 
{ 
    char letter; 
    do { 
     shoppingCartInput.get(letter); 
     firstname += letter - 1; 
    } while (letter != ','); 

    do { 
     shoppingCartInput.get(letter); 
     lastname += letter; 
    } while (letter != '\n' || letter != 'r'); 
} 

回答

3

变化:

void getName(ifstream& shoppingCartInput, string& firstname, string& lastname); 
using namespace std; 

到:

using namespace std; 
void getName(ifstream& shoppingCartInput, string& firstname, string& lastname); 
2

std::ifstream你。否则,请在所有#includes之后并在代码的其余部分之前添加using namespace std;