我有一个 C++ 测试问题,要求我们编写一个程序:1)要求用户输入一串答案 a、b、c 和 d:(示例:ACDBDA)2)要求用户输入一个字符串是答案键:(例如 DBADCD)(我们还需要输入一些东西以确保答案键的长度与答案相同,但这没什么大不了的)3)打印输入答案正确的百分比,即与答案键匹配。
其中大部分都还不错,但是我不确定如何比较两个字符串以查看它们的不同之处。有没有办法让一个变量等于字符串中的一个字符,然后可以将其与表示该字符的变量与另一个字符串在同一位置进行比较?这是我唯一想到的方法。我并不是真的在寻找特定的代码或有人为我做这件事,只需要一些正确方向的指针。
谢谢!
好的,所以我在这方面取得了一些进展,我想我已经接近了。现在的问题是我在注释行上收到“表达式必须是类类型”错误。我认为问题出在我传递参数的方式上,但我似乎无法弄清楚如何解决它:(。我在谷歌上搜索了几篇参数传递文章,但似乎没有人谈到传递参数然后得到它的长度或像我在程序中那样比较它。任何帮助将不胜感激。这是代码:
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
string getAnswers(string answers)
{
cout << "What are your answers to this questionless quiz? BE WARY OF CHOOSING INCORRECTLY\n:";
cin >> answers;
return answers;
}
string getKey(string key)
{
cout << "Just kidding. What are the answers? \n:";
cin >> key;
return key;
}
double getPercentage(double percentage)
{
int total;
int correct;
getAnswers();
getKey();
total = getAnswers.size(); // "error: expression must have class type"
for (i=0; i>total; i++)
{
int j = 0;
if (getAnswers.at(j) = getKey.at(j)) // "error: expression must have class type" (for both getAnswers and getKey)
correct++;
j++;
}
percentage = (correct/total) * 100;
}
int main()
{
getAnswers();
getKey();
cout << getPercentage(answers, key); //Also I'm really not sure how to get the parameters from previous functions into this one here. Maybe I shoudln't even add parameters and just show the function?
return 0;
}