Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <limits>
- #include <fstream>
- #include <cstdlib>
- using namespace std;
- const int MAX_SIZE = 90;
- const int MAX_ARRY_SIZE = 1000;
- struct MianMenu
- {
- int numberword;
- char wordslist [MAX_SIZE];
- };
- struct QuizFormat
- {
- char question [MAX_SIZE];
- int choiseNumber;
- char choice [MAX_SIZE];
- int choiseNumber2;
- char choice2 [MAX_SIZE];
- int choiseNumber3;
- char choice3 [MAX_SIZE];
- int choiseNumber4;
- char choice4 [MAX_SIZE];
- int answer;
- };
- void displayQuiz(QuizFormat [], int );
- bool readQuizFromFile(string &fileName, QuizFormat quiz[], int &numQuestion);
- void displayMenu(MianMenu [], int );
- bool readMenuFromFile(string &fileName, MianMenu menu[], int &numMenu);
- int main()
- {
- QuizFormat myQuiz[MAX_ARRY_SIZE];
- MianMenu myMenu[MAX_ARRY_SIZE];
- int userMenupick;
- string userName;
- string fileName = "main-menu.txt";
- int numberOfMenu;
- cout << "What is your name?" << endl;
- getline(cin,userName);
- cout << "Please choose a quiz to do from the list below." << endl;
- if (!readMenuFromFile(fileName, myMenu, numberOfMenu))
- {
- cout << "Trouble locating the file. Exiting....." << endl;
- }
- else
- {
- displayMenu(myMenu, numberOfMenu);
- }
- do
- {
- cin >> userMenupick;
- if(cin.fail())
- {
- cin.clear();
- cout << "Only numbers." << endl;
- cin.ignore(numeric_limits<streamsize>::max(), '\n');
- }
- switch(userMenupick)
- {
- case 1:
- {
- cout << "US State Capitals." << endl;
- string fileName = "us-state-capitals.txt";
- int numberOfQuiz;
- if (!readQuizFromFile(fileName, myQuiz, numberOfQuiz))
- {
- cout << "Trouble locating the file. Exiting....." << endl;
- }
- else
- {
- displayQuiz(myQuiz, numberOfQuiz);
- }
- return 0;
- break;
- }
- case 2:
- {
- cout << "World Capital Cities." << endl;
- string fileName = "world-state-capitals.txt";
- int numberOfQuiz;
- if (!readQuizFromFile(fileName, myQuiz, numberOfQuiz))
- {
- cout << "Trouble locating the file. Exiting....." << endl;
- }
- else
- {
- displayQuiz(myQuiz, numberOfQuiz);
- }
- return 0;
- break;
- }
- case 3:
- {
- cout << "Video Games" << endl;
- string fileName = "video-games.txt";
- int numberOfQuiz;
- if (!readQuizFromFile(fileName, myQuiz, numberOfQuiz))
- {
- cout << "Trouble locating the file. Exiting....." << endl;
- }
- else
- {
- displayQuiz(myQuiz, numberOfQuiz);
- }
- return 0;
- break;
- }
- case 4:
- {
- cout << "C++." << endl;
- string fileName = "cpp.txt";
- int numberOfQuiz;
- if (!readQuizFromFile(fileName, myQuiz, numberOfQuiz))
- {
- cout << "Trouble locating the file. Exiting....." << endl;
- }
- else
- {
- displayQuiz(myQuiz, numberOfQuiz);
- }
- return 0;
- break;
- }
- case 5:
- {
- cout << "History." << endl;
- string fileName = "history.txt";
- int numberOfQuiz;
- if (!readQuizFromFile(fileName, myQuiz, numberOfQuiz))
- {
- cout << "Trouble locating the file. Exiting....." << endl;
- }
- else
- {
- displayQuiz(myQuiz, numberOfQuiz);
- }
- return 0;
- break;
- }
- case 6:
- {
- cout << "Exit" << endl;
- return 0;
- }
- break;
- default:
- cout << "Pick only 1,2,3,4,5,or 6." << endl;
- break;
- }
- }while (userMenupick > 1 || userMenupick < 6);
- return 0;
- }
- void displayQuiz(QuizFormat quiz[], int numQuestion)
- {
- int userawsure;
- double correctawsure = 0;
- double wrongawsure = 0;
- double avg;
- for (int i=0; i<numQuestion; i++)
- {
- if(cin.fail())
- {
- cin.clear();
- cout << "Only numbers." << endl;
- cout << "Pick only 1,2,3,4,5,or 6" << endl;
- cin.ignore(numeric_limits<streamsize>::max(), '\n');
- }
- cout << quiz[i].question << endl;
- cout << quiz[i].choiseNumber << "." << quiz[i].choice << endl;
- cout << quiz[i].choiseNumber2 << "." << quiz[i].choice2 << endl;
- cout << quiz[i].choiseNumber3 << "." << quiz[i].choice3 << endl;
- cout << quiz[i].choiseNumber4 << "." << quiz[i].choice4 << endl;
- cout << "Your choice:";
- cin >> userawsure;
- if(userawsure == quiz[i].answer)
- {
- cout << "correct" << endl;
- correctawsure++;
- }
- else if(userawsure == quiz[i].answer)
- {
- cout << "wrong" << endl;
- wrongawsure++;
- }
- cout << "You got " << correctawsure << "right." << endl;
- cout << "You got " <<wrongawsure << "wrong." << endl;
- if(correctawsure < wrongawsure)
- {
- avg = correctawsure/wrongawsure;
- avg = avg * 100.0;
- cout << "Your Avg " << avg << endl;
- }
- else
- {
- avg = wrongawsure/correctawsure;
- avg = avg * 100.0;
- cout << "Your Avg " << avg << endl;
- }
- }
- }
- bool readQuizFromFile(string &fileName, QuizFormat quiz[], int &numQuestion)
- {
- ifstream iFile;
- bool retVal = true;
- int i;
- char temp[MAX_SIZE];
- iFile.open(fileName.c_str());
- if (!iFile)
- retVal = false;
- else
- {
- i=0;
- while (!iFile.eof())
- {
- iFile.getline(quiz[i].question, MAX_SIZE, ',');
- iFile.getline(temp, MAX_SIZE, ',');
- quiz[i].choiseNumber = atoi (temp);
- iFile.getline(quiz[i].choice, MAX_SIZE, ',');
- iFile.getline(temp, MAX_SIZE, ',');
- quiz[i].choiseNumber2 = atoi (temp);
- iFile.getline(quiz[i].choice2, MAX_SIZE, ',');
- iFile.getline(temp, MAX_SIZE, ',');
- quiz[i].choiseNumber3 = atoi (temp);
- iFile.getline(quiz[i].choice3, MAX_SIZE, ',');
- iFile.getline(temp, MAX_SIZE, ',');
- quiz[i].choiseNumber4 = atoi (temp);
- iFile.getline(quiz[i].choice4, MAX_SIZE, ',');
- iFile.getline(temp, MAX_SIZE, ',');
- quiz[i].answer = atof (temp);
- char nextChar;
- nextChar = iFile.peek();
- if (nextChar == EOF)
- {
- numQuestion = i;
- break;
- }
- i++;
- }
- }
- iFile.close();
- return retVal;
- }
- void displayMenu(MianMenu menu[], int numMenu)
- {
- for (int i=0; i<numMenu; i++)
- {
- cout << menu[i].numberword << "." << menu[i].wordslist << endl;
- }
- }
- bool readMenuFromFile(string &fileName, MianMenu menu[], int &numMenu)
- {
- ifstream iFile;
- bool retVal = true;
- int i;
- char temp[MAX_SIZE];
- iFile.open(fileName.c_str());
- if (!iFile)
- retVal = false;
- else
- {
- i=0;
- while (!iFile.eof())
- {
- iFile.getline(temp, MAX_SIZE, ',');
- menu[i].numberword = atoi (temp);
- iFile.getline(menu[i].wordslist, MAX_SIZE, ',');
- char nextChar;
- nextChar = iFile.peek();
- if (nextChar == EOF)
- {
- numMenu = i;
- break;
- }
- i++;
- }
- }
- iFile.close();
- return retVal;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement