PDA

View Full Version : input file with multiple types of data types




Beckie
May 3, 2005, 12:06 PM
Hi
I have an input file that I need to infile into my program:

Joe smith 23
alice west 45
bob MILLS 82
*
45 182 210 43 170 301 -1
82 150 97 -220 178 223 300 -1

I can get the names and the numbers after the names in, but when I get to
the asterisk, the computer thinks * is a string instread of a char, and then it skips 45 and 182 and then I can get 210, 43, etc as int data types, but the 45 and 182 are coming up as string data types.

Here is how I am doing it:

while(infile)
{
if (firstname != "*")
{
infile >> firstname >> lastname >> leagueIDnum;
cout << firstname << lastname << leagueIDnum << endl;
}

if(firstname == "*")
{
infile >> bowlingscores;
cout << bowlingscores << endl;
}
}



whooleytoo
May 5, 2005, 08:48 AM
Hi
while(infile)
{
if (firstname != "*")
{
infile >> firstname >> lastname >> leagueIDnum;
cout << firstname << lastname << leagueIDnum << endl;
}

}

I'm afraid this bit will never work. You read in the first, last names and league ID all at once, so by the time you've read in the "*", the program has already read ahead looking for lastname and leagueID.

If you can change the input data, replace "*" with "* * *"? This would be the easiest fix.

Otherwise, you could change the code to something like:

firstname = "" ;

while (firstname != "*")
{
infile >> firstname ;
if (firstname != "*")
{
infile >> lastname >> leagueIDNum ;
cout << firstname << lastname << leagueIDnum << endl;
}
}
.
.
.


(Of course, to be correct you should also be checking for the end-of-file, or handling exceptions in case the input file is incomplete or improperly formatted.)

Hope this helps!

Beckie
May 5, 2005, 12:02 PM
Thank you so much! This helps me so much. This is exactly what I was looking for.

Take care,
Beckie