Write a C++ program to allow the user to create a test bank of questions. The program should first ask the user how many questions he or she wishes to create. This quantity will be the first line in the test bank. The user should now be prompted for all information for each question, and then that question is written out to the test bank in the exact format specified in the Phase 2 Individual Project.
For each question, the following information should be gathered:
Question type: Multiple choice (MC) or True/False (TF)
Question value
Actual question
If the question is TF, then get answer
If the question is MC, then get the number of options followed by each option and finally the answer
This program should be robust and allow users who make mistakes the ability to correct them (exception handling).
This is what I have so far:
#include <iostream>
#include <fstream>
#include <cctype>
#include <cstring>
#include <stdlib.h> /* srand, rand */
#include <iomanip>
#include <limits.h>
#include <cmath>
#include <algorithm>
#include <vector>
#include <stack>
#include <string.h>
using namespace std;
int main()
{
ofstream outFile;
outFile.open ("output.txt");
int totalQuestion;
string option, type, question, answer;
int totalOptions;
cout << "Welcome to Michael's Quiz. Enter total number of
questions: ";
cin >> totalQuestion;
outFile << totalQuestion << endl;
for (int i = 0; i < totalQuestion; ++i)
{
cout << "\nQuestion " << (i+1)
<<endl;
cout << "Enter question type(TF/MC):
";
cin >> type;
if(type == "TF")
{
cin.ignore();
cout << "Enter
question: ";
getline(cin,question);
cout << "Enter
answer(True/False): ";
cin >> answer;
outFile << type
<< endl;
outFile <<
question << endl << answer << endl;
}
if(type == "MC")
{
cin.ignore();
cout << "Enter
question: ";
getline(cin,question);
cout << "Enter number
of options: ";
cin >>
totalOptions;
outFile << type
<< endl;
outFile <<
question << endl;
for (int j = 0; j <
totalOptions; ++j)
{
cin.ignore();
cout << "Enter option " << (j+1) <<": ";
getline(cin , option);
outFile << option << endl;
}
cout << "Enter
answer(correct option): ";
cin >> answer;
outFile << answer
<< endl;
}
}
outFile.close();
return 0;
}
/*
output:
Enter total number of question: 4
Question 1
Enter question type(TF/MC): TF
Enter question: There are 50 states in America?
Enter number of options: 2
Enter option 1: True
Enter option 2: False
Enter answer(True/False): True
Question 2
Enter question type(TF/MC): MC
Enter question: Donald Trump is the _____ President of the United
States?
Enter number of options: 4
Enter option 1: A 1st
Enter option 2: B 30th
Enter option 3: C 40th
Enter option 4: D 45th
Enter answer(correct option): D
Question 3
Enter question type(TF/MC): TF
Enter question: 1+2=3?
Enter number of options: 2
Enter option 1: True
Enter option 2: False
Enter answer(True/False): True
Question 4
Enter question type(TF/MC): TF
Enter question: The ocean is orange?
Enter number of options: 2
Enter option 1: True
Enter option 2: False
Enter answer(True?False): False
output.txt
4
TF
There are 50 states in America?
True
MC
Donald Trump is the ____ President of the United States of
America?
A 1st
B 30th
C 40th
D 45th
D
TF
1+2=3?
True
TF
The ocean is orange?
False
*/
Answer :
/* Let us complete your program */
#include <iostream>
#include <fstream>
#include <cctype>
#include <cstring>
#include <stdlib.h>
#include <iomanip>
#include <limits.h>
#include <cmath>
#include <algorithm>
#include <vector>
#include <stack>
#include <string.h>
using namespace std;
int main()
{
ofstream outFile;
outFile.open ("output.txt");
// A text file 'output.txt' will be created or opened in the same
directory of the .cpp file
int totalQuestion;
string option, type, question, answer;
int totalOptions;
try{
cout << "Welcome to Michael's Quiz. Enter total number of
questions: ";
cin >> totalQuestion;
outFile << totalQuestion << endl;
for (int i = 0; i < totalQuestion; ++i)
{
cout << "\nQuestion " << (i+1) <<endl;
cout << "Question type: Multiple choice (MC) or True/False
(TF) : ";
cin >> type;
if(type == "TF")
{
cin.ignore();
cout << "Enter the question : ";
getline(cin,question);
cout << "Enter the answer(True/False): ";
cin >> answer;
outFile << type << endl;
outFile << question << endl << answer <<
endl;
}
else if(type == "MC")
{
cin.ignore();
cout << "Enter the question : ";
getline(cin,question);
cout << "Enter the number of options for the question :
";
cin >> totalOptions;
outFile << type << endl;
outFile << question << endl;
cin.ignore(); /* This line should be out of the loop so that we don't have to press Enter twice */
for (int j = 0; j < totalOptions; ++j)
{
cout << "Enter option number " << (j+1) <<" :
";
getline(cin , option);
outFile << option << endl;
}
cout << "Enter the correct answer : ";
cin >> answer;
outFile << answer << endl;
}
else
{
throw "Wrong type of Question"; // Exception Handling
}
}
}
catch(const char* msg) // Catch Block to handle the
exception
{
cerr << msg << endl;
}
outFile.close();
return 0;
}
OUTPUT :
Write a C++ program to allow the user to create a test bank of questions. The...
fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string> #include <iomanip> using namespace std; struct book { int ISBN; string Author; string Title; string publisher; int Quantity; double price; }; void choice1(book books[], int& size, int MAX_SIZE) { ifstream inFile; inFile.open("inventory.txt"); if (inFile.fail()) cout <<"file could not open"<<endl; string str; while(inFile && size < MAX_SIZE) { getline(inFile, str); books[size].ISBN = atoi(str.c_str()); getline(inFile, books[size].Title); getline(inFile, books[size].Author); getline(inFile, books[size].publisher); getline(inFile,...
This is for a C++ program: I'm almost done with this program, I just need to implement a bool function that will ask the user if they want to repeat the read_course function. I can't seem to get it right, the instructions suggest a do.. while loop in the main function. here is my code #include <iostream> #include <cstring> #include <cctype> using namespace std; const int SIZE = 100; void read_name(char first[], char last[]); void read_course(int & crn, char des[],...
Fix my code, if I the song or the artist name is not on the vector, I want to user re-enter the correct song or artist name in the list, so no bug found in the program #include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; class musicList{ private: vector<string> songName; vector<string> artistName; public: void addSong(string sName, string aName){ songName.push_back(sName); artistName.push_back(aName); } void deleteSongName(string sName){ vector<string>::iterator result = find(songName.begin(), songName.end(), sName); if (result == songName.end()){ cout << "The...
C++ getline errors I am getting getline is undefined error messages. I would like the variables to remain as strings. Below is my code. #include <iostream> #include<string.h> using namespace std; int index = 0; // variable to hold how many customers are entered struct Address //Structure for the address. { int street; int city; int state; int zipcode; }; // Customer structure struct Customer { string firstNm, lastNm; Address busAddr, homeAddr; }; // Functions int displayMenu(); Customer getCustomer(); void showCustomer(Customer);...
working on a program in c++ The user enters an email address into your program. You must cuteverything after the @ symbol and output it. #include <iostream> #include <string> using namespace std; int main() { string email_adress = ""; //email get the info cout <<"what is your email adress? "; getline(cin, email_adress) ; cin.ignore('@'); // now we put into writitng in sentence cout <<"the email you entered is: " << email_adress << ". " <<"your email adress domian is :"...
I keep getting errors and i am so confused can someone please help and show the input and output ive tried so many times cant seem to get it. main.cpp #include <iostream> #include <vector> #include <string> #include "functions.h" int main() { char option; vector<movie> movies; while (true) { printMenu(); cin >> option; cin.ignore(); switch (option) { case 'A': { string nm; int year; string genre; cout << "Movie Name: "; getline(cin, nm); cout << "Year: "; cin >> year; cout << "Genre: "; cin >> genre; //call you addMovie() here addMovie(nm, year, genre, &movies); cout << "Added " << nm << " to the catalog" << endl; break; } case 'R': { string mn; cout << "Movie Name:"; getline(cin, mn); bool found; //call you removeMovie() here found = removeMovie(mn, &movies); if (found == false) cout << "Cannot find " << mn << endl; else cout << "Removed " << mn << " from catalog" << endl; break; } case 'O': { string mn; cout << "Movie Name: "; getline(cin, mn); cout << endl; //call you movieInfo function here movieInfo(mn, movies); break; } case 'C': { cout << "There are " << movies.size() << " movies in the catalog" << endl; // Call the printCatalog function here printCatalog(movies); break; } case 'F': { string inputFile; bool isOpen; cin >> inputFile; cout << "Reading catalog info from " << inputFile << endl; //call you readFromFile() in here isOpen = readFile(inputFile, &movies); if (isOpen == false) cout << "File not found" << endl;...
C++ programming I need at least three test cases for the program and at least one test has to pass #include <iostream> #include <string> #include <cmath> #include <iomanip> using namespace std; void temperatureCoverter(float cel){ float f = ((cel*9.0)/5.0)+32; cout <<cel<<"C is equivalent to "<<round(f)<<"F"<<endl; } void distanceConverter(float km){ float miles = km * 0.6; cout<<km<<" km is equivalent to "<<fixed<<setprecision(2)<<miles<<" miles"<<endl; } void weightConverter(float kg){ float pounds=kg*2.2; cout<<kg<<" kg is equivalent to "<<fixed<<setprecision(1)<<pounds<<" pounds"<<endl; } int main() { string country;...
The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried to divide the code in 3 parts (Patient.h, Patient.cpp and Main.cpp), but it is giving me errors. Patient.h #ifndef PATIENT_H #define PATIENT_H #include <string> #include "Patient.cpp" using namespace std; class Patient{ private : string firstname; string lastname; string location; static int cnt; int id; public : Patient(string, string, string);...
#include<iostream> #include<string> #include<iomanip> using namespace std; /* ********* Class Car ************* ********************************* */ class Car { private: string reportingMark; int carNumber; string kind; bool loaded; string choice; string destination; public: Car() { reportingMark = ""; carNumber = 0; kind = "Others"; loaded = 0; destination = "NONE"; } ~Car() { } void setUpCar(string &reportingMark, int &carNumber, string &kind, bool &loaded, string &destination); }; void input(string &reportingMark, int &carNumber, string &kind, bool &loaded,string choice, string &destination); void output(string &reportingMark, int &carNumber,...
c++ programming : everything is done, except when you enter ("a" ) in "F" option , it does not work. here is the program. #include <iostream> #include <string> #include <bits/stdc++.h> #include <iomanip> #include <fstream> using namespace std; #define MAX 1000 class Inventory { private: long itemId; string itemName; int numberOfItems; double buyingPrice; double sellingPrice; double storageFees; public: void setItemId(long id) { itemId = id; } long getItemId() { return itemId; } void setItemName(string name) { itemName = name; } string...