Guest User

Daily Programming C++ 2

a guest
Jul 21st, 2012
465
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.43 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <algorithm>
  4. #include <time.h>
  5. #include <string>
  6. #include <vector>
  7.  
  8. using namespace std;
  9.  
  10. /*
  11. METHODS
  12. */
  13. void createFile();
  14. void readFile();
  15. void getResults();
  16. void printResults();
  17. void getID();
  18.  
  19.  
  20. /*
  21. Variables
  22. */
  23. const char possibleAnswers[] = "TFTFTFTF ";
  24. const string answers = "TTFTFTFFTFTTFFFTTFTF";
  25. vector <string> students;
  26. vector <int> scores;
  27.  
  28. int main()
  29. {
  30.     createFile();
  31.     readFile();
  32.     getResults();
  33.     printResults();
  34. }
  35.  
  36.  
  37. void createFile()
  38. {
  39.  
  40.     ifstream file("scores.txt");
  41.  
  42.  
  43.    
  44.     if(file.good())
  45.     {
  46.     }
  47.     else
  48.     {
  49.         ofstream file;
  50.         file.open("scores.txt", ios::in | ios::app);
  51.  
  52.         string tAnswers;
  53.         char numberOrLetter;
  54.  
  55.         int counter = 1;
  56.  
  57.         srand (time(NULL));
  58.  
  59.         for(int i = 0; i < 150; i++)
  60.         {
  61.             for (int i = 0; i < 8; i++)
  62.             {
  63.                 numberOrLetter = rand() % 26 + 65;
  64.  
  65.                 file << numberOrLetter;
  66.  
  67.                 counter++;
  68.  
  69.             }
  70.  
  71.  
  72.             file << " ";
  73.  
  74.  
  75.             for(int i = 0; i < 20; i++)
  76.             {
  77.                 tAnswers[i] = possibleAnswers[rand() % 9];
  78.                 file << tAnswers[i];
  79.             }
  80.             file << "\n";
  81.         }
  82.  
  83.  
  84.  
  85.     }
  86.  
  87. }
  88.  
  89. void readFile()
  90. {
  91.  
  92.     string line;
  93.     ifstream file("scores.txt");
  94.  
  95.  
  96.     if(file)
  97.     {
  98.  
  99.         while(getline(file, line))
  100.         {
  101.  
  102.             students.push_back(line);
  103.  
  104.         }
  105.     }
  106.  
  107. }
  108.  
  109. void getResults()
  110. {
  111.  
  112.     string found;
  113.     string foundSubString;
  114.     int score = 0;
  115.  
  116.  
  117.     for(int i = 0; i < 150; i++)
  118.     {
  119.         found = students[i];
  120.  
  121.         foundSubString = found.substr (9,20);
  122.  
  123.  
  124.         for(int x = 0; x < 20; x++)
  125.         {
  126.  
  127.             if(foundSubString[x] == answers[x])
  128.             {
  129.  
  130.                 score += 2;
  131.  
  132.             }
  133.             else if(found[x] != answers[x] && found[x] != ' ')
  134.             {
  135.  
  136.                 score -=1;
  137.  
  138.             } else if(found[x] = ' ') {
  139.  
  140.                 //no score change
  141.             }
  142.         }
  143.  
  144.  
  145.         scores.push_back(score);
  146.         score = 0;
  147.     }
  148.  
  149.  
  150.  
  151.  
  152.  
  153. }
  154. void printResults()
  155. {
  156.  
  157.     string student;
  158.     string id;
  159.  
  160.     char grade;
  161.  
  162.  
  163.  
  164.     cout << "||--------------------------------------------------------------||" << endl;
  165.     cout << "||---------------------------RESULTS----------------------------||" << endl;
  166.     cout << "||                                                              ||" << endl;
  167.  
  168.     for(int i = 0; i < students.size(); i++)
  169.     {
  170.  
  171.         student = students[i];
  172.         id = student.substr(0, 8);
  173.  
  174.         if(scores[i] >= 35)
  175.         {
  176.  
  177.             grade = 'A';
  178.  
  179.         }
  180.         else if (scores[i] >= 30 && scores[i] < 35)
  181.         {
  182.  
  183.             grade = 'B';
  184.  
  185.         }
  186.         else if (scores[i] >= 25 && scores[i] < 30)
  187.         {
  188.  
  189.             grade = 'C';
  190.  
  191.         }
  192.         else if (scores[i] >= 20 && scores[i] < 25)
  193.         {
  194.  
  195.             grade = 'D';
  196.  
  197.         }
  198.         else if (scores[i] >= 15 && scores[i] < 20)
  199.         {
  200.  
  201.             grade = 'E';
  202.  
  203.         }
  204.         else
  205.         {
  206.  
  207.             grade = 'F';
  208.  
  209.         }
  210.         cout << "||\t Student ID: " << id << "\tScore: " << scores[i] << "\tGrade: " << grade << "\t||" <<endl;
  211.  
  212.     }
  213.  
  214.     cout << "||--------------------------------------------------------------||" << endl;
  215.  
  216.  
  217. }
Advertisement
Add Comment
Please, Sign In to add comment