|
-
February 27th, 2013, 05:51 PM
#37
Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).
(http://www.cplusplus.com/doc/tutorial/variables/) covers it. However, lets keep this simple for the moment. There are basically 3 different types of in-built c++ identifiers.
int is used when you want to store integers (whole numbers with no decimal part) eg 2, -4, 456 etc.
double is used when you want to use numbers with a decimal part eg 3.456, 4.78, -2.45 etc
char is used when you want to use a single character eg 'M', 'T', 'a' etc.
so we can do
Code:
int a;
double b;
char c;
a = 2; //a is defined to be an integer so assigning 2 is OK
b = 4.546; //b is defined to be a double so can hold numbers with a decimal part
c = 'j'; //c is defined to be a char so can hold 1 character
There are variations on these (like signed, unsigned, long etc) as you can see from the article but ignore these for the moment until you have mastered the basics.
The next issue is with strings. A c-style string is an array of characters terminated by the NULL character (NULL or \0). For the moment, though, I'd pass on this until you understand the others.
A string literal are characters delimited by ". So "qwerty" is a string literal and so is "string literal".
c++ also has what's called the string class. This is much easier to use than c-style strings and should be used where possible for strings. "phoenix" is a string literal so needs to be assigned to a string variable.
Code:
#include <string>
#include <iostream>
using namespace std;
string s1;
s1 = "phoenix";
cout << "String s1 is " << s1 << endl;
The type of variable you use depends upon what you want it to hold. Once you know for what you are going to use the variable, the type can then be chosen to match (ie int, double, char or string). Once you are comfortable with choosing and using these then you can start looking at the variations and c-style strings (which can cause problems if not used carefully).
Hope this helps. Ask if you don't understand this because this is fundamental to c++ programming and until you do you'll have problems trying to go further.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|