Advertisement
KingAesthetic

Example 1 for C++

May 20th, 2024
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.87 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. int main() {
  6.     // array that prints the names of cars:
  7.     string Cars[] = {"Bugatti", "Lamborghini", "Mercedes", "BMW"};
  8.     for (int i = 0; i < 4; i++) {
  9.         cout << i << " " << Cars[i] << "\n";
  10.     }
  11.  
  12.     // Create a structure and assign values to it, as well as print it:
  13.     struct {
  14.         int myNum;
  15.         string mystring;
  16.     } myStructure;
  17.     myStructure.myNum = 50;
  18.     myStructure.mystring = "President Emmanuel";
  19.     cout << myStructure.myNum << "\n";
  20.     cout << myStructure.mystring << "\n";
  21.  
  22.     // Reference to the string food:
  23.     string food = "Beans";
  24.     string &meal = food;
  25.     string* ptr = &meal;
  26.     cout << food << "\n";
  27.     cout << &meal << "\n";
  28.     cout << ptr << "\n";
  29.  
  30.     // To print today:
  31.     int day = 7;
  32.     switch (day) {
  33.         case 1:
  34.             cout << "Monday";
  35.             break;
  36.         case 2:
  37.             cout << "Tuesday";
  38.             break;
  39.         case 3:
  40.             cout << "Wednesday";
  41.             break;
  42.         case 4:
  43.             cout << "Thursday";
  44.             break;
  45.         case 5:
  46.             cout << "Friday";
  47.             break;
  48.         case 6:
  49.             cout << "Saturday";
  50.             break;
  51.         case 7:
  52.             cout << "Sunday" << "\n";
  53.             break;
  54.  
  55.     }
  56.  
  57.     // finding the sum of two numbers:
  58.     int x, y;
  59.     int sum;
  60.     cout << "Type a number: ";
  61.     cin >> x;
  62.     cout << "Type another number: ";
  63.     cin >> y;
  64.     sum = x + y;
  65.     cout << sum << "\n";
  66.  
  67. // Looping through an array of fruits:
  68.     string fruits[2][4] = {
  69.             {"Apples",  "Bananas", "Pear",     "Tangerines"},
  70.             {"Oranges", "Mangoes", "Eggplant", "coconut"}
  71.     };
  72.     for (int b = 0; b < 4; b++) {
  73.         for (int c = 0; c < 4; c++) {
  74.             cout << fruits[b][c] << "\n";
  75.         }
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement