C++函数返回值存储变量问题

标签 c++ function parameters return

我正在尝试使用 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/

相关文章:

c++ - 使用其指针屏蔽寄存器 - AVR 端口

function - 检查函数的参数是否为函数

python - 我需要 fillcolor 来读取 Python 中的值

function - Matlab计时器未定义函数或变量

python - 这些代码行是做什么的? (运动模拟)

memory - 由于自定义启动器,设置 Java 应用程序的虚拟机最大内存而无需访问 VM 参数?

c++ - 读取和写入文件 C++

c++ - GDB:我们如何从 std::tuple 中提取值

c++ - 如何在 32 位 Linux Mint 中构建 64 位 FastCGI 库?

python-3.x - 使用 tkinter.after 将参数传递给回调