我在C++编程中创建和执行原型函数遇到了麻烦。 我已经开始学习C++编程,只是在用不同的命令来处理问题,而且很难使用原型函数并执行它们。我用代码块做所有的事情。我的代码只是设置一个密码,让用户输入密码继续。看起来很简单,对吧?就我现在而言,这就是我所拥有的。我没有在这方面取得任何进展,因为我很早就发现了错误 #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; }

我在C++编程中创建和执行原型函数遇到了麻烦。 我已经开始学习C++编程,只是在用不同的命令来处理问题,而且很难使用原型函数并执行它们。我用代码块做所有的事情。我的代码只是设置一个密码,让用户输入密码继续。看起来很简单,对吧?就我现在而言,这就是我所拥有的。我没有在这方面取得任何进展,因为我很早就发现了错误 #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; },c++,function-prototypes,C++,Function Prototypes,当我按原样运行这个程序时,cout行不会出现在屏幕上,程序终止,什么也不做。请帮我做这个。非常节俭。好的,除了声明nCombi后缺少分号之外,您的代码还有一些问题 在int getPasswordint的声明和定义中,您声明该函数应采用整数,但在调用该函数时未提供整数。此外,该函数还返回一个未捕获的整数 我已经在下面提交了一些修复 #include<cstdlib> #include<cstdio> #include<iostream> using names

当我按原样运行这个程序时,cout行不会出现在屏幕上,程序终止,什么也不做。请帮我做这个。非常节俭。

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

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

我已经在下面提交了一些修复

#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;
}

我看到的第一件事是,您正在向主函数传递参数。这是一个no-no,因为主函数是首先被调用的函数,因此程序无法向其传递参数。所以我建议你把它改成简单的int main{…}

接下来的事情是,您已经定义了getPassword函数来接受参数nPassword,但是当您调用该函数时,您没有传递任何参数,因此我假设您的函数getPassword没有被调用。此问题有两个修复程序:

使getPassword不接受任何参数

b将参数传递给getPassword。但是,考虑到您的代码的方式,不建议这样做。nPassoword似乎是要存储用户输入的密码的变量;如果您将其作为参数传递,这意味着您已经拥有输入的密码,因此调用函数getPassword是没有意义的

因此,在修复“a”之后,我建议执行以下操作:

int getPassword(){
    int nPassword;

    std::cout << "Please enter password..." << std::endl;
    std::cin >> nPassword;
    return nPassword;
}
在单个代码中,上述内容如下所示:

#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;
}

当然,如果您希望用户一直输入密码直到密码正确,您可以将主函数的内容设置为while循环,直到输入的密码正确为止。希望这有帮助。

您的函数需要一个int参数,但您调用它时没有int参数。您的意思是在main中使用cin并将该值传递给getPassword吗?粘贴后,它将不会编译,更不用说运行-在const int nCombo=3141之后缺少分号,您正在调用getPassword;如果没有必需的INT参数,则getPassword接受INT类型,您没有传递任何内容。我很惊讶。此外,在使用变量nPassword之前,您会立即覆盖该函数中的变量nPassword。这使得按值传递函数变得毫无用处。尽可能不使用函数原型,而是对函数进行重新排序,以便在使用函数的任何代码行之前定义函数。如果有在多个文件中调用的函数,则必须使用函数原型。但是,如果函数仅在定义函数的源代码所在的文件中使用,则在使用函数之前,通常更容易获得函数定义及其主体,因此函数定义可用作原型。所以把函数getPassword移到main上面,去掉原型。非常感谢!你的代码是完美的。我不敢相信我把变量等同于getPassword的返回。我仍然不太明白为什么不能将参数传递给main?你确定吗?
#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;
}