简体   繁体   English

C ++-使用数组

[英]C++ - Working with Arrays

The Motor vehicle Administration has asked you to write a program that grades the written portion of the Driver's License exam. 机动车管理局要求您编写一个程序,对驾驶执照考试的书面部分进行评分。 The Exam has a 20 multiple choice questions with the answers for the questions 1…20 respectively shown in the list below; 考试有20个多项选择题,下表分别显示了1…20的答案。

A   B   D   A   B   B   B   C   C   D   A   C   C   C   D   A   B   D   D   B

It is assumed that a total of 10 students take the exam each day. 假设每天总共有10名学生参加考试。 Your program should store the correct answer shown above in an Array. 您的程序应将上面显示的正确答案存储在数组中。 It should then ask each student's answer for the 20 questions and the answers should be stored in another array. 然后,它将向每个学生的答案询问20个问题,并将答案存储在另一个数组中。 After the answers have been entered, the program should display a message indicating whether a student passed or failed the exam.(A student must correctly answer 18 out of 20 questions to pass the exam).It should then display the total number of correctly answered questions and the total number of incorrectly answered questions by each student. 输入答案后,程序应显示一条消息,指示学生是否通过了考试(学生必须正确回答20个问题中的18个才能通过考试),然后应显示正确回答的总数问题和每个学生错误回答的问题总数。 Your program should also display the percent of students who passed the test in a given day. 您的程序还应显示在特定日期通过测试的学生百分比。

#include <iostream>
using namespace std;

int main()
{
    // Declare variables and constants
    int correctAnswers = 0;
    const int QUESTIONS = 20;
    char answers[QUESTIONS] = {'A', 'B', 'D', 'A', 'B', 'B', 'B', 'C', 'C',  'D', 'A', 'C', 'C', 'C', 'D', 'A', 'B', 'D', 'D', 'B'};

    for(int count = 0; count < QUESTIONS; count++)
    {
        cout << "Please enter your answer for Question " << (count + 1) << endl;
        cin >> answers;
    }
    if(answers[0] == 'A')
    {
        correctAnswers++;
    }
    else if(answers[1] = 'B')
    {
        correctAnswers++;
    }

    return 0;
}    

So I have begun to write this code for the problem statement listed above. 因此,我已经开始为上面列出的问题陈述编写此代码。 This is what I have so far. 到目前为止,这就是我所拥有的。 I started making if statements for each of the answers, and if they inputted the correct answer, the result will add 1 to the counter. 我开始为每个答案制作if语句,如果他们输入正确答案,结果将加1。 My question is, Is there a better way to get the users answers and check to see if they are correct, or is what I am doing now, the best way to do it? 我的问题是,是否有更好的方法来获取用户答案并检查他们是否正确,或者我现在正在做的最好方法是?

In the specification you must store the list of correct answers in an array. 在规范中,您必须将正确答案列表存储在数组中。 the answer of each student must be too saved in an array. 每个学生的答案都必须保存在数组中。 After getting the answers of a student you can iterate over the answers and compare them with the expected answers. 得到学生的答案后,您可以遍历答案并将它们与预期答案进行比较。

  char correct_answers[QUESTIONS] = {'A', 'B', 'D','A','B', 'B','B','C','C', 'D','A','C' ,'C' , 'C' ,'D' ,'A' ,'B','D', 'D',  'B'};
    char answers[QUESTIONS];
    for(int count = 0; count < QUESTIONS; count++) {
        cout << "Please enter your answer for Question " << (count + 1) << endl;
        cin >> (answers+count);
    }

    for(int count = 0; count < QUESTIONS; count++) {
        if(answers[count] == correct_answers[count]) {
            correctAnswers++;
        }
    }

Lets see.. you want to cin on something else, not answers because then your overwriting what you set as the Answer Key! 让我们来看看..您想在其他内容上找到答案,而不是答案,因为那会覆盖您设置为答案键的内容!

When you look you check answers[count] == inputValue from cin. 当您查看时,检查cin的答案[count] == inputValue。

basically.. some pseudo code.. 基本上..一些伪代码..

char inputValue;

loop ..
    cin << inputValue;
    answers[count] == inputValue  ...  
        Then ++correctAnswer ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM