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


Now, in order for this to work, someone would have to put in JUST there first and last names. So if someone inputed "John Doe" then firstname would be assigned "John" and lastname would be assigned "Doe". Now, if someone put in "John Doe Jr" then first name would be "John" and lastname would be "Doe" and "Jr" would go out somewhere, in never never land. Basically, it is lost, and it put in for any future cin's. So that is a shortcut, you just have to be careful about it. Basically, you can do that as many times as you want, just a space has to seperate each value. Well, that is one, there are two more that I think are REALLY important. I will cover those, wont be too hard. Now that we can add strings, we will want to know how to compare strings, and how to copy ( or assign ) strings. Lets do the compare thing first :)

When we compare strings, there is a helpful little function in string.h called strcmp. Lets take the prime example above, and incorperate that into another example.

char * firstname[30];
char * lastname[30];
char * fullname[]="";
int stringcomp;
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);
stringcomp = strcmp(firstname,lastname); // stringcomp will tell us about the strings
if(stringcomp == 0)
   cout<<" They are equal!"<<endl;
else if(stringcomp > 0)
   cout<<" Your First Name is bigger than your last name "<<endl;
else
   cout<<" Your last Name is bigger than your first name "<<endl;


Now, we already know what this does, except for the strcmp part. This function, will take the two strings that you want to compare. strcmp will return a value to tell you which one is bigger or what. We are storing this value in stringcomp. If the value returned, stringcomp, is EQUAL to 0 ( zero ) then the two strings are equal. If stringcomp has a value greater than zero, the the first string is greater, and if stringcomp is less than zero, the second string is greater. And that is all there is too that. Then we just compare the value in stringcomp to see which is greater. Now, I bet you are wondering how the strings are compared. If you are, read the next paragraph, if not, just skip it.

Now, when you look up a word in the dictionary, you do it letter by letter, and when you compare a string, it does it in a similar fashion. They will compare the first two letters, and depending on that, it will compare the next two, and so on and so on. Now, I bet the next question in your head is " How do the computers do that ?" Well, in a computer, it handles numbers, and numbers only. And in order for the computer to compare letters, each letter is assigned what is called an ASCII value. ASCII stands for American Standard Code for Information Interchange. Now, what does that mean ?? That means that for ever character possible to input, it has an according ASCII value, which range from 0-255. So for instance, if I were to compare the letter 'A' and the letter 'f', I would do this if('A' > 'f'){ } Now, the compiler would not see that, they would see this if(65 > 102){ }. The code for a capitol A is 65, and the code for a lower case f is 102. Now, I bet your wondering, how can I do that neat trick, lets cover that in the next paragraph.

Now, if you remember, not to long ago, I introduced what was called Type-Casting. When we typecast a int to a double, we just add a decimal, and when we typecast an double to an int, we get the whole number, with the decimal part gone. Now, if I did this

int tempval;
tempval = int('A');
cout<<tempval;


The number 65 would be outputed. And thats all we do, just take a character, and type cast it to an int. Now, we do it the other way, just reversed, to an int. Like so:

int numone;
char letone;
numone = 65;
letone = 'f';
numone = int(letone); // numone is now 102
numone = 65;
letone = char(numone); // letone is now 'A'
cout<<" Numone: "<<numone<<endl;
cout<<" Letone: "<<letone<<endl;


And that is that. Nothing more to say about that really. Now, we are on to our last part of this basic string class. We are going to learn how to copy strings. Now, it would be nice to have something like this:

char *firstname[] = "Your First Name Here";
char *lastname[] = "Your Last Name Here";
strcpy(firstname,lastname);
cout<<" Last Name: "<<firstname<<endl;
// This is outputed
// Last Name: Your Last Name Here


Now, this is not to hard, all we do is copy one string into another. Now, lastname is not changed, just firstname is. It will copy whatever is in lastname into firstname. Thats all there is too it. And with that, we will end this final string lesson. This was not a hard lesson, it was just a different way of looking at things. One day, you can write your own string class, which is a project that we will do at one point.


C++ - File I/O


And we are back again, with another fun lesson in C++. Now, so far, we have learned a good deal of C++ basics, and now that we have the basics ( most of them ) out of the way, we can get into some things that are a little more fun, for instance, File IO. Now, I bet the first thing on anyone's mind is, " What is File IO? ". Well, I think knowing what we are going to learn would be a good place to start right ?

Lets think for a minute, we all know what a file ( meaning a file on a computer ) is right ? Well, how do those files get there ? Well, if its a text file of some sort, you made it with a program right ? But there are TONS of files on your computer that you yourself did not make. So who put them there ? Other programs did, and that is what file io is. We can get information from a file, and we can write information to a file. What it means is File Input/Output. We are inputting a file, and outputting to a file.

Now, when we go to write this lovely stuff, we, yet again need the help of another library, another header file.
#include <fstream.h>
Now, the header file fstream ( stands for File Stream ), contains some classes and stuff for us to be able to write to a file, and receive information from a file. Now, what is a stream you ask ? Well, the next paragraph will cover that, then we will get into some coding :)

Think of a electric cord, the kind from your computer to the wall. That is a prime example of a stream. Basically, its like a channel for information to go through. The electric cord has electricity going through it, now, it does not go anywhere else, but right to the computer. Now, when we have a stream in the computer, we have a direct " connection ", or a " channel " to send data to and from the other side.

NOw that we know what a stream is, lets make a little more sense of it, before we continue on. When we were doing cout and cin, those were streams also. Thats what the #include <iostream.h> means. iostream means input/output stream. That is a stream between the display adapter ( or monitor ), and the program. Now, we have a fstream, for files, that we will have a stream between the program and files on the computer. Now, lets get into some simple coding, and then talk about it a little.

Now, unlike the cout and cin, we have to declare variables. Now, the first thing we want to do, is decide, what do we want with the file. Do we want to get some data from it ( input stream ) or d owe want to write some data to it ( output stream ) ? I think to start off, it will be easier to explain output stream. When we want to declare that, there is the data type of ofstream, meaning output-file-stream. This is all we do to start of:

ofstream OutFile; // OutFile will be the output stream

There we go, we now have a stream, called OutFile. Now, we just did that, what good is it unless we can have fun with the file ? So, now is when we tell the computer what file we want to open, and thats done with the method of open(). Lets take a look:

OutFile.open("user.dat"); // Opens file user.dat for our output stream

Now, this is basicallxy saying, that we want our stream to connect between the program, and

Page : << Previous 6  Next >>