好的,谢谢大家的意见。我已经解决了很多问题。现在当我编译它时,它在main中给出了一个错误,我调用了getPercent()来说:
error: cannot convert ‘std::string’ to ‘std::string*’ for argument ‘3’
to ‘void getPercent(int, std::string*, std::string*, std::string*)’
有什么可以解决这个问题?
#include <iostream>
#include <string>
using namespace std;
string getYours()
{
cout << "Enter your DNA sequence: " ;
string sequence;
cin >> sequence;
return sequence;
}
int getNumber()
{
cout << "Enter the number of potential relatives: ";
int number;
cin >> number;
cout << endl;
return number;
}
void getNames(int number, string name[])
{
for (int i = 0; i < number; i++)
{
cout << "Please enter the name of relative #" << i + 1 << ": ";
cin >> name[i];
}
}
void getSequences(int number, string name[], string newsequence[])
{
cout << endl;
for (int i = 0; i < number; i++)
{
cout << "Please enter the DNA sequence for " << name[i] << ": ";
cin >> newsequence[i];
}
}
void getPercent(int number, string name[], string sequence[],
string newsequence[])
{
cout << endl;
int count = 0;
for (int i = 0; i < number; i++)
{
if (sequence[i] == newsequence[i])
count = count + 10;
cout << "Percent match for " << name[i] << ": " << count << "%";
}
}
int main()
{
string sequence = getYours();
int number = getNumber();
string name[50];
string newsequence[50];
getNames(number, name);
getSequences(number, name, newsequence);
getPercent(number, name, sequence, newsequence);
return 0;
}
答案 0 :(得分:3)
一些问题:
getYours()
没有副作用。你可能想要分配给sequence
中的字符串main()
,而是分配给一个局部变量,当它超出范围时会被销毁。std::vector
)。10
getPercent()
而不是number
元素(如果这是您想要的)。getPercent()
中,count
未初始化为0. getPercent()
中的逻辑似乎有点奇怪,所以我不确定你要做什么。sequence
中的参数newsequence
和getPercent()
实际上是相同的类型,并且编译正常。可能还有其他一些我忽略的事情。