我正在尝试将numofterms与我的UserPrompt函数的返回值一起存储,但是除了输入参数中的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: ");