简体   繁体   English

问题比较字符串的答案键 (C++)

[英]Issue Comparing strings for an Answer Key (C++)

I'm working on a midterm project for my coding class, and while I've gotten the majority of kinks worked out I'm struggling with comparing two string values and determining if they are equal or not.我正在为我的编码课程做一个中期项目,虽然我已经解决了大部分问题,但我正在努力比较两个字符串值并确定它们是否相等。 The strings in question are ANSWERKEY and studentAnswers .有问题的字符串是ANSWERKEYstudentAnswers The former is a constant that the latter is compared to.前者是与后者相比较的常数。

The code in question is as follows.有问题的代码如下。

if (studentAnswers == ANSWERKEY)
    {
        percentScore = 100.0;

        cout << "Score: " << percentScore << " % " << 'A' << endl;
    }

    else if (studentAnswers != ANSWERKEY)
        {
            int count = 0;
            double answerCount = 0.0;
            while (count < ANSWERKEY.length())
                {
                    if (studentAnswers.substr(count, count+1) == ANSWERKEY.substr(count, count+1)
                        {
                            answerCount++;
                            count++;
                        }
                    else
                        {
                            cout << "Incorrect answer." << endl;
                            count++;
                        }
                }
        percentScore = ((answerCount) / (double)ANSWERKEY.length()) * 100.0;
        cout << "Percent score is " << percentScore << "%" << endl;
        }

The exact issue I'm facing is that I can't work out a better way to compare the strings.我面临的确切问题是我无法找到更好的方法来比较字符串。 With the current method, the output is the following:使用当前方法,输出如下:

The intro to the code runs fine.代码简介运行良好。 Only when I get to checking the answers against the key, in this case "abcdefabcdefabcdefab", do I run into issues.只有当我根据密钥检查答案时,在这种情况下是“abcdefabcdefabcdefab”,我才会遇到问题。 Regardless of what characters are changed, the program marks roughly half of all characters as mismatching and drops the score down because of it.无论更改了哪些字符,程序都会将大约一半的字符标记为不匹配,并因此降低分数。

I've thought of using a pair of arrays, but then I can't find a solution to setting up the array when some values of it are empty.我想过使用一对数组,但是当数组的某些值为空时,我找不到设置数组的解决方案。 If the student's answers are too short, eg only 15 characters long, I don't know how to compare the blank space, or even store it in the array.如果学生的答案太短,例如只有 15 个字符长,我不知道如何比较空格,甚至不知道如何将其存储在数组中。

Thank you for any help you can give.感谢您提供的任何帮助。

First:第一的:

if (studentAnswers == ANSWERKEY)
{...}
else if (studentAnswers != ANSWERKEY)
{ ...}

looks like an overkill when comparing strings.比较字符串时看起来有点矫枉过正。 And where is the else part ? else部分在哪里?

Second, this is risky.其次,这是有风险的。 Read the IEE754 and articles about cancellation , or even SO :阅读IEE754和关于取消甚至SO 的文章

double answerCount = 0.0;
...
answerCount++

Third:第三:

You are checking character by character using substr.您正在使用 substr 逐个字符地检查。 To me it feels like using a hammer to kill a bacteria.对我来说,感觉就像用锤子杀死细菌。

studentAnswers.substr(count, count+1) == ANSWERKEY.substr(count, count+1)

Fourth:第四:

What if studentAnswers is shorter than ANSWERKEY ?如果studentAnswersANSWERKEYANSWERKEY办?

Conclusion: You need to clarify inputs/expected outputs and use the debugger to better understand what is happening during execution.结论:您需要澄清输入/预期输出并使用调试器更好地了解执行期间发生的情况。 Carefully check all your variables at each step fo your program.在程序的每个步骤中仔细检查所有变量。

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

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