Topic : C++ Tutorial
Author : Unknown
Page : << Previous 5  Next >>
Go to page :


what would be an integer result, and converting it into a double. I can do the same thing backwards, I could:

int temp;
double a = 2.1, b = 3.4;
temp = int(a + b);
cout<<temp; // Value in temp is 5


And that is a short lesson on Type-Casting. Its not that hard, so I dont want to contribute a whole lesson it, so I kinda side tracked from our orginal problem. But the only thing left was to output the average score, which is held from the variable avg Now, the same rules apply when naming a variable with an array, the only difference is that you have to have a [] at the end, with a number in the box. Now, arrays are a big subject, one that will be broken up into multiple lessons. So I will leave you with this basic overview of arrays, then cover some more material in the next lesson, so until next time, PROGRAM ! :)


C++ String as Arrays


Now, I know that this is a C++ tutorial and we are going to stay with strictly ALL C++...except this...
If you think about it, an array can hold numbers, decimals and such. But if we have an array of char's, then we have what is called a C-Style String. Basically, its a string, and all a string is just a whole bunch of letters ( char ) put together. Now, when we learn about this, we have to include another library to do some things with them.

#include <string.h> // This is the library for string sub routines

Now, from this point forward, I am referring to an array of char's as string. Well, without further ado, lets continue

I think that a good place to start with strings are how to declare them. We do it very similiar to an array, and there are two different ways to do it the way that I am going to show you. Lets look at both declerations, then we will examine each.

Method One:

char * string[] = "Your Name Here";


Method Two:

char * string[15];

First thing I want to touch on is dont worry about the *. That has to do with pointers, which will be covered later on. For right now, just know that you have to have it there for a C-Style String. This variable string is declared two different ways. The first way char * string[] = "Your Name Here"; is basically saying that we have this string, dont know how big it will be ( the empty [] ) and then we are setting it equal to Your Name Here. Now, the length of string will automatically be set whatever the length of Your Name Here is. That would be 14 characters. Now, say we want to access just one letter, or character of that string, we would do what we did to get an element from an array. For instance, string[5] will be 'N', and string[8] would be 'e'. And so on, remember though, the same rule applies to strings as they do to arrays, the to get the last character of an string, you have to do (LENGTHOFSTRING - 1), where LENGTHOFSTRING is the length of the string. Now, one more thing that has to be brought up before we can get into the fun stuff. All C-Style Strings, end in a character, called the NULL character. That character is defined as '/0' ( which is back-slash zero ). So actually, when we have the string "Your Name Here", when the compiler sees it, it sees "Your Name Here\0". Thats all, its not important right now, but will be later on, so just remember that I brought it up.

Well, first thing that we want to do is be able to output and input strings. Thats a very common data type of input. To output a string, we just do what we normally do:

char *string[] = "Bob The Frog"; // Declaring a string
cout<<string<<endl; // Just outputs "Bob The Frog" to the screen


Thats it, just the variable name and then your done. Now, inputting a string is a tad bit more tricky, but not to hard to say the least. We can use cin just like a normal variable, or we can use a function getline which is declared in iostream.h. Lets examine the difference
cin - When we use cin to get a value from the user, it will store what is inputed up to the first whitespace ( or the first space ). So if I had to input a string, and I was using cin, then I could only send a string that had no spaces. For instance, lets say that I cin>>string and the user inputs "Hello There". What is stored in string is now "Hello". "There" is a value that is junk and can mess up future inputs. That is where getline comes in handy

getline - When we use getline, we can do the same thing, except we can have spaces in the input. There are about 3 ways to use getline, but I like to be consistent, so I will show you the one way that I use it. Lets say that we had a variable char *string[50]. This is how we would use getline - getline(cin,string);. Now, getline will get all the characters including spaces, up to, but NOT including the carriage return, which is the character that is sent when you hit the [ENTER] key. So, know you are asking, where does that character go ???. Well, that is why we need something called a " Dummy Variable ". That is the actual term for it I think. We need a char variable to store that carriage return. So lets look at this code segment.

char * name[30];
char dummyvar;

cout<<" Input your FULL name: ";
getline(cin,name);
cin>>dummyvar;
cout<<" Welcome to my program "<<name<<endl;


Now, granted, that is not a very effective way to do that, that is a waste of memory. That is why I am going to introduce a member function for istream. First off, istream is just the input stream, like cin. Now, one of these member functions are called ignore(). When we use getline, just like I said, it will get all the characters up to, but NOT including the carriage return character. Now, if we use cin.ignore();, that will "ignore" the carriage return and we can simply continue with the program as before. Not to hard, and then all we have to do to change the source above, is this:

char * name[30];

cout<<" Input your FULL name: ";
getline(cin,name);
cin.ignore(); // Ignore the '/n' cout<<" Welcome to my program "<<name<<endl;



And that is that. Now, if you making a large scale program, in which case I doubt that you would use cin, but that is up to you. If you wish too, you can use a dummyvariable, or just cin.ignore(); Either way is not going to siginificantly change your programs speed. Ok, now that we got the Input/Output of a string down, lets actually use the <string.h> stuff hunh ? Well, first off, lets say that we have two strings, first name and last name, and we want to get there full name. We can use a free-function called strcat. Basically, this is string concatenation. All concatenation is when we combine two strings. For instance "FirstName" + " LastName" = "FirstName LastName". That is an example of concatenation, however, we cant do it like that with C-Style Strings, but strcat does that for you. Lets look at a brief example:

char * firstname[30];
char * lastname[30];
char * fullname[]="";
cout<<" Enter in your first name: ";
cin>>firstname;
cout<<" Enter in your last name: ";
cin>>lastname;
strcat(fullname,firstname);
strcat(fullname,lastname);
cout<<" Welcome "<<fullname<<endl;


Now, that is a prime example of how we concatenate a string. As you see, we first declare two variables to hold the first and last name, then a empty string variable to hold the first name. We then get the user's first and last name ( HINT: Any spaces in input will screw it all up ). Then we call strcat. We tell what we want to source string to be, and what string we want to add to it. Same with the last name, now, fullname, before the second call to strcat, only contains the first name, so we have to add the last name to that string, and not over write it. And after that call, fullname now contains the fullname ( duh ). Now, there is one trick that I might add to this. This has nothing to do with string concatenation, but, it is one that could be helpful. Remember how earlier we could use the << operator however many times we wanted to output stuff ? Well, we can do the same with the >> operator with cin. Lets re-do the example above with the new trick, then examine it.

char * firstname[30];
char * lastname[30];
char * fullname[]="";
cout<<" Enter in your first and last name: ";
cin>>firstname>>lastname; // Will get both names wherever space is at
strcat(fullname,firstname);
strcat(fullname,lastname);
cout<<" Welcome "<<fullname<<endl;


Thats it.

Page : << Previous 5  Next >>