我正在尝试使用 UserPrompt 函数的返回值存储 numofterms,但除了输入参数的 double 值之外,它一直在寻找字符串。有什么办法可以解决这个问题吗?
const string PROGDESCRIPTION = "Program will approximate the natural logarithm of 2 ";
double UserPrompt(string prompt, double terms)
{
cout << prompt;
cin >> terms;
while(terms <= 0)
{
cout << "ERROR - Input must be positive and non-zero! Please Try again. ";
}
return terms;
}
int main()
{
double numofterms;
cout << PROGDESCRIPTION << endl << endl << endl;
UserPrompt("Enter the number of terms to compute: ", numofterms);
numofterms = UserPrompt(numofterms);
cout << numofterms;
}
最佳答案
改变这个:
double UserPrompt(string prompt, double terms)
{
对此:
double UserPrompt(string prompt)
{
double terms;
还有这个:
UserPrompt("Enter the number of terms to compute: ", numofterms);
numofterms = UserPrompt(numofterms);
对此:
numofterms = UserPrompt("Enter the number of terms to compute: ");
关于C++函数返回值存储变量问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25043096/