Reading data file problems!

This is a discussion on Reading data file problems! within the C++ Programming forums, part of the General Programming Boards category; I am trying to read in a date from a data file that looks osmething like this 10/ 2 3 ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    27

    Reading data file problems!

    I am trying to read in a date from a data file that looks osmething like this

    10/ 2 3 / 20 0 4

    I want my variables to be month = 10, day = 23, and year = 2004

    Right now, it is only reading the last two numbers..so month and day are fine but year I am getting 04. Can anybody see what I'm doing wrong?

    I haven't included my main function but it just opens my data file.

    Code:
    void process_text(istream& in_file, ofstream& out_stream)
    {
      char next;
      int new_next;
      int last_digit = 0;
      int month;
      int day;
      int year;
      in_file.get(next);
      while(next != '/')
      {
       if(isdigit(next))
        {
          new_next = convert_digit(next);
          month = total_number(new_next, last_digit);
          last_digit = convert_digit(next);
          in_file.get(next);
        }
       else
          in_file.get(next);
      }
    
      last_digit = 0;
      
      in_file.get(next);
      while(next != '/')
      {
       if(isdigit(next))
        {
           new_next = convert_digit(next);
           day = total_number(new_next, last_digit);
           last_digit = convert_digit(next);
           in_file.get(next);
        }
       else
           in_file.get(next);
      }
       
      last_digit = 0;
          
      in_file.get(next);
      while(next != '/' && next != '\n')   
      {
       if(isdigit(next))
        {  
          new_next = convert_digit(next);
          year = total_number(new_next, last_digit);
          last_digit = convert_digit(next);
          in_file.get(next);
        }
       else
          in_file.get(next);
      }
       
    cout << "the month is " << month << endl << endl;
    cout << "the day is " << day << endl << endl;
    cout << "the year is " << year << endl << endl; 
    }
           
    int convert_digit(char& number)
    {
      int new_number = static_cast<int>(number) - 48;
      return(new_number);
    }  
      
    int total_number(int tot_num, int last_digit_par)
    {
      
        return((last_digit_par * 10) + tot_num);
    }

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Do you have to do it yourself? Can you use stringstreams or sscanf or something instead?

    Code:
          new_next = convert_digit(next);
          year = total_number(new_next, last_digit);
          last_digit = convert_digit(next);
    Why don't you just change that last line to this?
    Code:
    last_digit = new_next;
    You're calling in_file.get(next) more than you need to.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    27
    Unfortunately, I am not allowed to use the string class for this project.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading data from a text file
    By Dark_Phoenix in forum C++ Programming
    Replies: 8
    Last Post: 06-30-2008, 03:30 PM
  2. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  4. Reading data from a binary file
    By John22 in forum C Programming
    Replies: 7
    Last Post: 12-06-2002, 02:00 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 07:51 PM

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