Hello Guest, please login or register.
Did you miss your activation email?
Login with username, password and session length.

Pages: [1]   Go Down

Author Topic: Theforeshadower's C++ Tutorials... Lesson 3: Char, Strings, and Cin...  (Read 960 times)

0 Members and 1 Guest are viewing this topic.
Theforeshadower's C++ Tutorials... Lesson 3: Cha...
« on: October 23, 2007, 10:05:43 pm »
  • *
  • Reputation: +12/-2
  • Offline Offline
  • Gender: Male
  • Posts: 4849
So, in our last lesson we learned how to store variables in int and how to comment our code //.
You can also store letters and whole words with C++.

To store a letter,number, or symbol we use char.
char can only store ONE letter or number or symbol.

We would type something like this:
Code: [Select]
char one;
one =  'a';
First we declared a char that is named one.  Then we declared that one equals the letter a.
It must be in single quotes or the compiler will give you an error.  Double quotes is something we use to print phrases we don't want to store using our cout action.

We can also use cout to print our char.

Code: [Select]
char one;
one =  'a';
cout << one << endl;

You'll see that all that is printed is the letter a.

To store some symbols, you need to do a little more.
Double quotes is normal:
char ch = '"';

But, what if you want to store a single quote?
Then you would would do something like this:
char ch = ' \' ';

And finally, to do a backslash we do this:
char ch = '\\';

So now we now how to store letters.  What about whole words?  We don't want to store thousands of letters to make one word of course.

Instead, we use strings.

First, and most important, we must add this to the beginning of our program to use strings:
Code: [Select]
#include <string>
That allows us to use strings in our program.  Now to actually doing some strings.

Strings are called the same way as int or char:
Code: [Select]
string first;
first = "Why...This is my first string!";

So, we declared a string named first.  We then set first equal with the words "Why...This is my first string!".

Since these are full words and/or phrase, we have to treat them the same as cout and put double quotes around whatever we are storing.  We can also use our cout to print our string.

Code: [Select]
string first;
first = "Why...This is my first string!";
cout << first << endl;

So we can store words and letters...what now?

Do you ever want to make a program where you can have input from the user, such as their name?
We have a nifty little action that works opposite of cout.  It's call cin.

So, how do we get numbers from a user?  Like this:
Code: [Select]
int x;
cin >> x;
cout << x << endl;

You can see from that code we declared x.  We used cin thereafter.  Cin uses >> which means in is taking whatever is entered and storing them to x.  We then printed what is typed with cout.

Remember:INT can only store numbers...not letters.

We can do the same with char:
Code: [Select]
char ch;
cin >> ch;
cout << ch << endl;
Maybe you want to use char to get an answer to a multiple choice question?  You can use char to store whatever letter the user enters with cin.

Finally, strings.

Code: [Select]
string name;
cout << "What is your first name?" << endl;
cin >> name;
cout << " So, your name is " << name << endl;
I made the code like this so you could better understand why we might want to get a word from a user.
You have to have a different string for a last name.  If you had the user type in their full name, you'll see that only their first name would show.  That's because string with cin only stores everything before a space if a space is used.

So we know how to store letters,numbers,and symbols with char.
We also know how to store whole words and phrases with string.
Lastly, we learned how to get some input from the user with cin.

Mess around with everything you know.  Feel free to send me some of your lovely code if you have questions.

Later, I'll go more indepth with char,string, and cin.

Have fun coding!

~Theforeshadower
Logged
  • Super Fan Gamers!
Re: Theforeshadower's C++ Tutorials... Lesson 3:...
« Reply #1 on: October 24, 2007, 12:16:16 am »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1141
Just to remind you: when you create the other (more advanced) articles about strings/chars, you should mention string operations (like adding, copying, erasing, etc.), how to transfer string to int (like "13" becomes 13, or vice-versa), and how to deal with "char*" and "char[n]". Don't forget ;)
Logged
Pages: [1]   Go Up

 


Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved



Page created in 0.121 seconds with 41 queries.