input problems!

I'm new to programming and trying to do my first basic assignment. Unfortunately the bit I am struggling with is trying to deal with inputting the data.

The input data needs to come from a text file, with three test scores (integers) and then a name on each line. I can get my program to deal with the data fine until it comes across either a blank line, or where one of the test scores is actually letters instead of integers. Where data is bad I need the program to output a message stating it is invalid, followed by the line of bad data.

example of input data
42 44 Jones, I.
0 0 0 James, T.
39 xx 22 Wiggley, L.
41 77 78 Wilcox, P.A.
75 30 75 Zubaida, S.D.

89 0 89 Smith, S.

Therefore I'm assuming I need to do a getline() as the input, but then I'm not sure how to extract the three test scores as integers from my line of data. I need to use these scores to grade on a 1-10 scale and then do some if functions afterwards to say whether they have passed or not.

I'm probably approaching this in totally the wrong way so any help would be muc appreciated...!



closed account (1yvXoG1T)
What code do you have so far?
#include <iostream>
#include <string>
using namespace std;
int main()
{
int isight, ihearing, imobility; // input scores for each test
int sight, hearing, mobility; // grades awarded
string s, data;
bool finished = false;
string deployment;
int n=0;
while(n <=40)
{
cin >> isight >> ihearing >> imobility;
cin.ignore();
getline(cin, data);
if(cin.fail())
{ if(cin.eof())
{ finished = true;
cout << " End of data" << endl;
}

}
int sight = isight/10 + 1, hearing = ihearing/10 + 1, mobility = imobility/10 + 1;

if((sight >=5 && hearing >=5 && mobility >=5) || (sight >=4 && hearing >=4 && mobility >=8))
deployment = "ordinary";
else if(mobility >=7 && ((sight >=7 && hearing >=4) || (sight >=4 && hearing >= 7)))
deployment = "ordinary";
else if(sight >=8 && (hearing >=8 || mobility >=8) || (hearing >=8 && mobility >=8))
deployment = "special";
else deployment = "reject";
cout << data << endl;
cout << sight << " " << hearing << " " << mobility << " " << deployment << endl;
n++;

}
}

It's all a bit rough because I haven't really got past the first hurdle!
#include <iostream>
#include <string>
using namespace std;
int main()
{
int isight, ihearing, imobility; // input scores for each test
int sight, hearing, mobility; // grades awarded
string s, data;
bool finished = false;
string deployment;
int n=0;
while(n <=40)
{
cin >> isight >> ihearing >> imobility;
cin.ignore();
getline(cin, data);
if(cin.fail())
{ if(cin.eof())
{ finished = true;
cout << " End of data" << endl;
}

}
int sight = isight/10 + 1, hearing = ihearing/10 + 1, mobility = imobility/10 + 1;

if((sight >=5 && hearing >=5 && mobility >=5) || (sight >=4 && hearing >=4 && mobility >=8))
deployment = "ordinary";
else if(mobility >=7 && ((sight >=7 && hearing >=4) || (sight >=4 && hearing >= 7)))
deployment = "ordinary";
else if(sight >=8 && (hearing >=8 || mobility >=8) || (hearing >=8 && mobility >=8))
deployment = "special";
else deployment = "reject";
cout << data << endl;
cout << sight << " " << hearing << " " << mobility << " " << deployment << endl;
n++;

}
}

It's all a bit rough because I haven't really got past the first hurdle!
closed account (1yvXoG1T)
First, I would suggest researching using file streams. Very easy to open, read, and write that way. After that, you could try reading in a character:
streamThatYoureUsing.read(reinterpret_cast<char*>(&someChar), sizeof(char));
Then check if someChar falls between a - z or A - Z or is a space or \n then write the information accordingly. In the event that it isn't any of the above then it would follow that it must be an int, in the which case you can cast someChar as an int and write accordingly.

P.S.
Please use code tags next time you post code.
Thanks for your help, I have sorted it using file streams and string streams for the data.
Topic archived. No new replies allowed.