| | |
How to split 2 varibles using delimiter.
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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.
I'm not going to write it for you, so post the code you have done.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Or you can use stringstream and getline with ':' as the delimiter. That's generally easier to get right:
C++ Syntax (Toggle Plain Text)
#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'; }
I'm here to prove you wrong.
•
•
Join Date: Jul 2007
Posts: 35
Reputation:
Solved Threads: 0
I was able to do it like this.
If u can provide me with wat u have done, that will be help ful.
Thanks.
C++ Syntax (Toggle Plain Text)
#include<iostream.h> #include<stdio.h> #include<conio.h> #include<string.h> void splitString(char inputstr[100]) { clrscr(); // char inputstr[100], char str[10][20]; // gets(inputstr); int i=0,j=0,k=0,l; while(inputstr[i]!='\0') { for(l=0;l<20;l++) { str[j][l]='\0'; } str[j]; while(inputstr[i]!=':') { str[j][k++]=inputstr[i++]; if(inputstr[i]=='\0') { str[j][k+1]='\0'; goto label; } } str[j][k+1]='\0'; k=0; j++; i++; } label: for(i=0;i<=j;i++) { puts(str[i]); cout<<"\n"; } getch(); } void main() { char inputstr[100]; gets(inputstr); splitString(inputstr); }
If u can provide me with wat u have done, that will be help ful.
Thanks.
I strongly suggest that you try Narue's option. What didn't work with that code?
Where to start..
- don't use
- don't use
- don't use goto. A loop can always do what goto does.
- don't use
- instead of
- if you use
regards Niek
•
•
•
•
If u can provide me with wat u have done, that will be help ful.
- don't use
void main() . 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 fgets() - 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. regards Niek
Last edited by niek_e; Nov 14th, 2007 at 7:25 am.
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.
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.
Don't say
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
Don't use fgets() (or, *gasp*, gets()). Use
int main( void ) either. The main() 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. ![]() |
Similar Threads
- csv file split (Python)
- Server exception when opening a excel file in VB.net (VB.NET)
- Creating login screens in VB6 (Visual Basic 4 / 5 / 6)
- tokenization of file input (Java)
- To split or not to split? To substring or not to substring? (C#)
- Something about String.split("-"); problem (Java)
- [help] Split word in Java (Java)
- Help with programming assignment (Python)
- Monitor split vert. red (Monitors, Displays and Video Cards)
Other Threads in the C++ Forum
- Previous Thread: Calculating Time in sec
- Next Thread: I'm stumped, please help
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code coding compile console conversion convert count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






