Solved

terminate input

Posted on 1998-02-19
3
220 Views
Last Modified: 2010-04-10
How do I alter the getline function to terminate input on any one of few possibilities.

e.g.
~~~~.getline(line,LINEMAX,('\n'||'.'||'\t'||"a '/' followed by a space etc etc))

the above simply doesn't work.

Any help?
0
Comment
Question by:col
3 Comments
 
LVL 3

Accepted Solution

by:
q2guo earned 50 total points
ID: 1182533
What you can do is write a new getline function:
Here is what I think it should look like

char *dems should point to array of chars.  Each of the
chars in this array will cause newgetline to terminate.

int newgetline(const ifstream &in, char *buf,  in count, char *dems)
{
      int i=0;
      int k=0;
      char ch;

      while (i <count)
      {
            in.get(&ch);
            buf[i] = ch;
            while (dems[k] != '\0') {
                  if (dems[k] == ch) {
                        buf[i] = '\0';
                        return 1;
                  }
                  k++;
            }
            i++;
      }
      return 0;
}


0
 
LVL 22

Expert Comment

by:nietod
ID: 1182534
that wil work, but you could use strchr() to simplify (and probably speed up) this.

while (i <count)
{
   in.get(&ch);
   if (dems,ch)
   {
      buf[i] = '\0';
      return 1;
   }
   ++i;
   buf[i] = ch;
}

also you realy don't need ch.  I would use the input buffer dirrectly and use a character pointer rather than index buf with [] to speed things up.
0
 

Author Comment

by:col
ID: 1182535
Exactly what I was looking for thanks!!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question