Guest User

Untitled

a guest
Apr 24th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.49 KB | None | 0 0
  1. /* Hugo Cedano
  2. finalProject
  3. part 0
  4. */
  5. #include <iostream>
  6. #include <string>
  7. #include <sstream>
  8.  
  9. using namespace std;
  10.  
  11. struct student{
  12. string name;
  13. char sex;
  14. float gpa;
  15. char finalGrade;
  16. };
  17.  
  18. const int MAX_IN_CLASS = 20;
  19. student COP1334[MAX_IN_CLASS];
  20.  
  21. bool empty = true;
  22. string searched_Name;
  23.  
  24. void studentReg (student COP1334[], int& enrolled);
  25. void displayStudents (student COP1334[], int& enrolled);
  26. void checkRegistered (student COP1334[], int& enrolled);
  27. void dropStudent (student COP1334[], int& enrolled, int i);
  28. void assignFinalGrade(student COP1334[], int& enrolled, int& assignCount);
  29. //void checkArray (student COP1334[], int& enrolled, int i);
  30. void sortName(student COP1334[], int& enrolled);
  31. void sortGPA(student COP1334[], int& enrolled);
  32. void swap(student COP1334[], int i, int j);
  33.  
  34. int main() {
  35.  
  36. char user_Input,
  37. sortInput;
  38. int enrolled = 0,
  39. assignCount=0;
  40. string temp,
  41. tempSort;
  42.  
  43.  
  44. do{
  45. cout << "****************************************************" << endl;
  46. cout << "*" << "Welcome to the COP1334 Student Registration System!" << endl;
  47. cout << "*" << "To register a student enter A." << endl;
  48. cout << "*" << "To print all registered students enter P." << endl;
  49. cout << "*" << "To drop a student enter D." << endl;
  50. cout << "*" << "To sort students enter S." << endl;
  51. cout << "*" << "To assign final grades for students enter F." << endl;
  52. cout << "*" << "To quit this program enter Q." << endl;
  53. cout << "****************************************************" << endl;
  54.  
  55. getline(cin, temp);
  56. if(cin.fail()){
  57. cin.clear();
  58. cin.ignore(1337, '\n');
  59. }
  60. user_Input = temp[0];
  61. switch(user_Input){
  62. case 'A':
  63. case 'a':
  64. if (enrolled==MAX_IN_CLASS){
  65. cout << "----------------------------------------------------" << endl;
  66. cout << "The class is full! Sorry" << endl;
  67. cout << "----------------------------------------------------" << endl;
  68. }
  69. else
  70. studentReg (COP1334, enrolled);
  71. break;
  72. case 'P':
  73. case 'p':
  74. if (empty){
  75. cout << "----------------------------------------------------" << endl;
  76. cout << "There are no registered students" << endl;
  77. cout << "----------------------------------------------------" << endl;
  78. }
  79. else{
  80. displayStudents (COP1334, enrolled);
  81. }
  82. break;
  83. case 'D':
  84. case 'd':
  85. if (empty){
  86. cout << "----------------------------------------------------" << endl;
  87. cout << "There are no students to drop!" << endl;
  88. cout << "----------------------------------------------------" << endl;
  89. }
  90. else
  91. checkRegistered (COP1334, enrolled);
  92. break;
  93. case 'F':
  94. case 'f':
  95. if (empty){
  96. cout << "----------------------------------------------------" << endl;
  97. cout << "There are no students to assign grades to!" << endl;
  98. cout << "----------------------------------------------------" << endl;
  99. }
  100. else if (enrolled == assignCount){
  101. cout << "All grades have been assigned" << endl;
  102. }
  103. else{
  104. //assignFinalGrade(COP1334,enrolled);
  105. for(int i=0; i<enrolled; i++){
  106. if(COP1334[i].finalGrade == 'N'){
  107. assignFinalGrade(COP1334, enrolled, assignCount);
  108. }
  109. }
  110. break;
  111. }
  112.  
  113. break;
  114. case 'S':
  115. case 's':
  116. if (empty){
  117. cout << "----------------------------------------------------" << endl;
  118. cout << "There are no students to sort!" << endl;
  119. cout << "----------------------------------------------------" << endl;
  120. }
  121. else{
  122. cout << "----------------------------------------------------" << endl;
  123. cout << "To sort students by their Name enter N." << endl;
  124. cout << "To sort students by their GPA enter G." << endl;
  125. cout << "----------------------------------------------------" << endl;
  126. getline(cin, tempSort);
  127. if(cin.fail()){
  128. cin.clear();
  129. cin.ignore(1337, '\n');
  130. }
  131. sortInput = tempSort[0];
  132. switch (sortInput){
  133. case 'N':
  134. case 'n':
  135. sortName(COP1334, enrolled);
  136. break;
  137. case 'G':
  138. case 'g':
  139. sortGPA(COP1334, enrolled);
  140. break;
  141. default:
  142. cout << "Invalid input" << endl;
  143. }
  144.  
  145. }
  146. break;
  147. case 'Q':
  148. case 'q':
  149. break;
  150. default:
  151. cout << "Invalid input" << endl;
  152. }
  153. }
  154. while (user_Input != 'Q');
  155.  
  156. return 0;
  157. }
  158.  
  159. void studentReg (student COP1334[], int& enrolled){
  160. student s;
  161. string temp;
  162. cout << "Enter Name: ";
  163. getline(cin, s.name);
  164. cout << "GPA: ";
  165. cin >> s.gpa;
  166. cout << "Sex: ";
  167. cin.ignore(1, '\n');
  168. getline(cin,temp);
  169. s.sex = temp.substr(0,1)[0];
  170. s.finalGrade = 'N';
  171.  
  172. COP1334[enrolled] = s;
  173. enrolled++;
  174. empty = false;
  175. cout << "Student successfully registered" << endl;
  176. }
  177.  
  178. void displayStudents (student COP1334[], int& enrolled){
  179. cout << "----------------------------------------------------" << endl;
  180. cout << "Class Capacity: " << enrolled << '/' << MAX_IN_CLASS << " N- \"Not Entered\"\n";
  181. for(int i=0; i<enrolled; i++){
  182. cout << "----------------------------------------------------" << endl;
  183. cout << "Name\tGPA\tGender\tFinal Grade" << endl;
  184. cout << COP1334[i].name << "\t" << COP1334[i].gpa << "\t" << COP1334[i].sex << "\t" << COP1334[i].finalGrade << endl;
  185. cout << "----------------------------------------------------" << endl;
  186. }
  187. }
  188.  
  189. void checkRegistered (student COP1334[], int& enrolled){
  190. bool studentFound=false;
  191. cout << "----------------------------------------------------" << endl;
  192. cout << "Enter name of student to drop: " << endl;
  193. cout << "----------------------------------------------------" << endl;
  194. getline(cin, searched_Name);
  195.  
  196. if(searched_Name.compare(COP1334[enrolled-1].name) == 0){
  197. enrolled--;
  198. cout << "----------------------------------------------------" << endl;
  199. cout << "Student was dropped successfully" << endl;
  200. cout << "----------------------------------------------------" << endl;
  201. if(enrolled==0){
  202. empty=true;
  203. }
  204. return;
  205. }
  206. //check if student is registered and calls the actual drop student function
  207. for(int i=0; i<enrolled-1; i++){
  208. if(searched_Name==(COP1334[i].name)){
  209. studentFound = true;
  210. dropStudent (COP1334, enrolled, i);
  211. cout << "----------------------------------------------------" << endl;
  212. cout << "Student was dropped successfully!" << endl;
  213. cout << "----------------------------------------------------" << endl;
  214. }
  215. }
  216. if(!(studentFound)){
  217. cout << "----------------------------------------------------" << endl;
  218. cout << "Student isn't registered at this time" << endl;
  219. cout << "----------------------------------------------------" << endl;
  220. }
  221. if(enrolled==0){
  222. empty=true;
  223. }
  224. }
  225.  
  226. void assignFinalGrade(student COP1334[], int& enrolled, int& assignCount){
  227. char user_Input, user_Input2, user_Input3;
  228. string temp, temp2, temp3;
  229.  
  230. for (int i=0; i<enrolled; i++){
  231. cout << "----------------------------------------------------" << endl;
  232. cout << "To assign a final grade for " << COP1334[i].name << ", press A" << endl;
  233. cout << "To proceed to the next student, press P" << endl;
  234. cout << "----------------------------------------------------" << endl;
  235. getline(cin, temp);
  236. if(cin.fail()){
  237. cin.clear();
  238. cin.ignore(1337, '\n');
  239. }
  240. user_Input = temp[0];
  241. switch (user_Input){
  242. case 'A':
  243. case 'a':
  244. do{
  245. cout << "----------------------------------------------------" << endl;
  246. cout << "Enter a grade to assign to " << COP1334[i].name << ": " << endl;
  247. cout << "----------------------------------------------------" << endl;
  248. getline(cin, temp2);
  249. if(cin.fail()){
  250. cin.clear();
  251. cin.ignore(1337, '\n');
  252. }
  253. user_Input2 = temp2[0];
  254. cout << "----------------------------------------------------" << endl;
  255. cout << "You are about to assign " << user_Input2 << " to " << COP1334[i].name << endl;
  256. cout << "Enter P to proceed or C to cancel: " << endl;
  257. cout << "----------------------------------------------------" << endl;
  258. getline(cin, temp3);
  259. user_Input3 = temp3[0];
  260. switch (user_Input3){
  261. case 'p':
  262. case 'P':
  263. COP1334[i].finalGrade = user_Input2;
  264. assignCount++;
  265. break;
  266. case 'c':
  267. case 'C':
  268. break;
  269. default:
  270. cout << "Invalid input!" << endl;
  271. }
  272.  
  273. break;
  274. case'p':
  275. case'P':
  276. //implement if...else code
  277. if (enrolled=1){
  278. cout << "There are no other students." << endl;
  279. break;
  280. }
  281. else
  282. if(assignCount = enrolled){
  283. cout << "All final grades are already assigned" << endl;
  284. break;
  285. }
  286. else{
  287. assignFinalGrade;
  288. }
  289. break;
  290. default:
  291. cout << "Invalid input!" << endl;
  292. }
  293. while(user_Input2 != 'C');
  294. }
  295. }
  296. }
  297.  
  298. void dropStudent (student COP1334[], int& enrolled, int i){
  299. for(int i=0; i<enrolled; i++){
  300. if(searched_Name.compare(COP1334[i].name) == 0){
  301. for(int j=i; j<enrolled-1; j++){
  302. COP1334[j] = COP1334[j+1];
  303. }
  304. enrolled--;
  305. }
  306. }
  307. if(enrolled == 0){
  308. empty = true;
  309. }
  310. }
  311.  
  312. int binarySearch(int COP1334[], int& enrolled, int value)
  313. {
  314. int first = 0,
  315. last = enrolled - 1,
  316. middle,
  317. position = -1;
  318. bool found = false;
  319.  
  320. while (!found && first <= last)
  321. {
  322. middle = (first + last)/2;
  323. if (COP1334[middle] == value)
  324. {
  325. found = true;
  326. position = middle;
  327. }
  328. else if (COP1334[middle] > value)
  329. last = middle - 1;
  330. else
  331. first = middle + 1;
  332. }
  333. return position;
  334. }
  335. void sortName(student COP1334[], int& enrolled){
  336. for(int i=0; i<enrolled-1; i++){
  337. bool sorted = false;
  338. for(int j=1; j<=enrolled-1; j++){
  339. if(COP1334[j-1].name > COP1334[j].name){
  340. cout << "a at "<< j << " is: " << COP1334[j].name <<endl;
  341. cout << "a at "<< j-1 <<" is: " << COP1334[j-1].name <<endl;
  342. swap(COP1334, j, j-1);
  343. sorted = true;
  344. }
  345. }
  346. if(!sorted){
  347. break;
  348. }
  349. }
  350. }
  351. void sortGPA(student COP1334[], int& enrolled){
  352. for(int i=0; i<enrolled-1; i++){
  353. bool sorted = false;
  354. for(int j=1; j<=enrolled-1; j++){
  355. if(COP1334[j-1].gpa > COP1334[j].gpa){
  356. cout << "a at "<< j << " is: " << COP1334[j].gpa <<endl;
  357. cout << "a at "<< j-1 <<" is: " << COP1334[j-1].gpa <<endl;
  358. swap(COP1334, j, j-1);
  359. sorted = true;
  360. }
  361. }
  362. if(!sorted){
  363. break;
  364. }
  365. }
  366. }
  367. void swap(student COP1334[], int x, int y){
  368. student temp;
  369. temp = COP1334[x];
  370. COP1334[x] = COP1334[y];
  371. COP1334[y] = temp;
  372. }
  373. /*void checkArray (student COP1334[], int& enrolled, int i, int& assignCount){
  374. for(int i=0; i<enrolled; i++){
  375. if(COP1334[i].finalGrade == 'N'){
  376. assignFinalGrade(COP1334, enrolled, assignCount);
  377. }
  378. else
  379. break;
  380. }
  381. }*/
Add Comment
Please, Sign In to add comment