Other Types
GOALStore decimals, letters, words and true/false.

Not everything is a whole number. Each box must know what kind of thing it holds.
ON THIS PAGE
//THE EXAMPLE
#include <iostream>
using namespace std;
int main() {
int count = 7;
double price = 19.5;
char grade = 'A';
bool passed = true;
string name = "Saraa";
cout << name << " got " << grade << endl;
cout << "Price: " << price << endl;
cout << "Passed? " << passed << endl;
return 0;
}Saraa got A Price: 19.5 Passed? 1
//LINE BY LINE
Watch it run
An int box has a fixed edge. big = 2 000 000 000 only just fits inside it.
Whole numbers
An int holds numbers between about ±2 billion. For anything bigger use long long.
| Type | Range |
|---|---|
int | ≈ −2 000 000 000 … 2 000 000 000 |
long long | ≈ ±9 000 000 000 000 000 000 |
unsigned int | 0 … ≈4 000 000 000 (сөрөг үгүй / no negatives) |
Going past the limit makes the number wrap around to a negative. Use long long when products get large.
int big = 2000000000;
big = big + big; // хэтэрлээ — хог утга
long long safe = 2000000000;
safe = safe + safe; // ✓ 4000000000Numbers with a decimal point
A double holds numbers with a decimal point: 3.14, -0.5, 2.0. There is also float, but it is less precise — prefer double.
double price = 19.99;
double half = 7 / 2.0; // 3.5
cout << half << endl;3.5
Decimals are stored approximately, not exactly. So 0.1 + 0.2 == 0.3 can be false. Never compare decimals with ==.
Booleans (bool)
A bool has just two values: true and false. Every comparison you write produces a bool.
bool isRaining = true;
bool passed = (score >= 60); // харьцуулалтын хариу
cout << isRaining << endl; // 1 гэж хэвлэнэ
cout << boolalpha << isRaining;// true гэж хэвлэнэC++ prints a bool as a number: true is 1, false is 0. Use boolalpha if you want the words.
Single characters (char)
A char holds one character. It uses single quotes: 'A'. Double quotes "A" make a string, not a character.
char grade = 'A';
char digit = '7';
cout << grade << endl; // A
cout << (int)grade << endl; // 65 — ASCII дугаарA 65
Every character is really a number underneath (its ASCII code). So 'a' + 1 gives 'b', and '5' - '0' gives the number 5.
Which type should I use?
| What you are storing | Type |
|---|---|
| Хүний нас, тоо ширхэг / a count or age | int |
| Маш том тоо / a very large number | long long |
| Үнэ, дундаж, хэмжээ / a price or average | double |
| Тийм/үгүй / yes or no | bool |
| Нэг үсэг / one letter | char |
| Үг, өгүүлбэр / a word or sentence | string |