Solved

terminate input

Posted on 1998-02-19
3
195 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now