how to i correct this code to read the file correctly?

This is a discussion on how to i correct this code to read the file correctly? within the C++ Programming forums, part of the General Programming Boards category; This code is reading a file visitors.txt and the file has a list of information like this: int Name, enumerated ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    3

    how to i correct this code to read the file correctly?

    This code is reading a file visitors.txt and the file has a list of information like this:

    int
    Name, enumerated data type, int, int, int, int
    name, edt, int, int, int, int
    name, edt, int, int, int, int
    name, edt, int, int, int, int

    etc.

    i want to skip the first int, and take in the rest of the values. I think I might've messed up while trying to skip the commas. When I run the program it keeps printing out "didn't pink up the Type of Visitor" in an infinite loop.....ahh! help! Here's the code:

    Code:
      char char_ignore[5],char_name[25],char_numType[10], char_numAH[3], char_numAM[3], char_numDurH[3], char_numDurM[3];
      fstream file;
      file.open("visitors.txt");
    
      if(file.is_open())
      {
        file.getline(char_ignore,5,'\n');
        while(!file.eof())
          {
            file.getline(char_name,25,',');
            file.ignore();
            file.getline(char_numType,10,',');
            file.ignore();
            file.getline(char_numAH,3,',');
            file.ignore();
            file.getline(char_numAM,3,',');
            file.ignore();
            file.getline(char_numDurH,3,',');
            file.ignore();
            file.getline(char_numDurM,3,',');
            file.ignore();
    
            Visitor temp;
            temp.name = (string)char_name;
            if((string)char_numType == "UNDERGRAD")
              temp.type = UNDERGRAD;
            else if((string)char_numType == "GRAD")
              temp.type = GRAD;
            else if((string)char_numType == "PROF")
              temp.type = PROF;
            else
              cout << "Didn't pick up the Type of Visitor";
            temp.a_hours = (int)char_numAH;
            temp.a_mins = (int)char_numAM;
            temp.d_hours = (int)char_numDurH;
            temp.d_mins = (int)char_numDurM;
    
            fifo.push(temp);
          }
        file.close();
      }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So, do you know what it does read in? Also, getline discards the delimiter character, so you shouldn't need to discard any commas.

    Also, your attempts to cast char arrays to (string) and (int) are valiant, but ultimately hopeless. You can copy C-strings to strings, and you can use atoi or strtol or similar to get ints.

  3. #3
    A10
    A10 is offline
    Registered User
    Join Date
    Nov 2006
    Posts
    85
    Code:
            if((string)char_numType == "UNDERGRAD")
              temp.type = UNDERGRAD;
            else if((string)char_numType == "GRAD")
              temp.type = GRAD;
            else if((string)char_numType == "PROF")
              temp.type = PROF;
            else
              cout << "Didn't pick up the Type of Visitor";
    That doesn't look right to me. Try loosing the (string) and compairing them with strcmp()

  4. #4
    coder
    Join Date
    Feb 2008
    Posts
    127
    Code:
    if((string)char_numType == "UNDERGRAD")
    It doesn't look right but it works. What would be bad with that?

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,160
    Quote Originally Posted by carlorfeo View Post
    Code:
    if((string)char_numType == "UNDERGRAD")
    It doesn't look right but it works. What would be bad with that?
    It's bad because it doesn't look right. Why not use a normal looking temporary?
    Code:
    if(string(char_numType) == "UNDERGRAD")

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21