2015-11-16 88 views
0
#include <iostream> 
#include <iomanip> 
#include <fstream> 
#include <string> 

using namespace std; 

struct subscriberName 
{ 
    string first; 
    string last; 
    int custID; 
}; 

struct address 
{ 
    string address2; 
    string city; 
    string state; 
    int zipcode; 
}; 

struct date 
{ 
    string month; 
    int day; 
    int year; 
}; 

struct renewal_information 
{ 
    int monthsLeft; 
    date da; 
}; 

struct subscriberInfo 
{ 
    subscriberName si; 
    address ad; 
    renewal_information ri; 
}; 


int main() 
{ 
    void OpenFileIn(ifstream& FileIn, string& FilenameIn); 
    void OpenFileOut(ofstream& FileOut, string& FilenameOut); 
    bool ProcessCustInfo(bool& truth, ifstream& FileIn); 
    void OutputCustInfo(ifstream& FileIn, ofstream& FileOut); 

    ifstream FileIn; 
    ofstream FileOut; 
    string FilenameIn; 
    string FilenameOut; 
    bool truth; 
    subscriberInfo si; 


    OpenFileIn(FileIn, FilenameIn); 

    OpenFileOut(FileOut, FilenameOut); 

    ProcessCustInfo(truth, FileIn); 

    OutputCustInfo(FileIn, FileOut); 
    return 0; 
} 

bool ProcessCustInfo(bool& truth, ifstream& FileIn, subscriberInfo& si) 
{ 
    getline(FileIn, si.sn.first, '\n');     //here 
    getline(FileIn, si.sn.last, '\n'); 
    getline(FileIn, si.sn.custID, '\n'); 
    getline(FileIn, si.ad.address2, '\n'); 
    getline(FileIn, si.ad.city, '\n'); 
    getline(FileIn, si.ad.state, '\n'); 
    getline(FileIn, si.ad.zipcode, '\n'); 
    getline(FileIn, si.ri.monthsLeft '\n');  //to here 




} 



void OutputCustInfo(ifstream& FileIn, ofstream& FileOut, subscriberInfo& si) 
{ 
    if(si.ri.monthsLeft=0)      //here down to 
    { 
     FileOut << string(55,'*') << endl; 
     FileOut << si.sn.first << " " << si.sn.last << "(" << si.sn.custID << ")" << endl; 
     FileOut << sn.ad.address2 << endl; 
     FileOut << sn.ad.city << ", " << sn.ad.state <<sn.ad.zipcode << endl; 
     FileOut << "The last renewal notice was sent on " <<sn.ri.da.month << " " << sn.ri.da.day << ", " << sn.ri.da.year << endl;   //here 
     FileOut << string(55,'*') << endl; 
    } 
} 

我找不出是什麼原因導致此錯誤。它發生在所有getline調用都在的第一個函數中。編譯器專門調用第三個,第五個和最後一個,但我很確定它們都有問題。收到錯誤「沒有匹配函數調用'getline(std :: ifstream&,int&,char)'」

getline(FileIn, si.sn.custID, '\n'); 

這是一個問題:

+1

'geline()'只接受'串'作爲第二個參數,你需要把它放在一個字符串,然後將其轉換成'int'或其他類型。由於結構中的某些成員的類型爲'int' –

+0

@JohnMarkCaguicla將成員更改爲字符串會更容易嗎?這可能導致什麼問題? – LCro

+0

這取決於你將如何使用它們,如果你只是顯示它們,可以將它們改爲'string',但爲了更實際的目的,你可以使用'custID'作爲int '傳遞給一個函數。在這種情況下,只需將其作爲「字符串」進行檢索,然後將其轉換爲「int」。 –

回答

1

您在傳遞int類型的變量getline

用途:

std::string custID; 
getline(FileIn, custID, '\n'); 
si.sn.custID = std::stoi(custID); 

你有同樣的問題:

getline(FileIn, si.ad.zipcode, '\n'); 

getline(FileIn, si.ri.monthsLeft '\n'); 

此外,線

if(si.ri.monthsLeft=0) 

是錯誤的。我懷疑這是一個錯字。您需要使用==,而不是=

if(si.ri.monthsLeft == 0) 
+0

是的,謝謝。 「std :: stoi(custID)」是做什麼的? – LCro

+0

@LCro,它將字符串轉換爲整數。你可以在http://en.cppreference.com/w/cpp/string/basic_string/stol上看到更多的細節。 –

+0

另外,我只能有3個參數'bool',我怎麼能得到'custID','zipcode'和'monthsLeft'作爲範圍的一部分? – LCro

相關問題