C++ Tutorial/string/string read
Содержание
Read a string from keyboard and decide if to exit a while loop
<source lang="cpp">#include <iostream>
- include <string>
using namespace std; class Product { public:
Product(); void read(); bool is_better_than(Product b) const; void print() const;
private:
string name; double price; int score;
}; Product::Product() {
price = 1; score = 0;
} void Product::read() {
cout << "Please enter the model name: "; getline(cin, name); cout << "Please enter the price: "; cin >> price; cout << "Please enter the score: "; cin >> score; string remainder; /* read remainder of line */ getline(cin, remainder);
} bool Product::is_better_than(Product b) const {
if (b.price == 0) return false; if (price == 0) return true; return score / price > b.score / b.price;
} void Product::print() const {
cout << name << " Price: " << price << " Score: " << score << "\n";
} int main() {
Product best; bool more = true; while (more) { Product next; next.read(); if (next.is_better_than(best)) best = next; cout << "More data? (y/n) "; string answer; getline(cin, answer); if (answer != "y") more = false; } cout << "The best value is "; best.print(); return 0;
}</source>
Read string from keyboard and get substring
<source lang="cpp">#include <iostream>
- include <string>
using namespace std; int main() {
cout << "Enter your full name (first middle last): "; string first; string middle; string last; cin >> first >> middle >> last; string initials = first.substr(0, 1) + middle.substr(0, 1) + last.substr(0, 1); cout << "Your initials are " << initials << "\n"; return 0;
}</source>
Read string till a sign
<source lang="cpp">#include <iostream>
#include <string> using namespace std; int main(){ string full_name, nickname, address; string greeting("Hello, "); cout << "Enter your full name: "; getline(cin, full_name); cout << "Your full name is: " << full_name << endl; cout << "Enter your nickname: "; cin >> nickname; greeting += nickname; cout << greeting << endl; cout << "Enter your address on separate lines\n"; cout << "Terminate with "$"\n"; getline(cin, address, "$"); cout << "Your address is: " << address << endl; return 0; }</source>
Use cin in while loop to read string
<source lang="cpp">#include <iostream>
- include <string>
using namespace std; int main() {
int count = 0; string word; while (cin >> word) { count++; } cout << count << " words.\n"; return 0;
}</source>
Use cin to read string
<source lang="cpp">#include <iostream> using std::cout; using std::endl; using std::cin; using std::boolalpha;
- include <string>
using std::string; void display( const string & ); int main() {
string string1; cout << "Statistics before input:\n" << boolalpha; display( string1 ); cout << "\n\nEnter a string: "; cin >> string1; // delimited by whitespace cout << "The string entered was: " << string1; cout << "\nStatistics after input:\n"; display( string1 );
return 0;
} void display( const string &stringRef ) {
cout << "capacity: " << stringRef.capacity() << "\nmax size: " << stringRef.max_size() << "\nsize: " << stringRef.size() << "\nlength: " << stringRef.length() << "\nempty: " << stringRef.empty();
}</source>
Statistics before input: capacity: 0 max size: 1073741820 size: 0 length: 0 empty: true Enter a string: a string The string entered was: a Statistics after input: capacity: 1 max size: 1073741820 size: 1 length: 1 empty: false"