fork download
  1. #include <iostream>
  2. #include <cstdlib> // for rand() and srand()
  3. #include <ctime> // for time()
  4. #include <stdio.h> // for printf and scanf
  5. using namespace std;
  6.  
  7. int sum(int x, int y) {
  8. return x + y;
  9. }
  10.  
  11. void welcomeText() {
  12. cout << "### Welcome to the guessing number game ###" << endl;
  13. }
  14.  
  15. void puts(string str) {
  16. cout << str << endl;
  17. }
  18.  
  19. int getGuessNumber() {
  20. int guess;
  21. cout << "Guess number (1 to 10): ";
  22. cin >> guess;
  23. return guess;
  24. }
  25.  
  26. int main() {
  27. // Login system
  28. string username;
  29. cout << "Username: ";
  30. cin >> username;
  31.  
  32. string passwd;
  33. string p1 = "13022567";
  34. int attempts = 0;
  35. const int max_attempts = 4;
  36.  
  37. cout << "Enter Password: ";
  38. while (attempts < max_attempts) {
  39. cin >> passwd;
  40.  
  41. if (passwd == p1) {
  42. break;
  43. } else {
  44. attempts++;
  45. cout << "Incorrect password attempt " << attempts << endl;
  46.  
  47. if (attempts >= max_attempts) {
  48. cout << "Too many failed attempts. Exiting program." << endl;
  49. exit(0);
  50. }
  51. }
  52. }
  53.  
  54. // Calculator menu
  55. int choice;
  56. int num1, num2;
  57.  
  58. cout << "\nMain Menu\n";
  59. cout << "1. +\n";
  60. cout << "2. -\n";
  61. cout << "3. x\n";
  62. cout << "4. /\n";
  63.  
  64. while (true) {
  65. cout << "\n--------------------------------\n";
  66. cout << "Select choice ==> ";
  67. cin >> choice;
  68.  
  69. if (choice == 5) {
  70. break;
  71. }
  72.  
  73. if (choice < 1 || choice > 4) {
  74. cout << "Invalid choice! Please select a valid number between 1 and 4.\n";
  75. continue;
  76. }
  77.  
  78. cout << "Input Number 1 [1-10]: ";
  79. cin >> num1;
  80. while (num1 < 1 || num1 > 10) {
  81. cout << "Error: Please enter a number between 1 and 10.\n";
  82. cout << "Input Number 1 [1-10]: ";
  83. cin >> num1;
  84. }
  85.  
  86. cout << "Input Number 2 [1-10]: ";
  87. cin >> num2;
  88. while (num2 < 1 || num2 > 10) {
  89. cout << "Error: Please enter a number between 1 and 10.\n";
  90. cout << "Input Number 2 [1-10]: ";
  91. cin >> num2;
  92. }
  93.  
  94. cout << "\n--------------------------------\n";
  95. bool swapped = false;
  96. if (num1 < num2) {
  97. swapped = true;
  98. }
  99.  
  100. switch (choice) {
  101. case 1:
  102. if (swapped) {
  103. cout << num2 << " + " << num1 << " = " << (num2 + num1) << endl;
  104. } else {
  105. cout << num1 << " + " << num2 << " = " << (num1 + num2) << endl;
  106. }
  107. break;
  108. case 2:
  109. if (swapped) {
  110. cout << num2 << " - " << num1 << " = " << (num2 - num1) << endl;
  111. } else {
  112. cout << num1 << " - " << num2 << " = " << (num1 - num2) << endl;
  113. }
  114. break;
  115. case 3:
  116. if (swapped) {
  117. cout << num2 << " * " << num1 << " = " << (num2 * num1) << endl;
  118. } else {
  119. cout << num1 << " * " << num2 << " = " << (num1 * num2) << endl;
  120. }
  121. break;
  122. case 4:
  123. if (num2 == 0) {
  124. cout << "Error: Division by zero is not allowed.\n";
  125. } else if (swapped) {
  126. cout << num2 << " / " << num1 << " = " << (num2 / num1) << endl;
  127. } else {
  128. cout << num1 << " / " << num2 << " = " << (num1 / num2) << endl;
  129. }
  130. break;
  131. }
  132.  
  133. char continueChoice;
  134. cout << "\nDo you want to continue with another calculation? (y/n): ";
  135. cin >> continueChoice;
  136.  
  137. if (continueChoice != 'y' && continueChoice != 'Y') {
  138. break;
  139. }
  140. }
  141.  
  142. // Sum calculation
  143. int a = 3;
  144. int b = 5;
  145. cout << "a + b = " << sum(a, b) << endl;
  146.  
  147. // Guessing number game
  148. srand(time(NULL));
  149. bool is_gameover = false;
  150. int guess, secret;
  151. int guessed = 0;
  152.  
  153. welcomeText();
  154. secret = rand() % 10 + 1;
  155. puts("Secret number has been chosen");
  156.  
  157. do {
  158. guess = getGuessNumber();
  159. guessed++;
  160. if (secret < guess) {
  161. puts("The secret number is lower");
  162. } else if (secret > guess) {
  163. puts("The secret number is higher");
  164. }
  165. } while (secret != guess);
  166.  
  167. puts("Congratulations!");
  168. cout << "The secret number is " << secret << endl;
  169. cout << "You made " << guessed << " guess" << (guessed != 1 ? "es" : "") << endl;
  170.  
  171. // Grade calculator
  172. string name;
  173. int score1, score2, score3, total;
  174.  
  175. cout << "Grade Calculator\n";
  176. cout << "Name: ";
  177. cin >> name;
  178.  
  179. cout << "Score 1: ";
  180. cin >> score1;
  181.  
  182. cout << "Score 2: ";
  183. cin >> score2;
  184.  
  185. cout << "Score 3: ";
  186. cin >> score3;
  187.  
  188. cout << "======================\n";
  189. total = score1 + score2 + score3;
  190. cout << "Total: " << total << endl;
  191. cout << "======================\n";
  192.  
  193. switch (total / 10) {
  194. case 10:
  195. case 9:
  196. case 8:
  197. cout << "Grade A" << endl;
  198. break;
  199. case 7:
  200. cout << "Grade B" << endl;
  201. break;
  202. case 6:
  203. cout << "Grade C" << endl;
  204. break;
  205. case 5:
  206. cout << "Grade D" << endl;
  207. break;
  208. default:
  209. cout << "Grade E" << endl;
  210. break;
  211. }
  212.  
  213. // Multiplication Table
  214. int number;
  215. cout << "Enter number for multiplication table: ";
  216. cin >> number;
  217.  
  218. for (int i = 1; i <= 24; i++) {
  219. printf(" +-------------+----------------+\n");
  220. printf(" | %4d x %2d |%10d |\n", number, i, number * i);
  221.  
  222. if (i == 24) {
  223. printf(" +-------------+----------------+\n");
  224. }
  225. }
  226.  
  227. return 0;
  228. }
  229.  
Success #stdin #stdout 0.02s 25812KB
stdin
Standard input is empty
stdout
#include <iostream>
#include <cstdlib> // for rand() and srand()
#include <ctime>   // for time()
#include <stdio.h> // for printf and scanf
using namespace std;

int sum(int x, int y) {
    return x + y;
}

void welcomeText() {
    cout << "### Welcome to the guessing number game ###" << endl;
}

void puts(string str) {
    cout << str << endl;
}

int getGuessNumber() {
    int guess;
    cout << "Guess number (1 to 10): ";
    cin >> guess;
    return guess;
}

int main() {
    // Login system
    string username;
    cout << "Username: ";
    cin >> username;

    string passwd;
    string p1 = "13022567"; 
    int attempts = 0;
    const int max_attempts = 4;

    cout << "Enter Password: ";
    while (attempts < max_attempts) {
        cin >> passwd;

        if (passwd == p1) {
            break;
        } else {
            attempts++;
            cout << "Incorrect password attempt " << attempts << endl;

            if (attempts >= max_attempts) {
                cout << "Too many failed attempts. Exiting program." << endl;
                exit(0);
            }
        }
    }

    // Calculator menu
    int choice;
    int num1, num2;

    cout << "\nMain Menu\n";
    cout << "1. +\n";
    cout << "2. -\n";
    cout << "3. x\n";
    cout << "4. /\n";

    while (true) {
        cout << "\n--------------------------------\n";
        cout << "Select choice ==> ";
        cin >> choice;

        if (choice == 5) {
            break; 
        }

        if (choice < 1 || choice > 4) {
            cout << "Invalid choice! Please select a valid number between 1 and 4.\n";
            continue; 
        }

        cout << "Input Number 1 [1-10]: ";
        cin >> num1;
        while (num1 < 1 || num1 > 10) {
            cout << "Error: Please enter a number between 1 and 10.\n";
            cout << "Input Number 1 [1-10]: ";
            cin >> num1;
        }

        cout << "Input Number 2 [1-10]: ";
        cin >> num2;
        while (num2 < 1 || num2 > 10) {
            cout << "Error: Please enter a number between 1 and 10.\n";
            cout << "Input Number 2 [1-10]: ";
            cin >> num2;
        }

        cout << "\n--------------------------------\n";
        bool swapped = false;
        if (num1 < num2) {
            swapped = true;
        }

        switch (choice) {
            case 1: 
                if (swapped) {
                    cout << num2 << " + " << num1 << " = " << (num2 + num1) << endl;
                } else {
                    cout << num1 << " + " << num2 << " = " << (num1 + num2) << endl;
                }
                break;
            case 2:  
                if (swapped) {
                    cout << num2 << " - " << num1 << " = " << (num2 - num1) << endl;
                } else {
                    cout << num1 << " - " << num2 << " = " << (num1 - num2) << endl;
                }
                break;
            case 3:  
                if (swapped) {
                    cout << num2 << " * " << num1 << " = " << (num2 * num1) << endl;
                } else {
                    cout << num1 << " * " << num2 << " = " << (num1 * num2) << endl;
                }
                break;
            case 4:  
                if (num2 == 0) {
                    cout << "Error: Division by zero is not allowed.\n";
                } else if (swapped) {
                    cout << num2 << " / " << num1 << " = " << (num2 / num1) << endl;
                } else {
                    cout << num1 << " / " << num2 << " = " << (num1 / num2) << endl;
                }
                break;
        }
        
        char continueChoice;
        cout << "\nDo you want to continue with another calculation? (y/n): ";
        cin >> continueChoice;
        
        if (continueChoice != 'y' && continueChoice != 'Y') {
            break; 
        }
    }

    // Sum calculation
    int a = 3;
    int b = 5;
    cout << "a + b = " << sum(a, b) << endl;

    // Guessing number game
    srand(time(NULL));
    bool is_gameover = false;
    int guess, secret;
    int guessed = 0;
    
    welcomeText();
    secret = rand() % 10 + 1;
    puts("Secret number has been chosen");

    do {
        guess = getGuessNumber();
        guessed++;
        if (secret < guess) {
            puts("The secret number is lower");
        } else if (secret > guess) {
            puts("The secret number is higher");
        }
    } while (secret != guess);

    puts("Congratulations!");
    cout << "The secret number is " << secret << endl;
    cout << "You made " << guessed << " guess" << (guessed != 1 ? "es" : "") << endl;

    // Grade calculator
    string name;
    int score1, score2, score3, total;

    cout << "Grade Calculator\n";
    cout << "Name: ";
    cin >> name;

    cout << "Score 1: ";
    cin >> score1;

    cout << "Score 2: ";
    cin >> score2;

    cout << "Score 3: ";
    cin >> score3;

    cout << "======================\n";
    total = score1 + score2 + score3;
    cout << "Total: " << total << endl;
    cout << "======================\n";

    switch (total / 10) {
        case 10:
        case 9:
        case 8:
            cout << "Grade A" << endl;
            break;
        case 7:
            cout << "Grade B" << endl;
            break;
        case 6:
            cout << "Grade C" << endl;
            break;
        case 5:
            cout << "Grade D" << endl;
            break;
        default:
            cout << "Grade E" << endl;
            break;
    }

    // Multiplication Table
    int number;
    cout << "Enter number for multiplication table: ";
    cin >> number;
    
    for (int i = 1; i <= 24; i++) {
        printf(" +-------------+----------------+\n");
        printf(" | %4d x %2d   |%10d      |\n", number, i, number * i);
        
        if (i == 24) {
            printf(" +-------------+----------------+\n");
        }
    }

    return 0;
}