using c++ std::string object use the find method to locate the ':' and then the substr method to extract all the characters from position 0 up to but not including the colon.
I'm not going to write it for you, so post the code you have done.
Ancient Dragon
Retired & Loving It
30,041 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,341
Or you can use stringstream and getline with ':' as the delimiter. That's generally easier to get right:
#include <iostream>
#include <sstream>
#include <string>
int main()
{
std::stringstream in ( "A:B:C" );
std::string token;
while ( std::getline ( in, token, ':' ) )
std::cout<<"Split token: "<< token <<'\n';
}
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>Can anyone plz provide me with the sample code.
I've done that. Now you provide us with proof that you've attempted to solve the problem.
>Above one is not working for me.
Then you're using it wrong. Post your attempt.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
I strongly suggest that you try Narue's option. What didn't work with that code?
If u can provide me with wat u have done, that will be help ful.
Where to start..
- don't use <a href="http://www.research.att.com/~bs/bs_faq2.html#void-main">void main</a> () . Use int main(void) , that's how it's defined. (or int main(int argc, char* argv[])
- don't use conio.h . It's outdated
- don't use clrscr() . Not portable
- don't use goto. A loop can always do what goto does.
- don't use getch() use getchar() instead.
- instead of gets() use <a href="http://www.cplusplus.com/reference/clibrary/cstdio/fgets.html">fgets</a> ()
- use logical var-names. If you're writing a big program you don't want vars like i,j,k,l,m,m1,m2 etc...
- if you use cout , put std:: infront of it. Or using namespace std ; above main.
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
You'd be amazed how much "rough code" makes it through to production.
Get it right first time, then you won't have to say "I'll fix it later, promise", then fail miserably to back up your statement.
The same goes for indenting, commenting, structuring etc. It takes less time to do it properly first time around than it would be to continually struggle with a massive blob of poorly indented and uncommented code.
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
Don't say int main( void ) either. Themain() function actually does take arguments... but if you don't want to deal with them use C++, not some bad C abomination: int main()
When in Rome, do as the romans. When using C++, use C++ I/O.
Don't use getchar() (or any of the non-standard variations). Use std::cin.get() instead.
Don't use fgets() (or, *gasp*, gets()). Use std::getline() or std::cin.get() instead.
Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229