c++ Strings

c++ Strings - C+ S trings C provide a variabletypecalle...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
C++ Strings To use string variables you must add this to your program: #include < string > // put at the top of your program using namespace std; // or write: using std::string; C++ provides a variable type called string . Instead of holding numbers like int or double , string variables holds a string of character values .
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
#include <iostream> #include <string> using namespace std; int main(void) { string myname; myname = “Carey”; cout << “Hello, “ << myname; myname = “”; } C++ Strings You can define a new string variable just like you define any other C++ variable. You must use double quotation marks “ and “ when you assign a string variable to a string value . If you assign a string variable to the value “”, then you reset the string’s value to empty. Note: All string variables start out empty until they are set to a value. UNLIKE NORMAL VARIABLES!
Background image of page 2
#include <iostream> #include <string> using namespace std; int main(void) { string myname; cout << “Name? “; cin >> myname; cout << “Hello “ << myname; } C++ Strings You can input a string from the user using the standard cin >> command. BUT cin will only allow you to type in a single word at a time. If you type in more than one word, only the first word will be stored in your variable. myname Name? Alan Wang “Alan” Hello Alan
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
#include <iostream> #include <string> using namespace std; int main(void) { string myname; cout << “Name? “; cin >> myname; if (myname == “Carey”) cout << “You stud!\n”; else cout << “You slacker!\n”; } C++ Strings You can use the comparison operators on strings as well. As with char variables , comparisons are case sensitive. Name? CAREY myname You slacker! “CAREY”
Background image of page 4
#include <iostream> #include <string> using namespace std; int main(void) { string password; cout << “Password? “; cin >> password; if (password < “UCLA”) cout << “Too low…”; else if (password > “UCLA”) cout << “Too high!”; else cout << “Welcome, master.\n”; C++ Strings < <= > and >= comparisons are done lexicographically. “Alan” comes before “Aron”.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 20

c++ Strings - C+ S trings C provide a variabletypecalle...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online