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

C++ C++;函数返回值存储变量问题,c++,function,parameters,return,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 >>

我试图用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;

}
const string PROGDESCRIPTION=“程序将近似于2的自然对数”;
双用户提示(字符串提示,双术语)
{
术语;
而(术语改变了这一点:

double UserPrompt(string prompt, double terms)
{
为此:

double UserPrompt(string prompt)
{
    double terms;
numofterms = UserPrompt("Enter the number of terms to compute: "); 
这是:

UserPrompt("Enter the number of terms to compute: ", numofterms);
numofterms = UserPrompt(numofterms);
为此:

double UserPrompt(string prompt)
{
    double terms;
numofterms = UserPrompt("Enter the number of terms to compute: "); 
更改此项:

double UserPrompt(string prompt, double terms)
{
为此:

double UserPrompt(string prompt)
{
    double terms;
numofterms = UserPrompt("Enter the number of terms to compute: "); 
这是:

UserPrompt("Enter the number of terms to compute: ", numofterms);
numofterms = UserPrompt(numofterms);
为此:

double UserPrompt(string prompt)
{
    double terms;
numofterms = UserPrompt("Enter the number of terms to compute: "); 
更改此项:

double UserPrompt(string prompt, double terms)
{
为此:

double UserPrompt(string prompt)
{
    double terms;
numofterms = UserPrompt("Enter the number of terms to compute: "); 
这是:

UserPrompt("Enter the number of terms to compute: ", numofterms);
numofterms = UserPrompt(numofterms);
为此:

double UserPrompt(string prompt)
{
    double terms;
numofterms = UserPrompt("Enter the number of terms to compute: "); 
更改此项:

double UserPrompt(string prompt, double terms)
{
为此:

double UserPrompt(string prompt)
{
    double terms;
numofterms = UserPrompt("Enter the number of terms to compute: "); 
这是:

UserPrompt("Enter the number of terms to compute: ", numofterms);
numofterms = UserPrompt(numofterms);
为此:

double UserPrompt(string prompt)
{
    double terms;
numofterms = UserPrompt("Enter the number of terms to compute: "); 

<>我发现C++不喜欢你在函数中传递文字,所以像

char prompt[101] = "Enter the number of terms to compute: ";

UserPrompt(prompt, numofterms);

我不喜欢字符串函数,所以我使用字符数组。我会用任何方式工作。但是我不确定字符串是否像字符数组一样自动地被引用。

我发现C++不喜欢你在函数中传递文字。所以,做一些类似于

的事情。
char prompt[101] = "Enter the number of terms to compute: ";

UserPrompt(prompt, numofterms);

我不喜欢字符串函数,所以我使用字符数组。我会用任何方式工作。但是我不确定字符串是否像字符数组一样自动地被引用。

我发现C++不喜欢你在函数中传递文字。所以,做一些类似于

的事情。
char prompt[101] = "Enter the number of terms to compute: ";

UserPrompt(prompt, numofterms);

我不喜欢字符串函数,所以我使用字符数组。我会用任何方式工作。但是我不确定字符串是否像字符数组一样自动地被引用。

我发现C++不喜欢你在函数中传递文字。所以,做一些类似于

的事情。
char prompt[101] = "Enter the number of terms to compute: ";

UserPrompt(prompt, numofterms);


我不喜欢string函数,所以我使用的是字符数组。无论哪种方式我都可以。不过,我不确定字符串是否会像字符数组一样自动通过引用传递。

好吧,函数接受字符串,所以在调用它时需要传递字符串。那么,问题到底是什么?我认为传递
术语毫无意义e> 对于该函数,第二次调用显然是错误的。整个程序中还有几个对该函数的调用,只发布了这一部分,但感谢您的输入。您的代码有几个问题。主要是调用UserPrompt(“输入要计算的术语数:”,numofterms);而不为此函数分配一个临时的双变量来存储其返回值。另一个问题是,您还调用了函数UserPrompt(numofterms);只有一个参数。但是,函数UserPrompt接受两个参数,而不是一个参数,因此您缺少字符串参数。嗯,函数接受字符串,因此您需要在调用它时传递字符串。那么,问题到底是什么?我认为将
术语
传递给该函数毫无意义,第二个调用显然是完全错误。整个程序中还有几个对函数的调用,只发布了这一部分,但感谢您的输入。您的代码有几个问题。主要是调用UserPrompt(“输入要计算的术语数:”,numofterms);而不为此函数分配一个临时的双变量来存储其返回值。另一个问题是,您还调用了函数UserPrompt(numofterms);只有一个参数。但是,函数UserPrompt接受两个参数,而不是一个参数,因此您缺少字符串参数。嗯,函数接受字符串,因此您需要在调用它时传递字符串。那么,问题到底是什么?我认为将
术语
传递给该函数毫无意义,第二个调用显然是完全错误。整个程序中还有几个对函数的调用,只发布了这一部分,但感谢您的输入。您的代码有几个问题。主要是调用UserPrompt(“输入要计算的术语数:”,numofterms);而不为此函数分配一个临时的双变量来存储其返回值。另一个问题是,您还调用了函数UserPrompt(numofterms);只有一个参数。但是,函数UserPrompt接受两个参数,而不是一个参数,因此您缺少字符串参数。嗯,函数接受字符串,因此您需要在调用它时传递字符串。那么,问题到底是什么?我认为将
术语
传递给该函数毫无意义,第二个调用显然是完全错误。整个程序中还有几个对函数的调用,只发布了这一部分,但感谢您的输入。您的代码有几个问题。主要是调用UserPrompt(“输入要计算的术语数:”,numofterms);而不为此函数分配一个临时的双变量来存储其返回值。另一个问题是,您还调用了函数UserPrompt(numofterms);只有一个参数。但是,函数UserPrompt接受两个参数,而不是一个参数,因此您缺少字符串参数。@乔:不客气。不要忘记通过单击旁边的V来接受答案。@乔:不客气。不要忘记通过单击旁边的V来接受答案。@乔:不客气。不要忘记请点击旁边的V来接受答案。乔:不客气。别忘了点击旁边的V。我不确定告诉OP使用CHAR数组而不是字符串是很好的建议。Const可能会用文字来解决问题。我想这是我的大学C++语言风格。我们使用C字符串。从1开始,它们是C++规范的一部分,而字符串是一个附加组件,2)它只是很好地用来创建、传递和访问数组元素。但是,我认为很多(大多数)人可能只使用字符串。确实,好,const,好点。谢谢。我不确定告诉OP使用字符数组代替。