C++ Standard I/O and strings
In this tutorial we take a look at standard I/O and strings. The string statements used in this tutorial are basic. Later on we will take another look at strings. (We first need to explain some other topics like: control loops, pointers, etc).
cin and strings
In the last tutorial we already covered the use of cin and cout. Let’s take another look at a cin and cout example:
#include<iostream>
using namespace std;
int main()
{
int My_int;
cout << "Give a integer: ";
cin >> My_int;
cout << "Your integer is: " << My_int << "\n";
}
Note: Remember, if you ask for an integer, cin will return an integer. (Example: input of 1.2 (a float) then cin will return 1 . The .2 will be dropped).
To make use of strings we have to include the string library (#include <string>). A string variable can be declared like this: string mystr; Let’s take a look at a string example:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str_myname;
cout << "What's your name? ";
cin >> str_myname;
cout << "Hello " << str_myname << "\n"; //or use ‘\n'
return 0;
}
Note: the use of str_ in the name of our string str_myname. The use of str_ in string names make it easier for us to identify a string in a large program. (Remember, it is not necessary to use str_ , but it makes things easier).
But there is a problem in this example. The problem is, that cin stops reading as soon as it encounters a blank space character. So if your input is “jane” there is no problem. If your input is “jane grey” only the word jane will be read. This behavior can also be useful.(Example: if the question was “What is you surname?” in most cases the surname is just one word). In this case the question is “What’s your name?”. The user could answer with just his given name or the user could answer with his given name, middle name and surname. We need a function that can read one line instead of one word.
Strings and getline
This is where the function getline comes in. The getline function can be used if you need to read an entire line at a time.You can use the getline function and pass in an input stream object, such as cin (to read from standard input) or a stream associated with a file (reading lines from a file). A getline statement (using cin) will look like this: getline(cin, <your_string_variable>);
Take a look at an example:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str_mystr;
cout << "What is your name? ";
getline (cin, str_mystr);
cout << "Hello " << str_mystr << '\n';
cout << "What is your favorite city? ";
getline (cin, str_mystr);
cout << "I like " << str_mystr << '\n';
return 0;
}
Note: The string variable str_mystr can be used several times. When str_mystr is used the second time, the old content of str_mystr is replaced by the new input value.
If you execute the example you will see that everything will be “recorded” until a new-line (return) is given. It is also possible to use a third parameter with getline which is a character on which to terminate input. So the getline statement will look like this: getline(cin, <your_string_variable>, <terminate_input_character>);
Take a look at an example:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str_mystr;
cout << "What is your name? ";
// Input something like this:
// jane, hit return, grey#, hit return.
getline (cin, str_mystr, '#'); //The terminate character is #
cout << "Hello " << str_mystr << '\n';
return 0;
}
Note: Everything will be “recorded” until the terminate character # is found. (It is still necessary to hit return). It is possible to use the new-line character \n .
As you can see the terminate character can be very useful (if it is used in the right context, like a parser for example). But you have to be careful when you use terminate characters and the same string variable in multiple getline statements.
Try this example:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str_mystr;
cout << "What is your name? ";
getline (cin, str_mystr, '#');
cout << "Hello " << str_mystr << '\n';
// cin.ignore();
cout << "What is your favorite city? ";
getline (cin, str_mystr);
cout << "I like " << str_mystr << '\n';
return 0;
}
You will see that you won’t get a chance to give a second input sequence. The problem is that the second getline statement will process the new-line that is still in the input buffer (str_mystr becomes an empty string). The solution to this problem is to make use of cin.ignore(); immediately after the first std::cin statement. This will grab a character off of the input buffer (in this case, new-line) and discard it. Just remove comment slashes (//) and execute the program a second time.
Joining strings
It is possible to use the + operator to append two strings.
For example:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str_first;
string str_sec;
string str_third;
cout << "What is your given name? ";
getline (cin, str_first);
cout << "What is your surname? ";
getline (cin, str_sec);
// append a " " to the second string or
// jane + grey becomes janegrey
str_third = " " + str_sec;
//Swap the strings
str_sec = str_third;
//Join the two strings
str_third = str_first + str_sec;
cout << "Hello " << str_third << '\n';
return 0;
}
It is also possible to use compound assignments like the += operator. String concatenation will work as long as either of the two strings is a C++ string (the other string can be a char* or a static string).
That’s all for this tutorial.We encourage you to make your own programs or to make changes in the example programs. This is the only way you can learn programming C++. Make little programs, the more the better.
the joining strings is really confusing
@jim
Appending (joining) strings isn’t that hard. Maybe that the example confuses you, so take another look at the following example:
#include<iostream>
#include<string>
using namespace std;
void main(void){
string a;
string b;
string output;
// Fill the two strings
a = “Hello”;
b = ” World!”;
// Normal append of two strings
output = a + b;
cout << output << endl;
// Or without the output string variable
cout << (a + b) << endl;
// Using compound assignments += operator
output = a;
output += b;
// In fact you are saying output = output + b;
// So if you fill the string variables you get:
// Hello + World! = result in output.
cout << output << endl;
}
I hope that this joining strings example is somewhat easier to understand.
this tutorial gives me a good understanding of strings, thanks a million.
excellent execution of this kind of tutorial