2016-08-03 23 views
1

私はC++で小さな技術者を訓練するために電卓プロジェクトをプログラミングしていましたが(うまくいけば、私には達成感を与えてくれます)、私はwhileループ。ループが期待どおり/期待通りに機能しない

本質的に、プログラムは、ユーザに 'モード' /コマンド(例えば、乗算、除算など)を求め、適切なコマンドを呼び出すように促します。終了したら、それらを最初に戻し(whileループは本質的に)、再び開始する(0を返す)、終了するオプションを返す(1を返す)必要があります。しかし、たとえそれが初めてであっても、すぐに終了します。私は何か間違っているのですか? C++プログラミングを真剣に誤解していますか?または何?ここ は私のコードです:(ほとんどの機能を切り出し)

#include <iostream> 


using namespace std; 

int cMode(); // function prototypes 
int add(); 
int sub(); 
int mult(); 
int divide(); 
int sqr(); 

int main() { // main function start 
    do { 
      cMode(); 
    } while (0); 

    return 0; 
} 

int cMode() { // mode selection func 
    int mode; 
    cout<<"Please select which mode you would like to avail from the following:\n"; 
    cout<<"1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n5. Sqaure root finder\n6. Exit\n"; 
    cin>>mode; 
    if (mode == 1) { 
     return add(); 
    } 
} 

int add() { // addition function 
    int x, y; // variables 

    cout<<"Please type the first number to add: "; 
    cin>>x; 
    cin.ignore(); 
    cout<<"Please type the second number to add: "; 
    cin>>y; 
    x = x + y; 
    cout<<"The answer is "<< x <<"."; 
    return 0; 
} 

とにかく、誰かがはるかに高く評価されるだろう手助けができれば。また、さらに2つの小さな質問があります。< < "...." < < x < <;最後に ""を含める必要がありますか?私は彼らに間違いを見せていました。なぜ、私は叫び声の行の最後にendlを置いていませんか? "

ありがとうございます!

+1

...偽と最後に評価しながら、 0); 'おそらく'} while(1); ' – DimChtz

+0

@DimChtzいいえ、おそらく' while(variable) 'でなければなりません。ここで' variable'は関数の戻り値で設定されています。 –

+0

あなたが好きなエディタを使い、あなたのコードに貼り付けるつもりです。もし誰もが好きなキーバインドを設定しようとすると、誰もそれを望んでいない;) – jaggedSpire

答えて

1

問題はここにある:

int main() { // main function start 
    do { 
     cMode(); 
    } while (0); 
} 

間での条件はそれが終了し0あるので、それはその後、do {}一部を実行します。 do-whileは、条件が非ゼロ値と評価されるまで実行されます。あなたはおそらく、変数を作成し、cMode()からの戻り値を格納し、その後のようなものが欲しい

:これが動作するために、ところで

int main() { // main function start 
    int ret=0; 
    do { 
     ret=cMode(); 
    } while (ret); 
} 

を、あなたは確かのみcMode() 0を返すことを確認する必要がありますユーザーがモード6(終了)を選択した場合そしてもう一つは、多分それは質問のフォーマットの問題ですが、あなたはmain()I don't think that works in c++中にあなたadd()機能を持っている:それは、コードの書式設定の問題だったよう

NVMは、あなたの質問を編集します。

+0

またはちょうど 'while(cMode()){}' – jaggedSpire

+0

これも機能します! – Wajahat

+0

私は0はループの継続であり、1は終了すると考えていました。申し訳ありませんが、noobの間違い! –

0

あなたはやるべき :

#include <iostream> 


using namespace std; 

int cMode(); // function prototypes 
int add(); 
int sub(); 
int mult(); 
int divide(); 
int sqr(); 

int main() { // main function start 
    while (cMode() == 0); //This keep the loop 
    return 0; 
} 

int cMode() { // mode selection func 
    int mode; 
    cout<<"Please select which mode you would like to avail from the following:\n"; 
    cout<<"1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n5. Sqaure root finder\n6. Exit\n"; 
    cin>>mode; 
    if (mode == 1) { 
     return add(); //all functions should return 0 in success 
    } 
    if (mode == 6) { 
     return 1; //returning 1 exits the loop 
    } 
} 

int add() { // addition function 
    int x, y; // variables 

    cout<<"Please type the first number to add: "; 
    cin>>x; 
    cin.ignore(); 
    cout<<"Please type the second number to add: "; 
    cin>>y; 
    x = x + y; 
    cout<<"The answer is "<< x <<"."; 
    return 0; 
} 

の問題は、((0)しばらく(false)を言うと同じであるので、それは一方で} `

+0

ありがとうございます。しかし、上記の答えは、私のためにすでに明らかになっています。 –

+0

@ wolf_adventures1909問題はない、アイデアは同じことを行うためのさまざまな方法を示すことです。 –

関連する問題