-
-
Save mikewerz/9c1c7a77f7d2fbe77af5 to your computer and use it in GitHub Desktop.
Simple Flashcard-like App
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <fstream> | |
#include <stdlib.h> | |
#include <string> | |
using namespace std; | |
//Load and Display Functions | |
void loadQuestions(); | |
void displayQuestion(int); | |
void displayAllQuestions(); | |
//Write and Create Functions | |
void firstTimePopulate(); | |
void createNewQuestion(); | |
void writeQuestions(); | |
//Global Variables | |
struct Question{ | |
char *question; | |
char *answer; | |
} questions[100]; | |
char * fileName; | |
int numQuestions; | |
int main() { | |
cout<<"Specify your questions file:"; | |
cin.getline(fileName = new char[128], 128); | |
loadQuestions(); | |
int choice; | |
while(choice != 4){ | |
cout<<"\n\nWhat would you like to do?\n" | |
"0. Display All Questions\n" | |
"1. Add Questions\n" | |
"2. Ask 10 Questions\n" | |
"3. Questions File Info\n" | |
"4. Exit"<<endl; | |
cin>>choice; | |
cin.get(); | |
if (choice == 0){ | |
displayAllQuestions(); | |
} else if (choice == 1){ | |
createNewQuestion(); | |
} else if (choice == 2){ | |
for (int reps = 0; reps < 10; reps++) | |
displayQuestion(rand() % numQuestions); | |
} else if (choice == 3) | |
cout<<"You have "<<numQuestions<<" questions in your file "<<fileName<<endl; | |
} | |
cout<<"Exiting..."; | |
return 0; | |
} | |
void loadQuestions(){ | |
ifstream readFile; | |
readFile.open(fileName); | |
if (readFile.is_open() == false){ | |
readFile.close(); | |
numQuestions = 0; | |
firstTimePopulate(); | |
return; | |
} | |
numQuestions = 0; | |
while(!readFile.eof() && numQuestions < 100){ | |
readFile.getline(questions[numQuestions].question = new char[128], 128); | |
readFile.getline(questions[numQuestions].answer = new char[128], 128); | |
if (*questions[numQuestions].question != '\0') | |
numQuestions++; | |
} | |
readFile.close(); | |
} | |
void displayQuestion(int questionNumber){ | |
cout<<"Q: "<<questions[questionNumber].question<<endl; | |
cin.get(); | |
cout<<"A: "<<questions[questionNumber].answer<<endl; | |
cin.get(); | |
} | |
void displayAllQuestions(){ | |
cout<<"Questions in "<<fileName<<endl; | |
for (int i = 0; i < numQuestions; i++){ | |
cout<<"Q: "<<questions[i].question<<endl; | |
cout<<"A: "<<questions[i].answer<<endl<<endl; | |
} | |
} | |
void firstTimePopulate(){ | |
cout<<"It looks like you have zero questions in your question file!"<<endl; | |
cout<<"Lets add a new question..."<<endl; | |
createNewQuestion(); | |
} | |
void createNewQuestion(){ | |
cout<<"What would you like your question to be?"<<endl; | |
cin.getline(questions[numQuestions].question = new char[128], 128); | |
cout<<"What would you like your answer to be?"<<endl; | |
cin.getline(questions[numQuestions].answer = new char[128], 128); | |
numQuestions++; | |
writeQuestions(); | |
} | |
void writeQuestions(){ | |
ofstream writeFile; | |
writeFile.open(fileName); | |
int i = 0; | |
while (i < numQuestions){ | |
if (*questions[i].question != '\0'){ | |
writeFile<<questions[i].question<<"\n"; | |
writeFile<<questions[i].answer<<"\n"; | |
} | |
i++; | |
} | |
writeFile.close(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment