<string>

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2006
Posts: 17
Reputation: amen is an unknown quantity at this point 
Solved Threads: 0
amen amen is offline Offline
Newbie Poster

<string>

 
0
  #1
Mar 20th, 2006
i input a sentence " i love eat".how can wanto to get c++ to get a full sentence..

  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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: <string>

 
0
  #2
Mar 20th, 2006
The insertion operator >> stops at the first space. use getline() to get a whole sentence.
  1. getline(cin,word);
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 17
Reputation: amen is an unknown quantity at this point 
Solved Threads: 0
amen amen is offline Offline
Newbie Poster

Re: <string>

 
0
  #3
Mar 20th, 2006
i had do like this
  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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: <string>

 
0
  #4
Mar 20th, 2006
>can i use strtok to token the string

I wouldn't, use some fancy c++ command. Leave 'C' commands where they belong in 'C'.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,622
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: <string>

 
0
  #5
Mar 20th, 2006
>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:
  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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 17
Reputation: amen is an unknown quantity at this point 
Solved Threads: 0
amen amen is offline Offline
Newbie Poster

Re: <string>

 
0
  #6
Mar 21st, 2006
but how do i want to compare each token in string with word in text file in c++?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: <string>

 
0
  #7
Mar 21st, 2006
you can use std::string's find method to locate the spaces
  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. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,622
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: <string>

 
0
  #8
Mar 22nd, 2006
>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)?
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC