Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <cstdlib>
- #include <time.h>
- /* run this program using the console pauser or add your own getch, system("pause") or input loop */
- using namespace std;
- int main(int argc, char** argv) {
- srand(time(NULL));
- char op;
- double num1, num2, answer;
- int correct = 0;
- int wrong = 0;
- int pervar = 0;
- int product;
- char loginData[100], data[100];
- string pass = "1234";
- string input;
- ofstream outfile;
- outfile.open("afile.dat", ofstream::app);
- cout << "Please enter your login" << endl;
- cin.getline(loginData, 100);
- cout << "Please enter your password" << endl;
- cin >> input;
- if (!(input == pass)) {
- cout << "Incorrect password!" << endl;
- system("pause");
- return 0;
- }
- if (loginData[0] == 'T' || loginData[0] == 'S') {
- if (loginData[0] == 'T') {
- cout << "Students result:" << endl << endl;
- cout << ifstream("afile.dat").rdbuf() << '\n';
- system("pause");
- return 0;
- }
- if (loginData[0] == 'S') {
- outfile << "Student ID: " << loginData << endl << endl;
- }
- }
- else {
- cout << "Invalid Login" << endl;
- return 0;
- }
- cout << "Addition...............'+'" << endl;
- cout << "Subtraction............'-'" << endl;
- cout << "Multiplication.........'*'" << endl;
- cout << "Division...............'/'" << endl;
- cin >> op;
- switch (op)
- {
- case '+':cout << "Do the 5 addition question." << endl;
- for (int i = 1; i <= 5; i++)
- {
- num1 = rand() % 50 + 1;
- num2 = rand() % 50 + 1;
- cout << num1 << " + " << num2 << " = ";
- cin >> answer;
- outfile << "Question: " << num1 << " + " << num2 << " = " << answer;
- product = num1 + num2;
- if (answer == product)
- {
- cout << "Correct" << endl;
- correct++;
- pervar = pervar + 20;
- outfile << " Correct" << endl;
- }
- else
- {
- cout << "Sorry " << num1 << " + " << num2 << " = " << product << endl;
- wrong++;
- outfile << " Incorrect" << endl;
- }
- }
- cout << "You got " << correct << " right and " << wrong << " wrong." << endl;
- cout << "Percentage of correct answers " << pervar << "%" << endl;
- outfile << "Student got " << correct << " right and " << wrong << " wrong." << endl;
- outfile << "Percentage of correct answers " << pervar << "%" << endl;
- break;
- case '-':cout << "Do the 5 subtraction question." << endl;
- for (int i = 1; i <= 5; i++)
- {
- num1 = rand() % 50 + 1;
- num2 = rand() % 50 + 1;
- cout << num1 << "-" << num2 << "=";
- cin >> answer;
- outfile << "Question: " << num1 << " - " << num2 << " = " << answer;
- product = num1 - num2;
- if (answer == product)
- {
- cout << "Correct" << endl;
- correct++;
- pervar = pervar + 20;
- outfile << " Correct" << endl;
- }
- else
- {
- cout << "Sorry " << num1 << " - " << num2 << " = " << product << endl;
- wrong++;
- outfile << " Incorrect" << endl;
- }
- }
- cout << "You got " << correct << " right and " << wrong << " wrong." << endl;
- cout << "Percentage of correct answers " << pervar << "%" << endl;
- outfile << "Student got " << correct << " right and " << wrong << " wrong." << endl;
- outfile << "Percentage of correct answers " << pervar << "%" << endl;
- break;
- case '*':cout << "Do the 5 multiplication question." << endl;
- for (int i = 1; i <= 5; i++)
- {
- num1 = rand() % 50 + 1;
- num2 = rand() % 50 + 1;
- cout << num1 << "*" << num2 << "=";
- cin >> answer;
- outfile << "Question: " << num1 << " * " << num2 << " = " << answer;
- product = num1*num2;
- if (answer == product)
- {
- cout << "Correct" << endl;
- correct++;
- pervar = pervar + 20;
- outfile << " Correct" << endl;
- }
- else
- {
- cout << "Sorry " << num1 << " * " << num2 << " = " << product << endl;
- wrong++;
- outfile << " Incorrect" << endl;
- }
- }
- cout << "You got " << correct << " right and " << wrong << " wrong." << endl;
- cout << "Percentage of correct answers " << pervar << "%" << endl;
- outfile << "Student got " << correct << " right and " << wrong << " wrong." << endl;
- outfile << "Percentage of correct answers " << pervar << "%" << endl;
- break;
- case '/':cout << "Do the 5 division question." << endl;
- for (int i = 1; i <= 5; i++)
- {
- num1 = rand() % 50 + 1;
- num2 = rand() % 50 + 1;
- cout << num1 << "/" << num2 << "=";
- cin >> answer;
- outfile << "Question: " << num1 << " / " << num2 << " = " << answer;
- product = num1 / num2;
- if (answer == product)
- {
- cout << "Correct" << endl;
- correct++;
- pervar = pervar + 20;
- outfile << " Correct" << endl;
- }
- else
- {
- cout << "Sorry " << num1 << " / " << num2 << " = " << product << endl;
- wrong++;
- outfile << " Incorrect" << endl;
- }
- }
- cout << "You got " << correct << " right and " << wrong << " wrong." << endl;
- cout << "Percentage of correct answers " << pervar << "%" << endl;
- outfile << "Student got " << correct << " right and " << wrong << " wrong." << endl;
- outfile << "Percentage of correct answers " << pervar << "%" << endl;
- break;
- }
- outfile << endl << endl;
- outfile.close();
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement