Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.14 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <cstdlib>
  5. #include <time.h>
  6.  
  7.  
  8. /* run this program using the console pauser or add your own getch, system("pause") or input loop */
  9.  
  10. using namespace std;
  11.  
  12.  
  13. int main(int argc, char** argv) {
  14.  
  15.     srand(time(NULL));
  16.  
  17.     char op;
  18.  
  19.     double num1, num2, answer;
  20.     int correct = 0;
  21.     int wrong = 0;
  22.     int pervar = 0;
  23.     int product;
  24.     char loginData[100], data[100];
  25.     string pass = "1234";
  26.     string input;
  27.  
  28.     ofstream outfile;
  29.     outfile.open("afile.dat", ofstream::app);
  30.    
  31.     cout << "Please enter your login" << endl;
  32.     cin.getline(loginData, 100);
  33.     cout << "Please enter your password" << endl;
  34.     cin >> input;
  35.  
  36.     if (!(input == pass)) {
  37.         cout << "Incorrect password!" << endl;
  38.         system("pause");
  39.         return 0;
  40.     }
  41.  
  42.  
  43.     if (loginData[0] == 'T' || loginData[0] == 'S') {
  44.         if (loginData[0] == 'T') {
  45.             cout << "Students result:" << endl << endl;
  46.             cout << ifstream("afile.dat").rdbuf() << '\n';
  47.             system("pause");
  48.             return 0;
  49.         }
  50.        
  51.         if (loginData[0] == 'S') {
  52.             outfile << "Student ID: " << loginData << endl << endl;
  53.         }
  54.     }
  55.     else {
  56.         cout << "Invalid Login" << endl;
  57.         return 0;
  58.     }
  59.  
  60.     cout << "Addition...............'+'" << endl;
  61.     cout << "Subtraction............'-'" << endl;
  62.     cout << "Multiplication.........'*'" << endl;
  63.     cout << "Division...............'/'" << endl;
  64.     cin >> op;
  65.     switch (op)
  66.     {
  67.     case '+':cout << "Do the 5 addition question." << endl;
  68.         for (int i = 1; i <= 5; i++)
  69.         {
  70.             num1 = rand() % 50 + 1;
  71.             num2 = rand() % 50 + 1;
  72.             cout << num1 << " + " << num2 << " = ";
  73.             cin >> answer;
  74.             outfile << "Question: " << num1 << " + " << num2 << " = " << answer;
  75.             product = num1 + num2;
  76.  
  77.             if (answer == product)
  78.             {
  79.                 cout << "Correct" << endl;
  80.                 correct++;
  81.                 pervar = pervar + 20;
  82.                 outfile << " Correct" << endl;
  83.             }
  84.             else
  85.             {
  86.                 cout << "Sorry " << num1 << " + " << num2 << " = " << product << endl;
  87.                 wrong++;
  88.                 outfile << " Incorrect" << endl;
  89.             }
  90.         }
  91.         cout << "You got " << correct << " right and " << wrong << " wrong." << endl;
  92.         cout << "Percentage of correct answers " << pervar << "%" << endl;
  93.         outfile << "Student got " << correct << " right and " << wrong << " wrong." << endl;
  94.         outfile << "Percentage of correct answers " << pervar << "%" << endl;
  95.         break;
  96.  
  97.     case '-':cout << "Do the 5 subtraction question." << endl;
  98.         for (int i = 1; i <= 5; i++)
  99.         {
  100.             num1 = rand() % 50 + 1;
  101.             num2 = rand() % 50 + 1;
  102.             cout << num1 << "-" << num2 << "=";
  103.             cin >> answer;
  104.             outfile << "Question: " << num1 << " - " << num2 << " = " << answer;
  105.             product = num1 - num2;
  106.  
  107.             if (answer == product)
  108.             {
  109.                 cout << "Correct" << endl;
  110.                 correct++;
  111.                 pervar = pervar + 20;
  112.                 outfile << " Correct" << endl;
  113.             }
  114.             else
  115.             {
  116.                 cout << "Sorry " << num1 << " - " << num2 << " = " << product << endl;
  117.                 wrong++;
  118.                 outfile << " Incorrect" << endl;
  119.             }
  120.         }
  121.         cout << "You got " << correct << " right and " << wrong << " wrong." << endl;
  122.         cout << "Percentage of correct answers " << pervar << "%" << endl;
  123.         outfile << "Student got " << correct << " right and " << wrong << " wrong." << endl;
  124.         outfile << "Percentage of correct answers " << pervar << "%" << endl;
  125.         break;
  126.  
  127.     case '*':cout << "Do the 5 multiplication question." << endl;
  128.         for (int i = 1; i <= 5; i++)
  129.         {
  130.             num1 = rand() % 50 + 1;
  131.             num2 = rand() % 50 + 1;
  132.             cout << num1 << "*" << num2 << "=";
  133.             cin >> answer;
  134.             outfile << "Question: " << num1 << " * " << num2 << " = " << answer;
  135.             product = num1*num2;
  136.  
  137.             if (answer == product)
  138.             {
  139.                 cout << "Correct" << endl;
  140.                 correct++;
  141.                 pervar = pervar + 20;
  142.                 outfile << " Correct" << endl;
  143.             }
  144.             else
  145.             {
  146.                 cout << "Sorry " << num1 << " * " << num2 << " = " << product << endl;
  147.                 wrong++;
  148.                 outfile << " Incorrect" << endl;
  149.             }
  150.         }
  151.         cout << "You got " << correct << " right and " << wrong << " wrong." << endl;
  152.         cout << "Percentage of correct answers " << pervar << "%" << endl;
  153.         outfile << "Student got " << correct << " right and " << wrong << " wrong." << endl;
  154.         outfile << "Percentage of correct answers " << pervar << "%" << endl;
  155.         break;
  156.  
  157.     case '/':cout << "Do the 5 division question." << endl;
  158.         for (int i = 1; i <= 5; i++)
  159.         {
  160.             num1 = rand() % 50 + 1;
  161.             num2 = rand() % 50 + 1;
  162.             cout << num1 << "/" << num2 << "=";
  163.             cin >> answer;
  164.             outfile << "Question: " << num1 << " / " << num2 << " = " << answer;
  165.             product = num1 / num2;
  166.  
  167.             if (answer == product)
  168.             {
  169.                 cout << "Correct" << endl;
  170.                 correct++;
  171.                 pervar = pervar + 20;
  172.                 outfile << " Correct" << endl;
  173.             }
  174.             else
  175.             {
  176.                 cout << "Sorry " << num1 << " / " << num2 << " = " << product << endl;
  177.                 wrong++;
  178.                 outfile << " Incorrect" << endl;
  179.             }
  180.         }
  181.         cout << "You got " << correct << " right and " << wrong << " wrong." << endl;
  182.         cout << "Percentage of correct answers " << pervar << "%" << endl;
  183.         outfile << "Student got " << correct << " right and " << wrong << " wrong." << endl;
  184.         outfile << "Percentage of correct answers " << pervar << "%" << endl;
  185.         break;
  186.  
  187.     }
  188.     outfile << endl << endl;
  189.     outfile.close();
  190.     system("pause");
  191.     return 0;
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement