C++ For All You Beginners: Part Four
Filed in archive by Creative Weblogging on July 3, 2006
The last tutorial was based on strings and whatnot. But, there was a big part I did forget; variables and their definers.
Int and char are not the only variables on the C++ market. In fact, those are a whole bunch more. Today I would like to show you some of them, and possibly give examples.
Here are the variables I will briefly explain:
- 'int': Simple counting number; sometimes used to define more than just numbers, but not for strings.
- 'unsigned int': A positive (integer), including zero.
- 'long': Different, yet similar, version of 'int'. But, there is no difference between these two in the compiler (Dev-C++) we are using.
- 'unsigned long': A positive long (integer) including zero.
- 'float': This is what I mainly would like to focus on today. Float is what defines how many decimal places a number goes out; for example: Our currency is extended two (in Banks' cases, sometimes three) places after the period. Precision is the key here. Let me show you what I mean:
#include "iostream"
#include "cstdlib"
#include "cstdio"
using namespace std;
int main(int nNumberofArgs, char* pszArgs[])
{
float price;
float gas;
cout << "Enter how much gas you want: ";
cin >> gas;
cout << "\nEnter price per gallon: ";
cin >> price;
cout << "\nTotal: $" << price * gas;
system("PAUSE");
return 0;
}
What we're doing here is entering the price of gas (hopefully with two decimal places), and the amount of gas we want. If the program was correct, the outcome should be displayed with two decimal places. Of course, if we wanted to, we could make the number much longer. Back when memory was very valuable, float was a hassle, since it took up so much space. But these days, who honestly cares about a few bytes?
- 'double': This is basically like 'float', except it takes more memory and is even more accurate, with a bigger range too, than 'float'.
- 'bool': This is one of the coolest variables, to me at least. When you make an equation, like '1=1', and you include 'bool', the program will tell you if the equation is 'true' or 'false'. It's pretty neat.
This section is pretty vital to know, and I'm glad I caught myself early enough. You can apply these variables to make more complex and capable programs.
If you have any questions, or would like some more information regarding C++, please email me at: bjohanson1@msn.com.
The next tutorial will be some compiled miscellaneous stuff. Before long, we'll be working on Win32 applications.
The author is a registered user of Creative Reporter - join our blogging community today.
Permalink: C++ For All You Beginners: Part Four
Tags:
C++
Programming
creative
more
part
part+four
beginners+part
social+networking
Trackback: http://publish.creative-weblogging.com/publish/mt-tb.pl/

Mr Wong


