我的C ++程序没有执行我的cout代码

时间:2013-08-27 13:04:28

标签: c++ std cout

我正在学习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;
}

5 个答案:

答案 0 :(得分:6)

一旦您的代码执行此行

return 0;

不会执行您的代码的其他行 - 从实用的角度来看,您的程序将会结束。将此行向下移动,使其成为main()函数执行的最后一行代码。

答案 1 :(得分:2)

您的问题是您在做任何事情之前从main返回:

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

    // no code after return gets executed

}

答案 2 :(得分:1)

你的return 0;应该在main的末尾,而不是开头

答案 3 :(得分:0)

请重新定位“返回0;”声明

答案 4 :(得分:0)

由于main是一个返回整数的函数,main函数的执行主要是返回一些整数值。一旦返回该值,该函数就会假定其作业已完成,因此不再持有该程序的控制权。

您的代码:

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

其实你想做什么:

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