| | |
<string>
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2006
Posts: 17
Reputation:
Solved Threads: 0
i input a sentence " i love eat".how can wanto to get c++ to get a full sentence..
but i only get the first word,,how to get the sentence..
i try to use getline(cin,word) but error show up
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> using namespace std; void echo(string message) { cout << message << endl; } int main() { cout << "Enter your word: "; string word; // place declaration near its use cin>>word echo(word); }
but i only get the first word,,how to get the sentence..
i try to use getline(cin,word) but error show up
The insertion operator >> stops at the first space. use getline() to get a whole sentence.
C++ Syntax (Toggle Plain Text)
getline(cin,word);
•
•
Join Date: Feb 2006
Posts: 17
Reputation:
Solved Threads: 0
i had do like this
can i use strtok to token the string
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> using namespace std; void echo(string message) { cout << message << endl; } int main() { cout << "Enter your word: "; string word; getline(cin,word,'\n'); echo(word); }
can i use strtok to token the string
>can i use strtok to token the string
Not without first copying the string to an array. The c_str member function gives you a pointer to const char, and strtok modifies the string you pass to it. To do it right, you would end up with something like this:
There are better ways than resorting to the C library.
Not without first copying the string to an array. The c_str member function gives you a pointer to const char, and strtok modifies the string you pass to it. To do it right, you would end up with something like this:
C++ Syntax (Toggle Plain Text)
char *tstr = new char[line.size() + 1]; char *tok; std::strcpy ( tstr, line.c_str() ); tok = std::strtok ( tstr, delim ); while ( tok != 0 ) { // Process tok tok = std::strtok ( tstr, 0 ); }
I'm here to prove you wrong.
you can use std::string's find method to locate the spaces
C++ Syntax (Toggle Plain Text)
int main() { std::string line = "Hello World"; std::string word; int pos; while( line != "") { pos = line.find(' '); if(pos >= 0) { word = line.substr(0,pos); line = line.substr(pos+1); } else { word = line; line = ""; } // now word is just one word cout << word << endl; } }
>but how do i want to compare each token in string with word in text file in c++?
How do you define a token? If it's nothing more than a whitespace delimited sequence of characters, the >> operator may be better suited to your problem because it tokenizes input in such a way by default. Why have a two step process (read a line, then tokenize it) when you have a one step process (read a sequence of words)?
How do you define a token? If it's nothing more than a whitespace delimited sequence of characters, the >> operator may be better suited to your problem because it tokenizes input in such a way by default. Why have a two step process (read a line, then tokenize it) when you have a one step process (read a sequence of words)?
I'm here to prove you wrong.
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: Class with dynamic array how?
- Next Thread: Data loss in Type Conversion.
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion 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 integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets






