Post by Square*** on Jul 3, 2008 2:34:19 GMT -5
Hey all,
Here is another tutorial on C++, explaining the string function (IN fact its a class but Ill use it as function here )
Enjoy!
Ok again, First the code and then Ill explain it:
(This code creates a program that will read the keyboard input)
Alright, as you can see this code uses an extra header-file (with the command #include in front of it).
The string header is used to store whole words.
If you dont get this below, check out the tutorial C++ Getting started.
Ok, there we go with the strings.
First we have to give our string a name. I used the name "Word".
Then we have to let the console throw out a sentence asking you to put in a word.
(Wanna know more about cout command? Check out C++ Getting started.)
Then you have to put in a word, of course.
Therefor we use the cin- command.
So when you type a word when the program asks for it, it will be stored in the string "Word".
Now to check the input we'll use a simple cout command.
In fact very simple, but watch the last part.
When you have the normal text output and you want to put out a string for example, you just simply put another two << operators and then the name of your string.
Followed by another 2 operators to inject the endline command (endl) which will end the line and get to the next one.
So you are done and you have made a program which will ask someone for a word.
It may look stupid but you will need it if you are learning C++.
Do you have any questions or want more examples? Just PM me or reply here
Kind regards,
Square
Here is another tutorial on C++, explaining the string function (IN fact its a class but Ill use it as function here )
Enjoy!
Ok again, First the code and then Ill explain it:
(This code creates a program that will read the keyboard input)
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string word;
cout << "Type a word to check our program";
cin >> word;
cout << "This is the word you typed: " << word << endl;
return 0;
}
Alright, as you can see this code uses an extra header-file (with the command #include in front of it).
The string header is used to store whole words.
#include <iostream>
#include <string>
If you dont get this below, check out the tutorial C++ Getting started.
using namespace std;
int main ()
Ok, there we go with the strings.
First we have to give our string a name. I used the name "Word".
string word;
Then we have to let the console throw out a sentence asking you to put in a word.
cout << "Type a word to check our program";
(Wanna know more about cout command? Check out C++ Getting started.)
Then you have to put in a word, of course.
Therefor we use the cin- command.
cin >> word;
So when you type a word when the program asks for it, it will be stored in the string "Word".
Now to check the input we'll use a simple cout command.
cout << "This is the word you typed: " << word << endl;
In fact very simple, but watch the last part.
When you have the normal text output and you want to put out a string for example, you just simply put another two << operators and then the name of your string.
Followed by another 2 operators to inject the endline command (endl) which will end the line and get to the next one.
So you are done and you have made a program which will ask someone for a word.
It may look stupid but you will need it if you are learning C++.
Do you have any questions or want more examples? Just PM me or reply here
Kind regards,
Square