Advertisement
Guest User

C++ Help

a guest
Sep 9th, 2021
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.94 KB | None | 0 0
  1. #include <sstream>
  2. #include <vector>
  3. #include <string>
  4. #include <fstream>
  5. #include <iostream>
  6. #include "Instructor.h"
  7. #include "Student.h"
  8.  
  9. using namespace std;
  10.  
  11. //Global variables
  12. string username;
  13. string password;
  14. Instructor instructor;
  15. Student student;
  16. vector<string> studentVector;
  17. vector<string> instructorVector;
  18. vector<Student> studentList;
  19. vector<Instructor> instructorList;
  20.  
  21. //The functions in the document are being declared here to resolve scope issues
  22. void handleFiles(int argc, char ** argv);
  23. void loginProcess();
  24. void instructorScope();
  25. void studentScope();
  26.  
  27. //This is the first method called from main
  28. //handleFiles checks that the files passed in as command line arguments
  29. // and then parses the files into vectors
  30. void handleFiles(int argc, char ** argv) {
  31.     if (argc != 2) {
  32.         cout << "Usage: main [instructors_file] [students_file]";
  33.         exit(1);
  34.     } //if
  35.  
  36.     try {
  37.         ifstream in;
  38.         string line;
  39.         in.open(argv[0]);
  40.         while (getline(in, line)) {
  41.             stringstream lineStream(line);
  42.             string token;
  43.             while(lineStream >> token) {
  44.                 instructorVector.push_back(token);
  45.             } //while
  46.         } //while
  47.         in.close();
  48.         cin.sync();
  49.  
  50.         //This for loop is initializing all of the instructor objects
  51.         Instructor temp;
  52.         for (int i = 0, j = 0; i < instructorVector.size() / 4; i++) {
  53.             temp.setUsername(instructorVector[j]);
  54.             j++;
  55.             temp.setPassword(instructorVector[j]);
  56.             j++;
  57.             string fullName = instructorVector[j] + " " + instructorVector[j + 1];
  58.             temp.setInstructorName(fullName);
  59.             j += 2;
  60.             instructorList.push_back(temp);
  61.         } //for
  62.     } catch(...) {
  63.         cout << "Error: cannot parse instructors information from file "
  64.              << argv[0];
  65.         exit(1);
  66.     } //try/catch [parsing instructors file]
  67.  
  68.     try {
  69.         ifstream in;
  70.         string line;
  71.         in.open(argv[1]);
  72.         while (getline(in, line)) {
  73.             stringstream lineStream(line);
  74.             string token;
  75.             while (lineStream >> token) {
  76.                 studentVector.push_back(token);
  77.             } //while
  78.         } //while
  79.         in.close();
  80.         cin.sync();
  81.  
  82. //This for loop is initializing all of the student objects
  83.         Student temp;
  84.         for (int i = 0, j = 0; i < studentVector.size() / 8; i++) {
  85.             temp.setUsername(studentVector[j]);
  86.             j++;
  87.             temp.setPassword(studentVector[j]);
  88.             j++;
  89.             temp.setStudentName(studentVector[j] + " " + studentVector[j+1]);
  90.             j += 2;
  91.             temp.setProjectGrade(stoi(studentVector[j]));
  92.             j++;
  93.             temp.setQuizGrade(stoi(studentVector[j]));
  94.             j++;
  95.             temp.setMidtermGrade(stoi(studentVector[j]));
  96.             j++;
  97.             temp.setFinalGrade(stoi(studentVector[j]));
  98.             j++;
  99.             studentList.push_back(temp);
  100.         } //for
  101.     } catch (...) {
  102.         cout << "Error: cannot parse students information from file "
  103.              << argv[1];
  104.         exit(1);
  105.     } //try/catch [parsing students file]
  106. } //handleFiles
  107.  
  108. //This handles the entire experience from the Instructor's viewpoint
  109. //It
  110. void instructorScope() {
  111.     bool instructorBool = false;
  112.     string instructorOption;
  113.     while (!instructorBool) {
  114.         cout << "\n"
  115.              << "Query options,"
  116.              << "     1 - view grades of a student"
  117.              << "     2 - view stats"
  118.              << "Enter option number:";
  119.         cin >> instructorOption;
  120.  
  121.         if (instructorOption == "1" || instructorOption == "2") {
  122.             instructorBool = true;
  123.         } else {
  124.             cout << "\n"
  125.                  << "Invalid option. Please enter a valid option.";
  126.         } //if / else [selecting what to view]
  127.     } //while
  128.     if (instructorOption == "1") {
  129.  
  130.     } else if (instructorOption == "2") {
  131.  
  132.     } //if / else if [displaying the content]
  133. } //instructorScope
  134.  
  135. void studentScope() {
  136.     bool studentBool = false;
  137.     while (!studentBool) {
  138.         string studentOption;
  139.         cout << "\n"
  140.              << "Do you want to view grades (y/n)?";
  141.         cin >> studentOption;
  142.         if (studentOption == "y" || studentOption == "Y") {
  143.             cout << "\n"
  144.                  << "Student name: "
  145.                  << student.getStudentName()
  146.                  << "\n"
  147.                  << "Project    "
  148.                  << student.getProjectGrade()
  149.                  << "%"
  150.                  << "\n"
  151.                  << "Quiz       "
  152.                  << student.getQuizGrade()
  153.                  << "%"
  154.                  << "\n"
  155.                  << "Midterm    "
  156.                  << student.getMidtermGrade()
  157.                  << "%"
  158.                  << "\n"
  159.                  << "Final      "
  160.                  << student.getFinalGrade()
  161.                  << "%"
  162.                  << "\n"
  163.                  << "Overall    "
  164.                  << student.getOverallGrade()
  165.                  << "%"
  166.                  << "\n";
  167.                 loginProcess();
  168.         } else if (studentOption == "n" || studentOption == "N") {
  169.             studentBool = true;
  170.             loginProcess();
  171.         } //if / else if
  172.     } //while
  173. } //studentScope
  174.  
  175. void loginProcess() {
  176.     vector<string> throwAway;
  177.     string throwAwayString;
  178.     while (cin) {
  179.         cin >> throwAwayString;
  180.         throwAway.push_back(throwAwayString);
  181.     } //while
  182.     cout << throwAway.size();
  183.     cin.sync();
  184.     //cin.ignore(256);
  185.  
  186.     string loginChoice;
  187.     bool whileCondition = false;
  188.     while (!whileCondition) {
  189.         cout << "User types,"
  190.              << "\n"
  191.              << "     1 - Instructor"
  192.              << "\n"
  193.              << "     2 - Student"
  194.              << "\n"
  195.              << "Select a login user type or enter 3 to exit:";
  196.         cin.sync();
  197.         getline(cin, loginChoice);
  198.         //cin >> loginChoice;
  199.         //break;
  200.         cout << "\n"
  201.              << "Login Choice: "
  202.              << loginChoice
  203.              << "end"
  204.              << "\n";
  205.  
  206.         if (loginChoice == "1" || loginChoice == "2") {
  207.             whileCondition = true;
  208.         } else if (loginChoice == "3") {
  209.             exit(0);
  210.         } //if / else if
  211.     } //while
  212.  
  213.     bool loginSuccessful = false;
  214.     while (!loginSuccessful) {
  215.         cout << "\n"
  216.              << "Enter credentials to login,"
  217.              << "\n"
  218.              << "     Enter username:";
  219.         cin >> username;
  220.         cout << "\n"
  221.              << "     Enter password: ";
  222.         break;
  223.         cin >> password;
  224.  
  225.         if (loginChoice == "1") {
  226.             if (instructor.login(username, password)) {
  227.                 cout << "\n"
  228.                      << "You are now logged in as instructor "
  229.                      << instructor.getInstructorName()
  230.                      << ".";
  231.                 loginSuccessful = true;
  232.                 instructorScope();
  233.             } else {
  234.                 cout << "\n"
  235.                      << "Login as instructor failed.";
  236.                     } // if / else if [instructor login check]
  237.         } else if (loginChoice == "2") {
  238.             if (student.login(username, password)) {
  239.                 cout << "\n"
  240.                      << "You are now logged in as student "
  241.                      << student.getStudentName()
  242.                      << ".";
  243.                 loginSuccessful = true;
  244.                 studentScope();
  245.             } else {
  246.                 cout << "\n"
  247.                      << "Login as student failed.";
  248.                     } //if / else if [student login check]
  249.         } //if / else if [loginChoice check]
  250.     } //while
  251. } //loginProcess
  252.  
  253. int main(int argc, char **argv) {
  254.     //handleFiles(argc, argv);
  255.     loginProcess(); //I made this into its own method so I can repeatedly call it
  256.  
  257.     return 0;
  258. } //main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement