strange error in inputting numbers from .txt file

This is a discussion on strange error in inputting numbers from .txt file within the C++ Programming forums, part of the General Programming Boards category; Hi, I don't know if any of you know the answer to a problem i'm having. I'm inputting a file ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    12

    Unhappy strange error in inputting numbers from .txt file

    Hi,
    I don't know if any of you know the answer to a problem i'm having. I'm inputting a file into my program using a for loop, however, everytime the for loop runs, the line of data has the digit of the iteration in front of the numbers inputted.

    For example,
    original data: 56 85 42
    iteration (1): 156 185 142
    iteration (3): 356 385 342

    Does anyone know how to solve this?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    My Hat asks if you have your for-loop counter (i or whatever) in your << or >> lines anywhere?

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    12
    well, here's some of my code, for example:

    Code:
    struct RangeRecord
    {
    	// Data members
    	unsigned prn;
    	double   time;
    	double   position[3];
    	double   pseudorange;       // range measurement
    	double   pseudorangeStdDev; // range measurement std. dev.
    
        bool read( istream& input );
    
    };
    
    bool readRanges( const string& filename, vector<RangeRecord>& ranges );
    
    int main(int argc, char* argv[] )
    {
        // -- Read ranges into an array -----------------------------------------
    
        vector<RangeRecord> ranges;
        if( !readRanges( config.inputFileName, ranges ) )
        {
            cerr << "Error!\n  Unable to read ranges!\n\n";
           return EXIT_FAILURE;
        }
    
    //main continues (other functions....)
    //end main (main function is fine)
    }
    
    // Read a range record from a file
    // Returns:
    //  true  - input was successful
    //  false - input failed
    //
    // Parameters:
    //  input - input file stream that has already been opened
    
    bool RangeRecord::read( istream& input )
    {
        input >> prn
              >> time
              >> position[0] >> position[1] >> position[2] 
              >> pseudorange
              >> pseudorangeStdDev;
     
    
        if( !input )
            return false;
    
        return true;
    }
    
    
    // Read the configuration from a file
    //
    // Returns:
    //  true  - input was successful
    //  false - input failed
    //
    // Parameters:
    //  filename - Path of file containing ranges
    //  ranges   - Array of ranges
    
    bool readRanges( const string& filename, vector<RangeRecord>& ranges )
    
    {
    int size;
    
        ifstream input( filename.c_str() );
    
        if( !input )
            return false;
    
       input >> size;
       ranges.resize(size); //Changes size of vector to hold records
    
        for(int i = 0; i < size ; i++)
    {
         if(!ranges[i].read(input))
    			return false;
    }
    
    return true;
    }

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So you're storing them fine, which means you are inadvertently printing out your array index i when you print them out.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    Your input code doesn't match your data file.

    RangeRecord::read for example expects only numeric data, like
    1 2 3 4 5 6 7
    Parentheses and words (in your example) will just mess that up.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    12
    well, i 'cout'ed the values after they were input, and then i discovered that the values had changed
    just a simple cout

    Code:
     
    cout << "prn 1" << ranges[0].prn << endl;

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    12
    Quote Originally Posted by Salem View Post
    Your input code doesn't match your data file.

    RangeRecord::read for example expects only numeric data, like
    1 2 3 4 5 6 7
    Parentheses and words (in your example) will just mess that up.
    The entire function RangeRecord::read is correct because it was given in class.
    Also, the .txt file only contains numeric data.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by angelica View Post
    well, i 'cout'ed the values after they were input, and then i discovered that the values had changed
    just a simple cout

    Code:
     
    cout << "prn 1" << ranges[0].prn << endl;
    That's your 1 that you're complaining about, I think. Try putting a space (better, a colon and a space) between the 1 and the closing ".

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    12
    oh geeeeez. what a stupid mistake. thank-you so much for all your help! I really appreciate it

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. Comparing numbers to a list of numbers held in a text file
    By jmajeremy in forum C++ Programming
    Replies: 3
    Last Post: 11-06-2006, 06:56 AM
  3. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  4. C++ extract only numbers from a .txt file
    By overspray in forum C++ Programming
    Replies: 2
    Last Post: 04-17-2003, 01:27 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

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