Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 [Tutorial] Extracting data from a string
Index -> Programming, C++ -> C++ Tutorials
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
wtd




PostPosted: Mon Oct 04, 2004 12:07 am   Post subject: [Tutorial] Extracting data from a string

The standard stream classes we use for dealing with classes have well known capabilities to extract data (integers, floating-point numbers, etc.).

code:
#include <iostream>

int main()
{
   int i;
   double d;

   std::cin >> i >> d;

   return 0;
}


But what if you want to perform a similar operation on a string?

The istringstream class allows that very thing.

code:
#include <string>
#include <sstream>
#include <iostream>

int main()
{
        int i;
        double d;

        std::string str = "32 42.67";
        std::istringstream input(str);

        input >> i >> d;

        return 0;
}


Additionally, it's possible to check for errors with this class.

code:
#include <string>
#include <sstream>
#include <iostream>

int main()
{
        double d;

        std::string str = "hello";
        std::istringstream input(str);

        input >> d;

        if (input.fail())
        {
                std::cerr << "Couldn't extract a double." <<std::endl;
        }

        return 0;
}
Sponsor
Sponsor
Sponsor
sponsor
Andy




PostPosted: Mon Oct 04, 2004 4:01 pm   Post subject: (No subject)

very very cool

+10 bits
Mazer




PostPosted: Mon Oct 04, 2004 4:31 pm   Post subject: (No subject)

Shocked
Well... wow. When I was reading this in the morning, I was too tired to know what was going on. But wow, this is quite slick.
Andy




PostPosted: Mon Oct 04, 2004 5:11 pm   Post subject: (No subject)

who needs throw and catch when u have this 8)
rizzix




PostPosted: Wed Oct 20, 2004 1:11 pm   Post subject: (No subject)

yea dodge u need to learn a lot. Laughing
Andy




PostPosted: Wed Oct 20, 2004 3:03 pm   Post subject: (No subject)

o psh, u java mod, get out of my c++ world lol
wtd




PostPosted: Wed Oct 20, 2004 4:44 pm   Post subject: (No subject)

Exceptions are still very useful.

The method I posted, and the error-checking I demonstrated, is an improvement on the methods one would use to do the same in C (in many many ways). This doesn't mean there's not room for improvement.
Andy




PostPosted: Wed Oct 20, 2004 4:55 pm   Post subject: (No subject)

i noe they are still useful.. im just glad that there is a niceway of doing it without exceptions in c++
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Wed Oct 20, 2004 5:16 pm   Post subject: (No subject)

Going into pedantic mode for a moment:

What I demonstrated was C++ code. C is a different programming language. The two are not interchangeable.
Andy




PostPosted: Thu Oct 21, 2004 3:39 pm   Post subject: (No subject)

you have no proof.. o crap.. it says edited by dodge.. damit Embarassed
wtd




PostPosted: Thu Oct 21, 2004 5:00 pm   Post subject: (No subject)

I should have quoted you. Wink
Andy




PostPosted: Thu Oct 21, 2004 5:30 pm   Post subject: (No subject)

lol incase you havent noticed, i have c++ powers... and i can edit ur posts lol
Geminias




PostPosted: Fri Jan 13, 2006 5:34 am   Post subject: (No subject)

hey wtd, if you try to input a string manually with:

"std::cin >> str;"

I found that it doesn't extract. Can you explain why this is? And what might be a suitable alternative to initializing the str at runtime?
wtd




PostPosted: Fri Jan 13, 2006 7:23 am   Post subject: (No subject)

Be aware that if you do that, the string it reads will only be upto the first space, by default.

If, for instance, the user types "hello world foo bar", "str" will only contain "hello". You would need to use std::getline to read an entire line of input.
Display posts from previous:   
   Index -> Programming, C++ -> C++ Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 14 Posts ]
Jump to:   


Style:  
Search: