i have to write a program that 2. Use file I/O to read the integers from the file. If the data file is saved in your project folder, only
the file name is needed when you declare an ifstream variable, otherwise, a path to the file is
required.
3. Suppose the ifstream variable is input, then use while(!input.eof()) for testing whether the end
of file is reached.
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <sstream>
usingnamespace std;
// Determines if the string is a number
bool isNumeric(string pszInput);
int main () {
// Hold the line in the file
string line;
// Open the file
ifstream myfile ("example.txt");
if (myfile.is_open()) {
// While the file is good
while (myfile.good() ) {
// Get the current line in the file
getline (myfile, line);
// Verify that the line is an integer
if (isNumeric(line)) {
// Convert 'line' to an integer and calculate
if (atoi(line.c_str())%2 == 0) {
cout << "Even\n";
} else {
cout << "Odd\n";
}
}
}
myfile.close();
} else {
cout << "Unable to open file\n";
}
// Exit
return 0;
}
// Determines if the string is a number
bool isNumeric(string pszInput) {
istringstream iss(pszInput);
double dTestSink;
iss >> dTestSink;
// was any input successfully consumed/converted?
if (!iss) {
returnfalse;
}
// was all the input successfully consumed/converted?
return (iss.rdbuf()->in_avail() == 0);
}
it might be a little shotty.. whipped it up in a couple minutes
No I mean. Well for one he posted his thread in the wrong forum. He didn't so much ask a question as plead for help, oh and he didn't even thinly vale the fact that this is a homework assignment that you just gave him a complete answer to. Also no code tags (but that's nitpicking.)
I didn't mean there was so much wrong with his code, I meant there was so much wrong with this thread.