read to end of line problem

This is a discussion on read to end of line problem within the C++ Programming forums, part of the General Programming Boards category; I'm kicking myself because this isn't working. Basically, I need to read a bunch of numbers from an input file ...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    2

    Angry read to end of line problem

    I'm kicking myself because this isn't working. Basically, I need to read a bunch of numbers from an input file to an array and sort them. My problem is, I'm supposed to stop at the end of the line, sort that array, then read another line, sort that, and so on. I'm having trouble getting the loop to stop inputing at the end of the line. Using chars, I can get it to recognize the end of line character ('\n'), but using integers this doesn't work. Any ideas? My input text is listed below to give you an idea of what I'm talking about:


    data.txt:

    9132574
    8742323523498723648
    23423
    21342354324523467

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Maybe:
    http://www.cppreference.com/cppio_details.html#getline

    Post your code too.

    Or maybe something like:
    Code:
    #include <iostream>
    
    int main(void)
    {
      int ch;
      
      while ((ch = std::cin.get()) != EOF)
      {
        if (ch == '\n')
        {
          std::cout <<"End of line reached" <<std::endl;
        }
      }
      
      return(0);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    What's wrong with reading it as char's. Just subtract the ascii value for '0' from the char that was read.

    eg. if you read '9', then '9' - '0' will give you the integer value 9.

  4. #4
    Registered User
    Join Date
    Nov 2003
    Posts
    2
    Dunno why I can't read as char's, my prof is a jerk about it for some reason. I have this whole thing working using chars, output looks perfect. He's making me re-do it and I can't get it working. It's really annoying. I'll try the above mentioned code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading a buffer line by line, while using system read
    By Hammad Saleem in forum C Programming
    Replies: 9
    Last Post: 05-27-2008, 05:41 AM
  2. "sorting news" assignment
    By prljavibluzer in forum C Programming
    Replies: 7
    Last Post: 02-06-2008, 05:45 AM
  3. unable to read double A[0] and A[1] when n=1
    By sweetarg in forum C Programming
    Replies: 2
    Last Post: 10-25-2005, 12:35 PM
  4. Greenhand want help!
    By leereg in forum C Programming
    Replies: 6
    Last Post: 01-29-2002, 05:04 AM
  5. End of Line character
    By jloyd01 in forum C Programming
    Replies: 3
    Last Post: 12-02-2001, 04:59 AM

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