|
-
Aug 28th, 2001, 02:34 PM
#1
Thread Starter
Hyperactive Member
getline : what's wrong with these? [RESOLVED]
Hi, I've 2 pieces of codes here
Grade.cpp
Code:
#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;
}
}
}
Spam.cpp
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;
}
Grade.cpp can compile without error, but Spam.cpp has error on the getline(fData, xxxx); part of it.
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.
Last edited by Harddisk; Oct 10th, 2005 at 10:03 PM.
-
Aug 28th, 2001, 03:06 PM
#2
PowerPoster
what error are you getting
-
Aug 28th, 2001, 10:05 PM
#3
Thread Starter
Hyperactive Member
Argh...
I've found out what's wrong...
Silly me......
Should've put
getline(fData, strAnything, ':'); instead of using double quotes.
It's working now.
Thanks 
Harddisk
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|