测验或测试程序需要帮助 [英] Quiz or test program help needed
问题描述
大家好,
首先,我会让大家知道我真的很新.
我需要一个使用数组和循环来创建20个问题测验的简单C ++程序的帮助.程序需要完成的唯一操作是允许用户输入20个多项选择答案A,B,C或D,并将用户输入的答案与一组正确的答案进行比较,以计算输入的正确答案的百分比.如果有人可以向我指出正确的方向,或者给我一个简短的例子说明我应该拥有的东西,那将不胜感激.
到目前为止,这是我所拥有的..但是数量不多.
Hello all,
First off, I''ll just let you guys know that I''m really new at this.
I need some help with a simple C++ program using arrays and loops to create a 20 question quiz. The only thing the program needs to accomplish is allow the user to enter 20 multiple choice answers A,B,C, or D and compare the user entered answers to a correct set of answers to calculate a percentage of correct answers entered. If anyone could just point me in the right direction, or give me a quick example of what I should have, that would be greatly appreciated.
This is what I have so far.. but its not much.
#include <iostream>
using namespace std;
int main()
{
const int NUM_QUESTIONS = 20; // Number of Questions
char user_answers[NUM_QUESTIONS]; // Array with 20 elements
char correct_answers[NUM_QUESTIONS];
int count; // Loop counter variable
int correct;
char correct_answers[count] = {B, D, A, A, C, A, B, A, C, D, B, C, D, A, D, C, C, B, D, A}
cout << "\t\t*******************************\t\t\n";
cout << "\t\t*****Driver''s License Exam*****\t\t\n";
cout << "\t\t*******************************\t\t\n\n";
for (count = 0; count < NUM_QUESTIONS; count++)
{
cout << (count + 1) << ". ";
cin >> user_answers[count];
}
if (user_answers[count]==correct_answers[count])
{
correct++;
}
cout << endl;
cout << correct;
return 0;
}
谢谢
Thanks
推荐答案
<br />int count; // Loop counter variable<br />int correct;<br />char correct_answers[count] = {B, D, A, A, C, A, B, A, C, D, B, C, D, A, D, C, C, B, D, A}<br />
这只会引起问题.由于未初始化计数,因此correct_answers [count]可能应该是correct_answers [NUM_QUESTIONS].
This can only cause problems. correct_answers[count] should probably be correct_answers[NUM_QUESTIONS] since count is not initialized.
类似这样的东西
Something like this
const int NUM_QUESTIONS = 20; // Number of Questions
char user_answers[NUM_QUESTIONS]; // Array with 20 elements
char correct_answers[] = {"BDAACABACDBCDADCCBDA"};
int count; // Loop counter variable
int correct;
cout << "\t\t*******************************\t\t\n";
cout << "\t\t*****Driver''s License Exam*****\t\t\n";
cout << "\t\t*******************************\t\t\n\n";
correct = 0;
for (count = 0; count < NUM_QUESTIONS; count++)
{
char answer;
do
{
cout << (count + 1) << ". ";
cin >> answer;
answer = toupper(answer);
if (answer < ''A'' || answer > ''D'')
answer = ''?'';
cout << endl;
} while (answer == ''?'');
user_answers[count] = answer;
if (user_answers[count]==correct_answers[count])
{
correct++;
}
}
cout << endl;
cout << correct << " answers correct" << endl;
您可能需要在重复问题的周围添加一些花哨的内容,但这应该涵盖了基础知识.
You probably need to add some fancy bits around the repeat of the question but this should cover the basics.
这篇关于测验或测试程序需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!