0

私は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;
}
4

5 に答える 5

6

コードがこの行を実行するとすぐに

return 0;

コードの他の行は実行されません実用的な観点から、プログラムは終了します。main() 関数によって実行されるコードの最後の行になるように、この行を下に移動します。

于 2013-08-27T13:05:57.670 に答える
2

あなたの問題は、main何かをする前に戻ってきていることです:

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

    // no code after return gets executed

}
于 2013-08-27T13:05:34.463 に答える
1

最初return 0;ではなく、メインの最後にある必要があります

于 2013-08-27T13:06:00.090 に答える
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.
}
于 2013-08-27T13:19:15.387 に答える
0

「return 0;」の位置を変更してください。声明

于 2013-08-27T13:10:00.813 に答える