Advertisement
nher1625

basics

Sep 28th, 2015
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.25 KB | None | 0 0
  1. // This is a comment
  2.  
  3. // Win32Project1.cpp : Defines the entry point for the console application.
  4. //
  5. #include "stdafx.h"
  6. #include <iostream>  // use cout and other things...
  7. #include <vector>
  8. #include <string>
  9. #include <fstream>  // use file i/o
  10.  
  11. using namespace std;
  12.  
  13. int main()  // main container
  14. {
  15.     cout << "Hello World!" << endl;
  16.  
  17.     const double PI = 3.14159;  // constant vars cannot be changed and are typically in all caps
  18.  
  19.     char myGrade = 'A';  // char is for single characters (consumes 1 byte in memory)
  20.  
  21.     bool isHappy = true;  // 1 (true) 0 (false)
  22.  
  23.     int myAge = 22;
  24.  
  25.     float favNum = 666.666;  // accurate up to 6 decimal places
  26.  
  27.     double otherFavNum = 7777.484394843839;  // accurate up to 15 decimal places
  28.  
  29.     cout << "Favorite Number: " << favNum << endl;
  30.  
  31.     // other data types include
  32.     // short int: At least 16 bits
  33.     // long int: At least 32 bits
  34.     // long long int: At least 64 bits
  35.     // unsigned int: Same size as signed version
  36.     // long double: No less then double
  37.  
  38.     cout << "Size of int (bytes): " << sizeof(myAge) << endl;  // size of int in bytes
  39.  
  40.     int largestInt = 2147483647;  // largest integer we could possible have
  41.  
  42.     int five = 5;
  43.     cout << "5++ = " << five++ << endl;  // outputs 5 first because increment occurs after value fetch
  44.     cout << "++5 = " << ++five << endl;  // output 7 because of above as well as inrement occuring prior to value fetch
  45.     cout << "5-- = " << five-- << endl;  // output 7 value is really 6
  46.     cout << "--5 = " << --five << endl;  // output 5
  47.  
  48.     // Order of Operations states * and / before + and -
  49.     cout << "1 + 2 - 3 * 2 = " << 1 + 2 - 3 * 2 << endl;  // -3
  50.     cout << "(1 + 2 - 3) * 2 = " << (1 + 2 - 3) * 2 << endl;  // 0
  51.  
  52.     // type casting
  53.     cout << "4/5 = " << (float)4 / 5 << endl;
  54.  
  55.     // Comparison Operators: ==, !=, >, <, >=, <=
  56.     // Logical Operators: &&, ||, !
  57.  
  58.     int age = 70;
  59.     int ageAtLastExam = 16;
  60.     bool isNotIntoxicated = true;
  61.  
  62.     if ((age >= 1) && (age < 16)) {
  63.         cout << "You are not old enough to drive" << endl;
  64.     }
  65.     else if(!isNotIntoxicated) { cout << "You can drive if your blazed just not drunk dick" << endl; }
  66.     else if(age >= 80 && ((age > 100) || ((age - ageAtLastExam) > 5))) {
  67.         cout << "You can't drive old fuck" << endl;
  68.     }
  69.     else { cout << "You can drive!" << endl; }
  70.  
  71.     // switch cases
  72.     int greetingOption = 2;
  73.  
  74.     switch (greetingOption) {
  75.         case 1:
  76.             cout << "bonjour" << endl;
  77.             break;
  78.         case 2:
  79.             cout << "Hola" << endl;
  80.             break;
  81.         case 3:
  82.             cout << "Hallo" << endl;
  83.             break;
  84.         default :
  85.             cout << "Hello" << endl;
  86.     }
  87.  
  88.     // Ternary operator
  89.     // variable = (condition) ? true : false
  90.     int largestNum = (5 > 2) ? 5 : 2;
  91.  
  92.     // Arrays
  93.     int myFavNums[5];   // specify how many element containers you need
  94.  
  95.     int badNums[5] = { 4, 13, 14, 24, 34 };
  96.  
  97.     cout << "Bad Number 1: " << badNums[0] << endl;
  98.  
  99.     // multidimensional arrays
  100.  
  101.     char myName[3][9] = {   {'N', 'e', 'y'},
  102.                             {'H', 'e', 'r', 'n', 'a', 'n', 'd', 'e', 'z'} };
  103.  
  104.     cout << "2nd letter in 2nd array" << myName[1][1] << endl;
  105.     myName[0][2] = 'o';
  106.     cout << "New Value " << myName[0][2] << endl;
  107.  
  108.     for (int i = 0; i <= 10; i++) {
  109.         cout << i << endl;
  110.     }
  111.  
  112.     for (int j = 0; j < 3; j++) {
  113.         for (int k = 0; k < 9; k++) {
  114.             cout << myName[j][k] << endl;
  115.         }
  116.     }
  117.     return 0;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement