Need help for my program
how can I save the answer in my question in answer.txt(void studentAnswer)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
const int size = 10;
struct Quiz
{
char question;
string Question;
};
void login(string &name, string &ID);
char quizType();
void Multiple(Quiz ans[]);
void question(Quiz answer[]);
void studentAnswer(char *quiz, Quiz answer[], Quiz ans[]);
int main()
{
char type;
string StudentID;
string StudentNames;
Quiz ans[size];
Quiz answer[size];
login(StudentNames, StudentID);
type = quizType();
if (type == 'a'&&'A')
Multiple(ans);
else if (type == 'b'&&'B')
question(answer);
else
cout << "Enter 'a' or 'b' only!";
void studentAnswer( char *type, Quiz answer[], Quiz ans[]);
system("pause>0");
return 0;
}
void login(string &name, string &ID)
{
string studentName;
string studentID;
ofstream info;
info.open("StudentInfo.txt");
cout << "Please enter your name: ";
cin >> studentName;
cout << "Please enter your student ID: ";
cin >> studentID;
info << studentName << " " << studentID;
info.close();
}
char quizType()
{
char Q;
cout << "Choose type of quiz" << endl << endl
<< "Enter a for Multiple Choice" << endl
<< "Enter b for True/False" << endl << endl;
cout << "Enter your chosen quiz type: ";
cin >> Q;
return Q;
}
void Multiple(Quiz ans[])
{
string line;
ifstream dataQuiz("Quiz.txt");
for (int i = 0; i < size; i++)
{
while (dataQuiz.good())
{
getline(dataQuiz,line);
cout << line << endl;
cout << "ans:";
cin >> ans[i].question;
}
dataQuiz.close();
}
}
void question(Quiz answer[])
{
string line;
ifstream data("Quiz2.txt");
for (int i = 0; i < size; i++)
{
while (data.good())
{
getline(data, line);
cout << line << endl;
cout << "ans:";
cin>> answer[i].Question;
}
data.close();
}
}
void studentAnswer(char *quiz, Quiz answer[], Quiz ans[])
{
ofstream answ;
answ.open("Answer.txt");
if (quiz == "a" || "A")
answ << ans[size].question;
else if (quiz == "b" || "B")
answ << answer[size].Question;
answ.close();
}
|
Not sure if this is what you want I struggled following your code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
const int size = 10;
struct Quiz
{
char question;
string Question;
};
void login(string &name, string &ID);
char quizType();
void Multiple(Quiz ans[]);
void question(Quiz answer[]);
void studentAnswer(char quiz, Quiz *answer, Quiz *ans);
const int SIZE = 10;
int main()
{
ofstream makeFile;
makeFile.open("Quiz.txt");
makeFile.close();
ofstream makeFiles;
makeFiles.open("Quiz2.txt");
makeFiles.close();
char type;
string StudentID;
string StudentNames;
Quiz ans[SIZE];
Quiz answer[SIZE];
login(StudentNames, StudentID);
type = quizType();
if ((type == 'a') || (type == 'A'))
Multiple(ans);
else if ((type == 'b') || (type == 'B'))
question(answer);
else
cout << "Enter 'a' or 'b' only!";
studentAnswer(type, answer, ans);
system("pause");
return 0;
}
void login(string &name, string &ID)
{
string studentName;
string studentID;
ofstream info;
info.open("StudentInfo.txt");
cout << "Please enter your name: ";
cin >> studentName;
cout << "Please enter your student ID: ";
cin >> studentID;
info << studentName << " " << studentID;
info.close();
}
char quizType()
{
char Q;
cout << "Choose type of quiz" << endl << endl
<< "Enter a for Multiple Choice" << endl
<< "Enter b for True/False" << endl << endl;
cout << "Enter your chosen quiz type: ";
cin >> Q;
return Q;
}
void Multiple(Quiz ans[])
{
string line;
ifstream dataQuiz;
dataQuiz.open("Quiz.txt");
for (int i = 0; i < SIZE; i++)
{
while (dataQuiz.good())
{
getline(dataQuiz, line);
cout << "ans:";
cout << line << endl;
cin >> ans[i].question;
}
}
dataQuiz.close();
}
void question(Quiz answer[])
{
string line;
ifstream data;
data.open("Quiz2.txt");
for (int i = 0; i < SIZE; i++)
{
while (data.good())
{
getline(data, line);
cout << "ans:";
cout << line << std::endl;
cin >> answer[i].Question;
}
}
data.close();
}
void studentAnswer(char quiz, Quiz *answer, Quiz *ans)
{
ofstream answ;
answ.open("Answer.txt");
if (quiz == 'a' || quiz == 'A')
{
answ << ans->question;
answ.close();
}
else if (quiz == 'b' || quiz == 'B')
{
answ << answer->Question;
answ.close();
}
}
|
Last edited on
//Comments are a great thing in any programming language!
Topic archived. No new replies allowed.