Post by Square*** on Jul 11, 2008 5:21:12 GMT -5
Hey guys,
Here is another tutorial for C++
This one is about more function for the class String.
As you can read in the earlier tutorials, strings are used to read an console input which include a word.
In this tutorial I will also explain how you can read a whole line (sentence) from input.
Alright let's start with the reading of a whole sentence,
First the code, then explanation
Well, that's the code, now the explanation:
This pieces of code you know right now, Im not going to explain them. If you still don't get them, go to the first C++ Tutorial in the Tutorial index.
First we give our string the name "sentence".
Then we create a console output asking to typ a sentence.
Easy, right?
Here is the most important part:
"getline" is the command here, after that (in the () ) are the arguments we need.
The "cin" argument works as a normal "cin" it will read the console input.
The argument "sentence" will tell the program to store the sentence that's typed into the string sentence.
After this you can easily let the console tell what sentence was typed
That wasnt hard, was it?
Now I will explain how you can read a input without letting the person who types hit "Enter/return".
The following code will ask the person to typ some text, then it will red the first letter that was typed.
The code :
A new type like "int" and "string", "char" is used to store 1 character or the code for that character.
Assigning a name to it works the same (If you want more than 1 word in the name us the _ or let every new word start with a capital character like A).
What we do here, is instead of declaring a character or charactercode, we use a command.
So this will tell the program to store the first characterinput into the character some_letter.
(Why dont I use "some_character" as name? Its not very handy and you can see more easily what are the names you created yourself. And if you use "some_char" the compiler might think the type char is used again.)
Now, the rest is easy:
Here we create an output to the console telling the person what the first character he typed was.
Now some other things about the class "string".
The class "string" has a lot of methods you can use in it.
I'll explain some of them below.
Ill use this sentence -> "Hello all programmers"
The following code will:
- Tell you how long the string is
- Tell you where the word programmers will start
- Tell you on what position the last "e" was used
Code:
Ok, Here you see the sentence I used. I declared it directly behind the function between (). (Be sure to use " " otherwise it wont work).
I gave the string the name "sentence1" so we can use it later.
First Method (length):
As you can see I took the name of the string and then I use a ".'
to include a method. This method will tell you how long the string is.
You have to put two empty () behind it so the console can fill in how long the sentence is.
Next one (find):
Here I putted a "." behind the name again but with some other method. (Find). The method "Find" will search in the sentence for a word you declared in the (). In this case thats "Programmers".
And with a simple "cout" it will tell you at what position the word "Programmers" start.
Another one (find_last_of):
This one works the same as the "find" method above, except this one will search for the last position a character was used. In this case the "e".
NOTE: You can see I used \'e\' and later in the "find_last_of" method ('e'). This is needed to tell the compiler that you are using a character and not some sort of command or pointer.
And that was it for this tutorial. I hope you guys learned something about it and you can always ask my for help.
Of course Ill try to answer as many questions as I can.
Regards,
Square
Here is another tutorial for C++
This one is about more function for the class String.
As you can read in the earlier tutorials, strings are used to read an console input which include a word.
In this tutorial I will also explain how you can read a whole line (sentence) from input.
Alright let's start with the reading of a whole sentence,
First the code, then explanation
#include <iostream>
using namespace std;
int main ()
{
string sentence;
cout << "Typ a sentence, please ";
getline (cin, sentence)
cout << "This is the sentence you typed: " << sentence <<
endl;
return 0;
Well, that's the code, now the explanation:
#include <iostream>
using namespace std;
int main ()
return 0;
This pieces of code you know right now, Im not going to explain them. If you still don't get them, go to the first C++ Tutorial in the Tutorial index.
string sentence;
cout << "Typ a sentence, please ";
First we give our string the name "sentence".
Then we create a console output asking to typ a sentence.
Easy, right?
Here is the most important part:
getline (cin, sentence)
"getline" is the command here, after that (in the () ) are the arguments we need.
The "cin" argument works as a normal "cin" it will read the console input.
The argument "sentence" will tell the program to store the sentence that's typed into the string sentence.
After this you can easily let the console tell what sentence was typed
cout << "This is the sentence you typed: " << sentence<< endl;
.That wasnt hard, was it?
Now I will explain how you can read a input without letting the person who types hit "Enter/return".
The following code will ask the person to typ some text, then it will red the first letter that was typed.
The code :
#include <iostream>
using namespace std;
int main()
{
char some_letter;
some_letter = cin.get();
cout << "This is the first letter you typed: " << some_letter <<
endl;
return 0;
}
Explanation:
[code]char some_letter;
A new type like "int" and "string", "char" is used to store 1 character or the code for that character.
Assigning a name to it works the same (If you want more than 1 word in the name us the _ or let every new word start with a capital character like A).
some_letter = cin.get();
What we do here, is instead of declaring a character or charactercode, we use a command.
So this will tell the program to store the first characterinput into the character some_letter.
(Why dont I use "some_character" as name? Its not very handy and you can see more easily what are the names you created yourself. And if you use "some_char" the compiler might think the type char is used again.)
Now, the rest is easy:
cout << "This is the first letter you typed: " << some_letter <<
endl;
Here we create an output to the console telling the person what the first character he typed was.
Now some other things about the class "string".
The class "string" has a lot of methods you can use in it.
I'll explain some of them below.
Ill use this sentence -> "Hello all programmers"
The following code will:
- Tell you how long the string is
- Tell you where the word programmers will start
- Tell you on what position the last "e" was used
Code:
#include <iostream>
using namespace std;
int main ()
{
string sentence1 ("Hello all programmers.");
cout << "Length of the string: " << sentence1.length() << endl;
cout << "Position of the start of programmers: " sentence1.find
("programmers")
cout << "Last position of the character \'e\' : " <<
sentence1.find_last_of('e') << endl;
return 0;
}
Explanation:
[code] string sentence1 ("Hello all programmers.");
Ok, Here you see the sentence I used. I declared it directly behind the function between (). (Be sure to use " " otherwise it wont work).
I gave the string the name "sentence1" so we can use it later.
First Method (length):
cout << "Length of the string: " << sentence1.length() << endl;
As you can see I took the name of the string and then I use a ".'
to include a method. This method will tell you how long the string is.
You have to put two empty () behind it so the console can fill in how long the sentence is.
Next one (find):
cout << "Position of the start of programmers: " sentence1.find
("programmers")
Here I putted a "." behind the name again but with some other method. (Find). The method "Find" will search in the sentence for a word you declared in the (). In this case thats "Programmers".
And with a simple "cout" it will tell you at what position the word "Programmers" start.
Another one (find_last_of):
cout << "Last position of the character \'e\' : " <<
sentence1.find_last_of('e') << endl;
This one works the same as the "find" method above, except this one will search for the last position a character was used. In this case the "e".
NOTE: You can see I used \'e\' and later in the "find_last_of" method ('e'). This is needed to tell the compiler that you are using a character and not some sort of command or pointer.
And that was it for this tutorial. I hope you guys learned something about it and you can always ask my for help.
Of course Ill try to answer as many questions as I can.
Regards,
Square