Hi, I've 2 pieces of codes here
Grade.cpp
Spam.cppCode:#include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; string ReadLN() { string str; char ch; while (cin.get(ch) && ch != '\n') str = str + ch; return str; } void main() { string FileName; cout << "Enter name of grades file: "; FileName = ReadLN(); ifstream input(FileName.c_str()); if (input.fail()) { cerr << "Could not open file: " << FileName << endl; exit(1); } string line, name, score; double sum = 0, average = 0; int num, count = 0; while (!input.eof()) { getline(input, name, ':'); getline(input, score, '\n'); // Declare an istringstream object/datatype // required #include <sstream> istringstream sin(score); while (sin >> num) { sum += num; count++; } cout << "Average for " << name << ": "; if (count != 0) { average = sum / count; cout << average << endl; } else { cout << "<< Error >>" << endl; } } }
Grade.cpp can compile without error, but Spam.cpp has error on the getline(fData, xxxx); part of it.Code:#include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; string ReadLN() { char ch; string str; while (cin.get(ch) && ch != '\n') str = str + ch; return str; } void main() { string strTemplate, strData; string strTemplateData, strTmp; cout << "Please enter template filename (spam.dat): "; strTemplate = ReadLN(); ifstream fTemp(strTemplate.c_str()); if (fTemp.fail()) { cerr << "Could not open file: " << strTemplate << endl; exit(1); } while (!fTemp.eof()) { getline(fTemp, strTmp); strTemplateData = strTemplateData + strTmp; } fTemp.close(); cout << "Please enter data filename (dummies.dat); "; strData = ReadLN(); ifstream fData(strData.c_str()); if (fData.fail()) { cerr << "Could not open file: " << strData << endl; exit(1); } int intRec = 0; string strFName, strLName, strAdd, strCity, strState; string strOut; while (!fData.eof()) { getline(fData, strOut); getline(fData, strFName, ":"); getline(fData, strLName, ":"); getline(fData, strAdd, ":"); getline(fData, strCity, ":"); getline(fData, strState, "\n"); ofstream fout(intRec + ".spm"); fout << strTemplateData; fout.close(); } cout << "Done!" << endl; }
From the looks of it, both function calls are the same. Is there something that I'm missing?
Thanks for any help.
Harddisk
PS: BTW, the spam code is harmless, just some exercise I m doing.




Reply With Quote