If the file is exactly as you've shown then it has blank lines in it, and you (inexplicably) told your program to break the input loop if it encounters a blank line ("error no more data"). Just have it say "blank line" (or nothing) and continue (you don't really need all the continues, though).
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <string>
usingnamespace std;
int main() {
string filename;
getline(cin, filename);
ifstream inputFile(filename);
string line;
while (getline(inputFile, line)) {
int a = 0, b = 0;
string c = "c", d = "d";
istringstream sin(line);
if (!(sin >> a)) {
if (sin.eof())
cerr << "blank line\n";
else
cerr << "incorrect datatype\n";
}
elseif (!(sin >> b)) {
if (sin.eof())
cerr << "a value only\n";
else
cerr << "incorrect data type\n";
}
elseif (!(sin >> c))
cerr << "a and b value only\n";
elseif (c != "x" && c != "y")
cerr << "c value isnt correct\n";
elseif (!(sin >> d))
cerr << "a and b and c value only\n";
elseif (d != "x" && d != "y")
cerr << "d value isnt correct\n";
elseif (!(sin >> ws).eof())
cerr << "ignorning extra data\n";
}
}
Is there any function that can remove the spaces in my text file :/ its for an assignment and the text files supplied have a space in between. It said feel free to use any functions that might help such as "using peek() function to remove any tabs or spaces; etc.)." in the assignment details
I assume by "spaces" you mean blank lines. The above code skips them just fine. All you need to do is stop it from printing "blank line". It'll just do nothing and loop around for the next line. Something like this:
1 2 3 4 5
if (!(sin >> a)) {
if (!sin.eof())
cerr << "incorrect datatype\n";
// otherwise it's a blank line; ignore it
}