954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How can I use a delimter to take sevral variables from the same input

Hello
I need the user to input for example a time format HH:MM(AMorPM) or a complex number format: X+jY

Then I want to read HH in and int and MM in another int
and the same for the complex number example.

I am aware I should use delimiters, but i dont know what to do, any help will be appreciated

bojomojo
Newbie Poster
24 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

I believe this is generally called tokenizing? Take a look at this -
http://www.daniweb.com/forums/thread27905.html

Is that what you were asking?

daviddoria
Posting Virtuoso
1,996 posts since Feb 2008
Reputation Points: 437
Solved Threads: 204
 

Look at the time input validation stuff and try to adopt it for complex number input (and other user types):

struct Time {
    int hh, mm;
    std::string ampm; // "AM" or "PM"
};

bool getTime(std::istream& is, Time& t)
{
    bool res = false;
    char c;
    if (is >> t.hh) { // true if it's a number
        if (is.get(c) && c == ':' && (is >> t.mm)) { // :MM here
            if ((is >> t.ampm) && (t.ampm == "AM" || t.ampm == "PM")) {
                // Check up hh & mm values
                if (t.hh >= 0 && t.hh <= 12 && t.mm >= 0 && t.mm < 60)
                    res = true; // good time value
            }
        }
    } 
    return res;
}
// Advanced (syntax sugar):
std::istream& operator>>(std::istream& is, Time& t) {
    if (!getTime(is,t))
        is.setstate(std::ios::failbit);
    return is;
}
std::ostream& operator<<(std::ostream& os, const Time& t) {
    os << t.hh << ':' << t.mm << ' ' << t.ampm;
    return os;
}

However this approach has a serious defect in the console input environment. If the user types a part of a valid time value only then getTime waits the rest of Time value. It seems like you program hangs. Possible workaround: read a line then use istringstream:

bool GetTime(Time& t)
{
    bool res = false;
    std::string line;
    if (std::getline(std::cin,line)) {
        std::istringstream is(line);
        res = getTime(is,t);
    }
    return res;
}

Of course, it's not areference implementation. Sorry but I have no time to prepare Program Logic Manual for this improvisation ;)...

ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
 

I had that first method in a book.

But i have a question:
how does the program know how many characters are hh and mm and so on

we fist tell it if(cin>>hh) will that work whether the user inputs 10:40 or 2329:40?

I will check for it to be between 0 and 12, but the thing I mean is how can it tell that it will stop before the ":"

bojomojo
Newbie Poster
24 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

Another option would be to use boost libraries and regex to evaluate a correct time. It would save a lot of messing around as ArkM has demonstrated. :P

For example:
http://binaryelves.wordpress.com/2008/08/28/regular-expressions-time-hhmm-validation-24-hours-format/

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

Another option would be to use boost libraries and regex to evaluate a correct time. It would save a lot of messing around as ArkM has demonstrated. :P

For example: http://binaryelves.wordpress.com/2008/08/28/regular-expressions-time-hhmm-validation-24-hours-format/

I am not doing it for the time, I want to learn the method, the time is just an example.

Would appreciate if someone tell me what tells the program to stop reading hh before the delimiter when i use

if (cin>>hh>

bojomojo
Newbie Poster
24 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

Nah look at the link below kiddo:
http://www.daniweb.com/forums/post140537-2.html

Then you separate them into to arrays.

array[0]
array[1]

Then go through all the rules, so you ensure you get valid times.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 
Nah look at the link below kiddo: http://www.daniweb.com/forums/post140537-2.html

okay, this is alot easier, but this will get all of them as strings, i want to be able to extract some as strings, some as chars and some as intergers and doubles
and i also want them to be into different variables, not just one, like token for example

and is the std:: instead of using namespace std ??

bojomojo
Newbie Poster
24 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

okay, i dont get the last part you added to your post, about the arrays, how can i extract different types as I specified, and keep them in different variables

bojomojo
Newbie Poster
24 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

Pretty simple really, to convert a string to an int etc, just use stringstreams.

http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1046996179&id=1043284385

You don't really need to use single chars here.


And if you use a vector instead of an array you just build the values on the fly as you go. Although you could use a dynamic array, but that's gay in my opinion.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

okay, and what you meant by the arrays

you mean instead of the while with the token,

i make it like getlink(iss, token[i], :) ????

is that what you mean?
btw, i never used stringstreams, and i dont even know what is iss :)

bojomojo
Newbie Poster
24 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

>btw, i never used stringstreams, and i dont even know what is iss

Yeah well, you learn something new tous le jour and such and such.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

yes, i agree, but you didnt answer my question about the arrays

bojomojo
Newbie Poster
24 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

Well personally I'd use a vector:

vissssssssssssssssssss

#include <iostream>
#include <vector>
#include <string>
#include <sstream>

using namespace std;

int main()
{

   vector <string> array; 
 
   string tmp = "12:20";
   string token;
   istringstream iss(tmp);
   while ( getline(iss, token, ':') )
   {
      array.push_back(token);
   }   
   cout <<array[0] <<endl;
   cout <<array[1] <<endl;
   
   cin.get();
}
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

i think i will have to

vector array


in order for that to work, correct?

bojomojo
Newbie Poster
24 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

I guess, but I'm just conjecturing here.

I was thinking you might get into trouble with times such as "03:45" but maybe stringstream converts the "03" to 3 anyhoo.

Don't quote me on that.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

very good,

but i am not quite getting istringstream, i got its to input the stream, and does iss has anything to do with it?

bojomojo
Newbie Poster
24 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

Post your code...

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

what code?
I mean the one you made, this one:

#include <iostream>
#include <vector>
#include <string>
#include <sstream>

using namespace std;

int main()
{

   vector <string> array; 
 
   string tmp = "12:20";
   string token;
   istringstream iss(tmp);
   while ( getline(iss, token, ':') )
   {
      array.push_back(token);
   }   
   cout <<array[0] <<endl;
   cout <<array[1] <<endl;
   
   cin.get();
}
bojomojo
Newbie Poster
24 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

Is that a joke? No the bit where you're not quite getting the istringstream?

Post an example of what you've tried, state what you expect, and what you're unsure of.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You