简体   繁体   English

我的C ++程序未执行我的cout代码

[英]My C++ Program isn't executing my cout code

I am learning C++, and I am trying to make a simple program which prints 5 variables, as my book said to do this, but it is not executing my code. 我正在学习C ++,并且正尝试制作一个简单的程序来打印5个变量,正如我的书所说的那样,但是它没有执行我的代码。

#include<iostream>
using namespace std;
int main()
{
    //Program Code below
    return 0;
    char letter;    letter = 'A'; //Declared, then initialized
    int number;    number = 100; //Declared, then initialized
    float decimal = 7.5;   //Declared AND initialized
    double pi = 3.14159;   //Declared AND initialized
    bool isTrue = false; //Declared AND initialized
    cout<<"Char letter: "<<letter<<endl;
    cout<<"Int number: "<<number<<endl;
    cout<<"Float decimal: "<<decimal<<endl;
    cout<<"Double pi: "<<pi<<endl;
    cout<<"Bool isTrue: "<<isTrue<<endl;
}

As soon as your code executes this line 您的代码一旦执行此行

return 0;

no other lines of your code will be executed - from a practical point of view, your program will have ended. 您的代码的其他行将不会执行-从实际的角度来看,您的程序将结束。 Move this line down so that it is the last line of code executed by your main() function. 向下移动此行,以便它是main()函数执行的代码的最后一行。

Your problem is that you are returning from main before doing anything: 您的问题是您在做任何事情之前都从main返回:

int main()
{
    return 0; // HERE!!

    // no code after return gets executed

}

Your return 0; 您的return 0; should be at the end of main, not the start 应该在main的结尾,而不是开始

Please re-position the "return 0;" 请重新定位“返回0”; statement 声明

Since main is a function that returns an integer, the execution of the main function is primarily to return some integral value. 由于main是一个返回整数的函数,因此main函数的执行主要是返回某个整数值。 As soon as the value is returned, the function assumes its job is complete and hence does no longer hold the control of the program. 一旦返回该值,该函数将假定其工作已完成,因此不再保留程序的控制权。

Your code: 您的代码:

#include<iostream>
using namespace std;
int main()
{
    return 0; // Function thinks its job is done hence it ignores everything after it. 
    ...some other code
}

Actually what you wish to do: 实际上,您想做的是:

#include<iostream>
using namespace std;
int main()
{
    ... useful code
    return 0;  // Okay. Returning is alright, as the useful job is done.
}

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

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