2016-09-17 9 views
-5

変数番号が0のときに私のプログラムがなぜ動いているのか教えてもらえますか?それ以上の質問や情報の入力をせずに停止してはいけませんか?変数が0のときにプログラムが実行されるのはなぜですか?

元のプログラムはwhile文を使っていました....教授はそれをdo文に変更するように頼んだ。整数を求めた後に0を入力した場合、そこで停止してはいけませんか?代わりに、2番目の質問をします。

数値が0ではない間にこれを行うと、数字が0のときに何が行われるのですか?

おかげ

#include <iostream> 

using namespace std; 

int main() { 

    int number, product = 1, count = 0; 

    cout << "Enter an integer number to be included in the product" << endl << "or enter 0 to end the input: "; 
    cin >> number; 

    do { 

     product = product * number; 
     count++; 
     cout << "Enter an integer number to be included in the product" << "or enter 0 to end the input: "; 
     cin >> number; 

    } while (number != 0); 

    if (count > 0) { 

     cout << endl << "The product is " << product << "." << endl; 
    } 

} 
+0

このような問題を解決する正しいツールは、デバッガです。スタックオーバーフローを尋ねる前に、コードを一行ずつ進める必要があります。詳しいヘルプは、[小さなプログラムをデバッグする方法(Eric Lippert)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)を参照してください。最低限、問題を再現する[最小、完全、および検証可能](http://stackoverflow.com/help/mcve)の例と、その問題を再現するためのデバッガ。 –

+0

http://en.cppreference.com/w/cpp/language/do – juanchopanza

+0

Do-whileはおそらくあなたがここで探しているループではありません – Bettorun

答えて

1

提示コードのロジックは、入力を求めるいくつかの計算を実行し、(ループ本体の内側からこの時間)を再び尋ね、今0

を確認することですプログラムが意図した通常の方法で終了する前に少なくとも2つの質問があります。

別の方法としては、(通常は)強制的にコンソールプログラムを中止するはCtrl +Cを使用することができます。


プログラム内のロジックは、ループ半として知られており、ループの中央から出ることにより、冗長性なしで符号化することができます。これにより

#include <iostream> 
using namespace std; 

int main() { 
    int number, product = 1, count = 0; 
    for (;;) { 
     cout << "Enter an integer number to be included in the product" << "or enter 0 to end the input: "; 
     cin >> number; 
     if (number == 0) { 
      break; 
     } 
     product = product * number; 
     count++; 
    } 
    if (count > 0) { 
     cout << endl << "The product is " << product << "." << endl; 
    } 
} 

免責条項の最初の型付きでプログラムが終了を修正:コードコンパイラの手で触れていません。

#include <iostream> 
using namespace std; 

int main() { 
    int number, product = 1, count = 0; 
    do { 
     cout << "Enter an integer number to be included in the product" << "or enter 0 to end the input: "; 
     cin >> number; 
     if (number != 0) { 
      product = product * number; 
      count++; 
     } 
    } while (number != 0); 
    if (count > 0) { 
     cout << endl << "The product is " << product << "." << endl; 
    } 
} 

をそれはもう少し厄介だが、一部の人々が好む:do ... while(number != 0)、あなたがコメントで説明し、その後、あなたの教授が必要と改訂質問として、それはこのようになりますで表現


ジャンプフリーのコード。

また、免責条項:コンパイラの手によって触れられていないコード。

+1

Downvoter、あなたの愚かなdownvoteを説明してください。同じように、あなた自身を見せてください。これはちょうどそれがそれを読むのに十分なほど幸運なので、将来あなたを無視することが容易になります。 –

+0

教授は具体的に私たちにwhile(number!= 0)からdo {code} while(number!= 0)にコードを変更するよう依頼しました。 –

+0

@ThomasWalker:あなたはこの情報/要件を追加する部分にはっきりとマークされた**編集**部分で質問を修正できます。 –

-1

doを使用しているので、これを試してください。

#include <iostream> 

using namespace std; 

int main() { 

    int number, product = 1, count = 0; 

    cout << "Enter an integer number to be included in the product" << endl << "or enter 0 to end the input: "; 
    cin >> number; 

    while (number != 0) { 

     product = product * number; 
     count++; 
     cout << "Enter an integer number to be included in the product" << "or enter 0 to end the input: "; 
     cin >> number; 

    } 

    if (count > 0) { 

     cout << endl << "The product is " << product << "." << endl; 
    } 

} 

あなたがを使用したい場合は、しばらくはこのようなものです:ループは条件が何であれ、少なくとも一度実行されるかのブロックを意味している

#include <iostream> 

using namespace std; 

int main() { 

    int number, product = 1, count = 0; 


    do { 
     cout << "Enter an integer number to be included in the product" << "or enter 0 to end the input: "; 
     cin >> number; 
     if(!number) 
      break; 
     product = product * number; 
     count++; 


    } while(true); 

    if (count > 0) { 

     cout << endl << "The product is " << product << "." << endl; 
    } 

} 
+0

これは元のコードでした。教授は、do whileループを代わりに使用するよう具体的に教えてくれました。私は単に彼の指示に従っています。なぜそれがうまくいかないのか分かりません。 –

+0

あなたは悪い教授をしています。do while a condicionalの後にdo whileループを使いたいのであれば、{if(number!= 0)break; ...} while(真); –

+0

@ThomasWalkerはあなたの問題を解決しましたか? –

0

を行います。成功または失敗する。なぜなら、このタイプのループは実行から始まり、逆の条件をテストするからです。

は、以下の例を考えてみましょう。あなたのケースで

#include <iostream> 

using namespace std; 

int main() 
{ 

    int a = 7; 

    do 
    { 
     cout << "Hello " << endl; // this line will be reached whatever the condition below succeeds or fails 
     a--; // this line also will be reached whatever the condition below succeeds or fails 
    } while(a > 100); 

    return 0; 
} 

あなたが実行する前にテストしたいあなたは、「少なくとも一度実行」そうwhileループを使用したくないので、そう-whileループを使用しないでくださいあなたの例は次のようになります:

#include <iostream> 
using namespace std; 

int main() { 

    int number, product = 1, count = 0; 

    cout << "Enter an integer number to be included in the product" << endl << "or enter 0 to end the input: "; 
    cin >> number; 

    while (number != 0) 
    { 

     product = product * number; 
     count++; 
     cout << "Enter an integer number to be included in the product" << "or enter 0 to end the input: "; 
     cin >> number; 

    } 

    if (count > 0) { 

     cout << endl << "The product is " << product << "." << endl; 
    } 

    return 0; 
} 
関連する問題