【问题标题】:I am having troubles with creating and executing prototype functions in c++ programming我在 C++ 编程中创建和执行原型函数时遇到了麻烦
【发布时间】:2014-07-04 21:48:16
【问题描述】:

我已经开始学习 c++ 编程,只是在玩弄不同的命令,似乎很难使用原型函数并执行它们。我对所有事情都使用 CodeBlocks。我的代码是简单地设置一个密码,并让用户输入密码以继续。看起来很简单,对吧?就我现在而言,这就是我所拥有的。 (我没有进一步了解,因为我很早就发现了错误)

#include<cstdlib>
#include<cstdio>
#include<iostream>
using namespace std;

// prototype declaration
int getPassword(int nPassword);
// declare combo to be matched
const int nCombo = 3141
int main(int nNumberofArgs, char* pszArgs[])
{
    getPassword();
    return 0;
}
// fetch password from user to compare to nCombo
int getPassword(int nPassword)
{
    cout << "Please enter password..." << endl;
    cin  >> nPassword;
    return nPassword;
}

当我按原样运行这个程序时,屏幕上没有出现 cout 行,程序终止,什么也没做。请帮我解决一下这个。非常令人沮丧。

【问题讨论】:

  • 您的函数需要一个 int 参数,但您在没有参数的情况下调用了它。您的意思是在main 中使用cin 并将该值传递给getPassword
  • 当你粘贴它时,它不会编译更不用说运行 - 你在 const int nCombo = 3141 之后缺少一个分号,你正在调用 getPassword();没有所需的 INT 参数。
  • getPassword() 采用 int 类型,你什么也没传递。我很惊讶它编译。此外,在使用之前,您会立即覆盖函数内部的变量nPassword。这使得按值传递它完全没用。
  • 尽可能不要使用函数原型,而是对函数重新排序,以便在使用该函数的任何代码行之前定义函数。如果您有在多个文件中调用的函数,则必须使用函数原型。但是,如果该函数仅在定义该函数的源代码所在的文件中使用,那么在使用该函数之前通常更容易获得函数定义及其主体,因此该函数定义用作原型。所以把函数getPassword()移到main()上面,去掉原型。

标签: c++ function-prototypes


【解决方案1】:

好的,除了在您声明nCombi 之后缺少分号之外,您的代码还有一些问题。

int getPassword(int) 的声明和定义中,您声明该函数应采用整数,但在调用该函数时未提供整数。此外,该函数返回一个您未捕获的整数。

我在下面提交了一些修复。

#include<cstdlib>
#include<cstdio>
#include<iostream>
using namespace std;

// prototype declaration
int getPassword();

// declare combo to be matched
const int nCombo = 3141;

int main(int nNumberofArgs, char* pszArgs[])
{
    // The main control loop. We continue this forever or until the user submits the correct password
    while (true)
    {
        // Capture input from the user. Save the result in pw
        int pw = getPassword();

        // Here we test if the user has submitted the correct password
        if (nCombo == pw)
        {
            cout << "You got it!" << endl;

            // Break out of the loop, and exit the program
            break;
        }
    }

    return 0;
}

// Fetch password from user to compare to nCombo
int getPassword()
{
    int nPassword = 0;

    cout << "Please enter password..." << endl;
    cin  >> nPassword;

    return nPassword;
}

【讨论】:

  • 非常感谢!您的代码完美无缺。我不敢相信我把变量等同于 getPassword() 的返回。我还是不太明白为什么 cout
【解决方案2】:

嗯,我看到的第一件事是您正在将参数传递给您的 main 函数。这是一个禁忌,因为 main 函数是第一个被调用的函数,因此程序无法将参数传递给它。所以我建议你把它改成简单的int main(){...}

接下来你已经定义了getPassword 函数来接收参数nPassword,但是当你调用函数时,你没有传递任何参数,所以我假设你的函数getPassword没有被调用。对此有两个修复:

a) 让getPassword 不接受任何争论。

b) 将争论传递给getPassword。但是,鉴于您的代码的方式,不建议这样做。似乎nPassoword 是您要存储用户输入密码的变量;如果你把它作为争论传递,那意味着你已经输入了密码,因此调用函数getPassword是没有意义的。

所以在修复“a”之后,我建议做类似的事情:

int getPassword(){
    int nPassword;

    std::cout << "Please enter password..." << std::endl;
    std::cin >> nPassword;
    return nPassword;
}

然后通过

调用函数
int nPassword;
nPassword = getPassword();

单个代码中的上述内容如下:

#include<iostream>

//protoype declaration
int getPassword();

//combo to check for
const int nCombo = 3141;

int main(){
    int nPassword;
    nPassword = getPassword();

    if (nPassword == nCombo)
    {
        std::cout << "Correct password!" << std::endl;
    }
    else
    {
        std::cout << "Incorrect password!" << std::endl;
    }

    return 0;
}

int getPassword(){

    int nPassword;

    std::cout << "Please enter password..." << std::endl;
    std::cin >> nPassword;
    return nPassword;
}

当然,如果您希望用户继续输入密码直到密码正确,您可以将main 函数的内容放入while loop 中,直到输入的密码正确为止。希望这会有所帮助。

【讨论】:

  • 无法将参数传递给main()?你确定吗?
猜你喜欢
  • 2021-06-06
  • 2022-01-03
  • 1970-01-01
  • 1970-01-01
  • 2011-08-28
  • 1970-01-01
  • 1970-01-01
  • 2023-02-11
  • 2022-01-05
相关资源
最近更新 更多