input file with multiple types of data types

Discussion in 'Mac Programming' started by Beckie, May 3, 2005.

  1. macrumors newbie

    Joined:
    Apr 6, 2005
    #1
    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;
    }
    }
     
  2. macrumors 603

    whooleytoo

    Joined:
    Aug 2, 2002
    Location:
    Cork, Ireland.
    #2
    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:

    Code:
    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!
     
  3. thread starter macrumors newbie

    Joined:
    Apr 6, 2005
    #3
    Thank you so much! This helps me so much. This is exactly what I was looking for.

    Take care,
    Beckie
     

Share This Page