944,145 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1841
  • C++ RSS
Mar 20th, 2006
0

<string>

Expand Post »
i input a sentence " i love eat".how can wanto to get c++ to get a full sentence..

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5. void echo(string message)
  6. {
  7. cout << message << endl;
  8. }
  9. int main()
  10. {
  11.  
  12. cout << "Enter your word: ";
  13. string word; // place declaration near its use
  14. cin>>word
  15. echo(word);
  16.  
  17. }

but i only get the first word,,how to get the sentence..
i try to use getline(cin,word) but error show up
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
amen is offline Offline
17 posts
since Feb 2006
Mar 20th, 2006
0

Re: <string>

The insertion operator >> stops at the first space. use getline() to get a whole sentence.
C++ Syntax (Toggle Plain Text)
  1. getline(cin,word);
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Mar 20th, 2006
0

Re: <string>

i had do like this
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5. void echo(string message)
  6. {
  7. cout << message << endl;
  8. }
  9. int main()
  10. {
  11.  
  12. cout << "Enter your word: ";
  13. string word;
  14. getline(cin,word,'\n');
  15. echo(word);
  16.  
  17. }

can i use strtok to token the string
Reputation Points: 10
Solved Threads: 0
Newbie Poster
amen is offline Offline
17 posts
since Feb 2006
Mar 20th, 2006
0

Re: <string>

>can i use strtok to token the string

I wouldn't, use some fancy c++ command. Leave 'C' commands where they belong in 'C'.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Mar 20th, 2006
0

Re: <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:
C++ Syntax (Toggle Plain Text)
  1. char *tstr = new char[line.size() + 1];
  2. char *tok;
  3.  
  4. std::strcpy ( tstr, line.c_str() );
  5.  
  6. tok = std::strtok ( tstr, delim );
  7.  
  8. while ( tok != 0 ) {
  9. // Process tok
  10. tok = std::strtok ( tstr, 0 );
  11. }
There are better ways than resorting to the C library.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 21st, 2006
0

Re: <string>

but how do i want to compare each token in string with word in text file in c++?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
amen is offline Offline
17 posts
since Feb 2006
Mar 21st, 2006
0

Re: <string>

you can use std::string's find method to locate the spaces
C++ Syntax (Toggle Plain Text)
  1. int main()
  2. {
  3. std::string line = "Hello World";
  4. std::string word;
  5. int pos;
  6. while( line != "")
  7. {
  8. pos = line.find(' ');
  9. if(pos >= 0)
  10. {
  11. word = line.substr(0,pos);
  12. line = line.substr(pos+1);
  13. }
  14. else
  15. {
  16. word = line;
  17. line = "";
  18. }
  19.  
  20. // now word is just one word
  21. cout << word << endl;
  22. }
  23.  
  24. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Mar 22nd, 2006
0

Re: <string>

>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)?
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Class with dynamic array how?
Next Thread in C++ Forum Timeline: Data loss in Type Conversion.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC