getline question

This is a discussion on getline question within the C++ Programming forums, part of the General Programming Boards category; Hi, does anyone know if you can get getline to read to ',' or the end of the line... so ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    20

    getline question

    Hi, does anyone know if you can get getline to read to ',' or the end of the line...

    so for example:
    Input:
    1,s,3a,find
    2,3b,g,go

    Code:
    while(fin.peek!=EOF)
    {
        char temp[100];
         fin.getline(temp,100, ',' | '\n');
         cout<<temp<<endl;
    }
    so you get an output of this:
    1
    s
    3a
    find
    etc...

    does anyone know how to do this?

    Thanks

  2. #2
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    that is what it is doing right now...

  3. #3
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    It's usually better to read the whole line and then break it up at your leisure. Nobody likes to be rushed when it comes to potentially buggy input. :-)
    Code:
    #include <iostream>
    #include <cstring>
    #include <cstdlib>
    
    using namespace std;
    
    int main()
    {
      char a[100];
    
      cin.getline(a, 100, '\n');
    
      for (char *p = strtok(a, ","); p != 0; p = strtok(0, ","))
      {
        cout<< p <<endl;
      }
    
      system("PAUSE");
    }
    *Cela*

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    20
    ...really, its not working for me. get line is reading to the end of the line and spitting out the whole line. does anyone know why its not working?

    Thanks

  5. #5
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    That is what that code is meant to do...
    fin.getline(temp,100, ',' | '\n'); real till ' or \n is encountered and display..

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    20
    hmm...well, if you wouldn't mind vasanth, could you take a look at this and see if you know whats wrong?

    I've included my code, input and output

    Thanks
    Attached Files Attached Files

  7. #7
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    could you tell us in detail what output u r expecting for that input...

  8. #8
    Registered User
    Join Date
    Jan 2003
    Posts
    20
    oh..sorry
    I'm expecting something like this;
    1

    back
    4
    1
    a
    bang
    3

    Thanks

  9. #9
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    Cela's soultions should work then.. amd soory.. i did not understand the output you wanted in the begining...

  10. #10
    Registered User
    Join Date
    Jan 2003
    Posts
    20
    hehe, k, thx for your input tho! =o) And Thx Cela!!=o)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question About Getline
    By bengreenwood in forum C++ Programming
    Replies: 2
    Last Post: 04-28-2009, 05:13 PM
  2. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  3. A question about getline
    By jriano in forum C++ Programming
    Replies: 2
    Last Post: 06-24-2003, 02:21 AM
  4. getline question
    By Swaine777 in forum C++ Programming
    Replies: 2
    Last Post: 03-30-2003, 05:17 AM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM

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