2017-04-03 6 views
1

私のクラスのこの課題を2,3日間おきにしようとしていますが、何か助けが必要です。C++のループとブール式

割り当ては、性別に応じて身長と体重の資格に基づいて受諾状況をユーザーに知らせるプログラムを作成することです。

プログラムの最後に、受け入れられた候補の数と、受け入れられた候補の平均を候補者の総数に出力します。

割り当て - https://www.saddleback.edu/faculty/slinker/CS1A/CS1A_Fall2013/Assignment8.pdf

我々は(結果のみに正しいメッセージを出力するための)スイッチ、条件演算子、および選択を使用することはできません。私たちは、ループや複雑なブール式

私がいる問題を使用することができます:

  1. なぜ彼らはそれらが受け入れられた場合に出力しての1つがされていない、私のすべての入力3は、有効な場合入力(高さまたは重さ)またはその両方が拒否された場合、それが出力されないのはなぜですか?ブール変数が正しくありませんか?もしそうなら、それをどうやって修正するのですか?

  2. Xを入力したときにループ/プログラムを終了できないのはなぜですか。私のwhileループは正しいかどうかですか?

  3. 「選択文ではない」選択肢がありますか?ここで

私のコード

#include <iostream> 
#include <iomanip> 
using namespace std; 

int main() 
{ 

    char gender; 
    int height; 
    int weight; 
    bool heightOK; 
    bool weightOK; 
    int candidateCount; 
    int validCandidateCount; 
    bool invalidGender; 
    bool invalidHeight; 
    bool invalidWeight; 
    float percentOutput; 

    candidateCount = 0; 
    validCandidateCount = 0; 

    cout << "Please enter the candidate's information (enter 'X' to exit)." 
    << endl; 

    do 
    { 
     cout << "Gender: "; 
     cin.get(gender); 

     cin.ignore (1000,'\n'); 
     invalidGender = (!(gender == 'm' || 
          gender == 'M' || 
          gender == 'f' || 
          gender == 'F')); 

     candidateCount = candidateCount + 1; 

     if(invalidGender) 
     { 
      cout << "***** Invalid gender; please enter M or F*****" <<  endl; 
     } 

    }while(invalidGender); 

    while (gender != 'X' || gender != 'x') 
    { 
     candidateCount = candidateCount + 1; 

     do 
     { 
      cout << "Height: "; 
      cin >> height; 

      invalidHeight = height < 24 || height > 110; 

      heightOK = ((gender == 'm' || gender == 'M') && 
         (height > 65 && height < 80)); 

      heightOK = heightOK || ((gender == 'f' || gender == 'F') && 
            (height > 62 && height < 75)); 

      if(invalidHeight) 
      { 
       cout << "***** Invalid height; please enter a height in inches between 24 and 110. *****" 
        << endl; 
      } 

     }while(invalidHeight); 


     do 
     { 
     cout << "Weight: "; 
     cin >> weight; 

     invalidWeight = weight < 50 || weight > 1400; 

     weightOK = ((gender == 'm' || gender == 'M') && 
         (weight > 130 && weight < 250)); 

     weightOK = weightOK || ((gender == 'f' || gender == 'F') && 
        (weight > 110 && weight < 185)); 

     if(invalidWeight) 
      { 

       cout << "***** Invalid weight; please enter a weight in lbs between 50 and 1400." 
        << endl; 
      } 

     }while(invalidWeight); 



     if(heightOK && weightOK) 
     { 
      cout << "This candidate has been ACCEPTED!" << endl; 

      validCandidateCount = validCandidateCount + 1; 
     } 
     else if (!heightOK) 
     { 
      cout << "This candidate has been rejected based on the HEIGHT requirement." 
       << endl; 
     } 
     else if (!weightOK) 
     { 
      cout << "This candidate has been rejected based on the WEIGHT requirement." 
       << endl; 
     } 
     else if (!(heightOK && weightOK)) 
     { 
      cout << "This candidate has been rejected based on the HEIGHT and WEIGHT requirements" 
       << endl; 
     } 
     do 
     { 
      cout << "Gender: "; 
      cin.get(gender); 
      cin.ignore (1000,'\n'); 

      candidateCount = candidateCount + 1; 

      if(invalidGender) 
      { 
      cout << "***** Invalid gender; please enter M or F*****" <<  endl; 
      } 
     }while(invalidGender); 


    } 

    cout << validCandidateCount << " candidate(s) accepted!" << endl; 

    percentOutput = validCandidateCount/candidateCount; 

    cout << "That's " << percentOutput <<"%!" << endl; 



return 0; 
} 
+1

コード全体。バグに加えて、キーボードが壊れているようです。 Tabキーが正しく機能しません。結果として、示されたコードは論理的で有意義な字下げがなく、ほとんど読めません。キーボードを固定する必要があります。結局のところ、プログラミング上の問題を他の人が理解できるように頼んでいるのであれば、できるだけ論理的な字下げを使用して、コードを読みやすくしてフォローする努力を少し費やすことができます。あなたのコードを正しくインデントしても気にしないなら、なぜ誰かがあなたを助けたいのですか? –

+0

@SamVarshavchik 今修正されましたか?投稿時にタブを使用できないことを気付かなかった。だから私はちょうど4つのスペースをした。 –

+0

あなたは 'invalidGender'をどこで宣言しましたか?あなたのコードを実行すると、コンパイラは私にエラーを返します: '|| ===ビルドファイル:" no project "の" no target "(コンパイラ:不明)=== | || 'int main()'関数内:|| 31 | error: 'invalidGender'はこのスコープ内で宣言されていませんでした|| 42 | error: 'invalidGender'はこのスコープ内で宣言されていませんでした。 || ===ビルドに失敗しました:2エラー、0警告(0分、1秒)=== | ' ' –

答えて

1

は、メインwhileループを持っており、条件べきです。

while(gender !='X' && gender!='x) 

あなたの選択コードに間違った条件文があります。

if(heightOK && weightOK) 
    { 
     cout << "This candidate has been ACCEPTED!" << endl; 

     validCandidateCount = validCandidateCount + 1; 
    } 
    else if (!heightOK) // you have written else if(heightOK) 
    { 
     cout << "This candidate has been rejected based on the HEIGHT requirement." 
      << endl; 
    } 
    else if (!weightOK) // you have written else if(weightOK) 
    { 
     cout << "This candidate has been rejected based on the WEIGHT requirement." 
      << endl; 
    } 
    else if (!(heightOK && weightOK)) 
    { 
     cout << "This candidate has been rejected based on the HEIGHT and WEIGHT requirements" 
      << endl; 
    } 

あなたがそうでなければ、それはあなたが代わりに無効な性別条件をX. を押して終了したい場合でも、の起動に置くことができる無限ループが発生します、do-while文の最後にそのinvalidgender条件を削除する必要がありますmain Whileループ。

invalidGender変数に値をもう一度割り当てる必要があります。それ以外の場合は、以前に格納された値を取得します。

invalidGender = (!(gender == 'm' || 
         gender == 'M' || 
         gender == 'f' || 
         gender == 'F')); 

私はいくつかの悪いニュースを持っている

#include <iostream> 
#include <iomanip> 
using namespace std; 

int main() 
{ 

char gender; 
int height; 
int weight; 
bool heightOK; 
bool weightOK; 
int candidateCount; 
int validCandidateCount; 
bool invalidGender; 
bool invalidHeight; 
bool invalidWeight; 
double percentOutput; 

candidateCount = 0; 
validCandidateCount = 0; 

cout << "Please enter the candidate's information (enter 'X' to exit)." 
<< endl; 

    cout << "Gender: "; 
    cin.get(gender); 

while (gender != 'X' && gender != 'x') 
{ 
    candidateCount = candidateCount + 1; 

    do 
    { 
     invalidGender = (!(gender == 'm' || 
         gender == 'M' || 
         gender == 'f' || 
         gender == 'F')); 

     if(invalidGender) 
     { 
      cout << "***** Invalid gender; please enter M or F*****" <<  endl; 
      cout << "Gender: "; 
      cin>>gender; 
      cin.ignore (1000,'\n'); 
     } 
    }while(invalidGender); 

    do 
    { 
     cout << "Height: "; 
     cin >> height; 

     invalidHeight = height < 24 || height > 110; 

     heightOK = ((gender == 'm' || gender == 'M') && 
        (height > 65 && height < 80)); 

     heightOK = heightOK || ((gender == 'f' || gender == 'F') && 
           (height > 62 && height < 75)); 

     if(invalidHeight) 
     { 
      cout << "***** Invalid height; please enter a height in inches between 24 and 110. *****" 
       << endl; 
     } 

    }while(invalidHeight); 


    do 
    { 
    cout << "Weight: "; 
    cin >> weight; 

    invalidWeight = weight < 50 || weight > 1400; 

    weightOK = ((gender == 'm' || gender == 'M') && 
        (weight > 130 && weight < 250)); 

    weightOK = weightOK || ((gender == 'f' || gender == 'F') && 
       (weight > 110 && weight < 185)); 

    if(invalidWeight) 
     { 

      cout << "***** Invalid weight; please enter a weight in lbs between 50 and 1400." 
       << endl; 
     } 

    }while(invalidWeight); 



    if(heightOK && weightOK) 
    { 
     cout << "This candidate has been ACCEPTED!" << endl; 

     validCandidateCount = validCandidateCount + 1; 
    } 
    else if (!heightOK) 
    { 
     cout << "This candidate has been rejected based on the HEIGHT requirement." 
      << endl; 
    } 
    else if (!weightOK) 
    { 
     cout << "This candidate has been rejected based on the WEIGHT requirement." 
      << endl; 
    } 
    else if (!(heightOK && weightOK)) 
    { 
     cout << "This candidate has been rejected based on the HEIGHT and WEIGHT requirements" 
      << endl; 
    } 


     cout << "Gender: "; 
     cin>>gender; 
     cin.ignore (1000,'\n'); 

} 

cout << validCandidateCount << " candidate(s) accepted!" << endl; 

percentOutput = (double)validCandidateCount/(double)candidateCount; 

cout << "That's " << percentOutput*100 <<"%!" << endl; 



return 0; 
} 
+0

こんにちはチャンディーニ、応答ありがとう。私は2番目のジェンダー入力(最後のdo-while)にinvalidGender変数の割り当てを再度追加しましたが、メインの間に最後のdoから無効なジェンダー条件をどこに置くべきか分かりません。 –

+0

最後のdoで無効な条件を削除した場合、代わりに何を追加しますか?私は空の状態でdoの中にwhile条件を残すことはできません。そして、メインループの最初にループを追加するにはどうしたらいいですか? –

+0

さて、私はそれをしたが、高さのループは(invalidGender) { 裁判所未満<<「*****場合 は { を行う追加しながら、私は直前に性別 –