I have another question but I don't want to make a new topic for it.
When defining variables you can use int for integers and string for "text"
The string variable can hold text (ie letters) and numbers but int can only hold numbers
Why would you want to use int.
Say i was asking for an age one person might type it in as a number like 12 but someone else might type in the actual word like twelve
Why would you want to use int instaed of string
Does string have any limitations in the program or is it just bad programing.
Well, programmers like to use data types responsibly. This kind of issue comes up because you always get input from the user in string format, and in order to have a real -number- a conversion must take place.
There are times when leaving numbers in string format is easier or best: like dates, or IP addresses, but for 90% of the time you want to store a number in a number type because:
- doing math is easy
- the number is represented in a compact format
- it's easier to check to see if the numerical data is an error itself, like your age program showed
One more thing - strings can handle numbers as well. For
example, you can declare a string variable and use it for a
numerical selection (1, 2, 398, 10 etc). The thing is, the string
data type handles characters - which themselves are a data
type (look up char somewhere). A string is merely a group of
The problem is, that if you used a string (or a single char), the
numbers you enter are in a completely different format than
if the data type was an integer. This is due to ASCII, the standard
encoding for characters. Characters are typically represented
internally by 1 byte (allowing for 0-255 unique values). The
problem lies with the fact that the equivalent ASCII "code" for
the number 0 doesn't start at 0 - it is 48 (obviously not the
direct binary equivalent).
This can cause all sorts of problems - such as performing math
operations, or even representing numbers with 2 digits or more.
So we have integers - which represent the numbers we enter in
their binary equivalent. We can do math operations and represent
numbers bigger than 9 using their direct binary equivalent.
There are also differences in performing comparisons,
assignments/initialisations, and so on. Take a look at this
brief example and not the highlighted difference. Run it and
see a huge difference in the output for the same number.
Lastly, characters, integers and strings all have different sizes in
using namespace std;
int main (void)
int a_num = 9;
char a_char = '9';
string a_string = "9";
cout << a_num + a_num << endl; //performing normal addition
cout << a_char + a_char << endl; //outputs sum of their decimal equivalent codes
cout << a_string + a_string << endl; //Results in appending one string onto another
memory - as I said, a char is typically 1 byte, and string is actually
a class - which means that there is a lot more than meets the
eye - much larger than a char or an int.
Wow there is a huge difference in the numbers that it returned.
the int was right and said 18, I don't have a clue wat char did and string just added them together in a line instead of adding them
Well like i said, the integer is the best way to represent real
numbers (well it won't handle decimal fractions, but there's also
a float type, and a double type which handle them). The ASCII
value of character 9 is 57. This means the in binary, if the data
type is char, the 9 looks like 00111001. Adding it to itself
produces 114 which is actually the character 'r', but the decimal
equivalent value is outputted. Then with the strings, the + operator
appends one string onto another, resulting in a string containing
two characters - 9 and 9 (strings are groups of characters, remember?)