简体   繁体   English

我的代码只会运行main函数,其他函数不会在C ++中编译

[英]My code will only run the main function, other functions just wont compile in C++

i am a beginner in programming in C++. 我是C ++编程的初学者。 In my class we were recently showed to use functions and parameters. 在我的课堂上,我们被证明使用函数和参数。 We were left an assignment where we are supposed to work with functions, except the only problem is i can't seem to get a start. 除了一个唯一的问题是我似乎无法开始之外,我们还剩下了应该使用函数的任务。 I have wrote two functions so far. 到目前为止,我已经编写了两个函数。 My main function, which asks for input from the user. 我的主要功能,要求用户输入。 Also another function which uses this input to create a new integer i will later need. 另外一个使用此输入来创建新整数的函数,稍后将需要。 I'm sure there are several mistakes in my code, but right now i really only need to know why only my main function will execute. 我确定我的代码中有几个错误,但是现在我真的只需要知道为什么只有我的main函数才能执行。 I have been looking for hours, and switching stuff around, just to get another function other than the main to run, but my it will only run the main function and then the program will end. 我一直在寻找时间,并在其他地方进行切换,只是为了使除主程序以外的其他功能可以运行,但是我只会运行主功能,然后程序结束。 After inputting data from the user, the program will end. 从用户输入数据后,程序将结束。 I haven't been able to get any other function to run other than the main, since i started this assignment. 自从我开始这项任务以来,除了主要功能之外,我没有其他任何功能可以运行。 I am using visual studio 2017. Sorry for the trouble. 我正在使用Visual Studio2017。很抱歉遇到麻烦。

#include <iostream>
#include <cmath>
#include <string>

using namespace std;
int digits(int zip);

int main() {
    int zip = 0;
    cout << "Enter zipcode: ";
    cin >> zip;

    return 0;
}

int digits(int zip){
    int d5 = int();
    d5 = zip % 10;
    cout << "test: " << d5 << endl;
    return d5;

        }

You need to call the function digits 您需要调用功能digits

 #include <iostream>
    #include <cmath>
    #include <string>

    using namespace std;
    int digits(int zip);

    int main() {
        int zip = 0;
        cout << "Enter zipcode: ";
        cin >> zip;
        int data = digits(zip);
        cout<<"test: "<<data<<endl;
        return 0;
    }

    int digits(int zip){
        int d5 = int();
        d5 = zip % 10;
        // cout << "test: " << d5 << endl;
        return d5;
        }

C++ looks for the main function and executes whatever is in it. C ++查找主函数并执行其中的任何内容。 In your case, it creates a new variable 'zip', asks the user for input, then inserts that value into the zip variable. 在您的情况下,它将创建一个新的变量“ zip”,询问用户输入,然后将该值插入zip变量。 Then it returns 0 and stops running. 然后返回0并停止运行。 You must call the function digits() inside int main(). 您必须在int main()中调用函数digits()。 Like so: 像这样:

int main() {
    int zip = 0;
    cout << "Enter zipcode: ";
    cin >> zip;
    digits(zip);

    return 0;
}

int digits(int zip){
    int d5 = int();
    d5 = zip % 10;
    cout << "test: " << d5 << endl;
    return d5;
}

Additionally, be aware that after executing the digits() function this program will simply return 0, not manipulating the 'd5' variable (returned by int digits()) in any way. 此外,请注意,在执行digits()函数之后,该程序将仅返回0,而不以任何方式操纵“ d5”变量(由int digits()返回)。

Also, it is not a good idea to cout anything inside additional functions. 同样,在附加函数中引用任何内容也不是一个好主意。 This makes it difficult to reuse the function. 这使得难以重用该功能。 To keep the function as versatile as possible, make sure it only performs one task. 为了使功能尽可能地通用,请确保它仅执行一项任务。 In your case it should look something like this: 在您的情况下,它应如下所示:

int main() {
    int zip = 0;
    cout << "Enter zipcode: ";
    cin >> zip;
    cout << "test: " << digits(zip) << endl;

    return 0;
}

int digits(int zip){
    int d5 = int();
    d5 = zip % 10;
    return d5;
}

You need to call the function digits before hand otherwise the program will skip over the function entirely so the above code should actually look something like this: 您需要事先调用函数digits ,否则程序将完全跳过该函数,因此上述代码实际上应如下所示:

#include <iostream>
#include <cmath>
#include <string>

using namespace std;
int digits(int zip);

int main() {
    int zip = 0;
    cout << "Enter zipcode: ";
    cin >> zip;

    return 0;
}

int digits(int zip){
    int d5 = int();
    d5 = zip % 10;
    cout << "test: " << d5 << endl;
    return d5;

        }

Also on a side note in d5 = zip % 10; 同样在d5 = zip % 10;旁注d5 = zip % 10; if your looking for it to be more accurate just define d5 as a double or a float. 如果您想更精确地将d5定义为双精度或浮点型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM