2016-09-25 1 views
-1

私はGPA計算機を作成しています。私はOR(||)関数でクレジットの入力が正しいかどうかをチェックする問題を抱えています(間違っていると、再度質問する必要があります)。また、成績の入力が「I」または「W」の場合、プログラムはその情報を使用すべきではありません(何もしないでください)。 手伝っていただきありがとうございます。ここ は、コードは次のとおりです。GPA電卓。 ORの問題と0で割る

// Include Statements 
#include <iostream> 
using namespace std; 

//Program to calculate GPA 
int main() 
{ 
    // Program made by Jose Luis Landivar 
    // Fall 2016 
    cout << ""<< endl; 
    cout << "The following program will calculate your GPA "<< endl<<'\n'; 

    char grade; 
    int g = 1; 
    //variables for the algebraic operations needed 
    int credit = 0; 
    int totalcredit = 0; 
    int ptotal = 0; 
    double gpa, p = 1; 
    double count = 1; 
    double nclass; 
    string coursename, coursenumber; 


    cout << "Please enter the number of classes for the GPA calculator"<< endl; 
     cin >> nclass; 
    cout << ""<< endl; 
    while (count <= nclass) // While 
    { 
     //Information for the class 
     cout << "Please enter the course name for the class # "<< count << endl; 
      cin >> coursename; 
     cout << "Please enter the course number for the class # "<< count << endl; 
      cin >> coursenumber; 

     //Getting grades and credits from user 
     cout << "Enter the grade for the class # "<< count <<" (A,B,C,D,F,I,W)"<< endl; 
      cin >> grade; 
     if (grade == 'A' || grade == 'B' || grade == 'C' || grade == 'D' || grade == 'F' || grade == 'I' || grade == 'W') 

     { 

      // If for grades 
      if (grade == 'A') 
      { 
       g = 4; 
       cout << "Enter the number of credits for the class # "<< count <<" (1,2,3,4,5)"<< endl; 
        cin >> credit; 
       cout << ""<< endl; 
       if (credit == 5 || credit == 4 || credit == 3 || credit == 2 || credit == 1){ 
        p = g * credit; 
        totalcredit = totalcredit + credit; 
        ptotal = ptotal + p; 
       } 
       else {cout << "Input is incorrect\n"; 
       } 
      } 


      if (grade == 'B') 
      { 
       g = 3; 
       cout << "Enter the number of credits for the class # "<< count <<" (1,2,3,4,5)"<< endl; 
        cin >> credit; 
       cout << ""<< endl; 
       if (credit == 5 || credit == 4 || credit == 3 || credit == 2 || credit == 1){ 
        p = g * credit; 
        totalcredit = totalcredit + credit; 
        ptotal = ptotal + p; 
       } 
       else {cout << "Input is incorrect\n"; 
       } 
      } 

      if (grade == 'C') 
      { 
       g = 2; 
       cout << "Enter the number of credits for the class # "<< count <<" (1,2,3,4,5)"<< endl; 
        cin >> credit; 
       cout << ""<< endl; 
       if (credit == 5 || credit == 4 || credit == 3 || credit == 2 || credit == 1){ 
        p = g * credit; 
        totalcredit = totalcredit + credit; 
        ptotal = ptotal + p; 
       } 
       else {cout << "Input is incorrect\n"; 
       } 
      } 

      if (grade == 'D') 
      { 
       g = 1; 
       cout << "Enter the number of credits for the class # "<< count <<" (1,2,3,4,5)"<< endl; 
        cin >> credit; 
       cout << ""<< endl; 
       if (credit == 5 || credit == 4 || credit == 3 || credit == 2 || credit == 1){ 
        p = g * credit; 
        totalcredit = totalcredit + credit; 
        ptotal = ptotal + p; 
       } 
       else {cout << "Input is incorrect\n"; 
       } 
      } 

      if (grade == 'F') 
      { 
       g = 0; 
       cout << "Enter the number of credits for the class # "<< count <<" (1,2,3,4,5)"<< endl; 
        cin >> credit; 
       cout << ""<< endl; 
       if (credit == 5 || credit == 4 || credit == 3 || credit == 2 || credit == 1){ 
        p = g * credit; 
        totalcredit = totalcredit + credit; 
        ptotal = ptotal + p; 
       } 
       else {cout << "Input is incorrect\n"; 
       } 
      } 

      count = count + 1; 
     } 

     else {cout << "Input is incorrect\n"; 
     } 
    } 
    //algebraic formulas 
    gpa = ptotal/totalcredit; 

    //output results 
    cout << "Your total number of credits is: " << totalcredit << endl; 
    cout << "Your GPA is: " << gpa << endl; 

    if (grade == 'I') 
    { 
    } 
    if (grade == 'W') 
    { 
    } 
    return 0; 
} 
+0

"OR(||)関数に問題があります"ということで、どのような問題があるのでしょうか? –

答えて

3

charintを比較する際には注意してください。例えば、

intの3とcharの値 '3'は等しくありません。 charとintを比較することはできますが、同じ値を持つことはできません。例えば

cout << "Int : " << int(3) << std::endl; 
cout << "Char: " << int('3') << std::endl; 

は出力できます:あなたは

if (credit == 5 || credit == 4 || credit == 3 || credit == 2 || credit == 1){ 

UPDATEに

if (credit == '5' || credit == '4' || credit == '3' || credit == '2' || grade == '1'){ 
                    // ^^^ is this suppose to be "credit"? 

のあらゆるのoccuranceを変更する必要があります

Int : 3 
Char: 51 


「I」または「W」が提供されている場合(実際にはcount変数をインクリメントする以外には、コードは実際に何もしないことに注意してください)。ただし、EVERYコースが「I」または「W」の場合(つまり、すべてのコースが(i)ncompleteまたは(W)iithdrawlの場合)、totalcredit変数は0になり、 '0で除算'エラーが発生します。変更

//algebraic formulas 
gpa = ptotal/totalcredit; 

if (totalcredit > 0) { 
    gpa = ptotal/totalcredit; 
} else { 
    gpa = 0; 
} 

にelse文は、おそらく( gpaが既にゼロになります)に必要なイマイチ、それはあなたがに有効なクレジットが存在しない場合GPAがしたい、まさにそれが明示的になりますGPAを計算する。

+0

OR機能の問題を解決しました。ユーザーの入力が「I」または「W」のときに何をすべきかまだわからない –

+0

上記の質問から、ユーザーが「I」または「W」を入力すると、基本的に無視したいと思うように聞こえます。あなたは既にあなたのコードでやっています。私は(I)ncompletesまたは(W)iithdrawlのGPAがどのように計算されているのかよく分かりません。一部の学校では、コースが修了するまでGPAで不完全を「F」と計算します。ほとんどの学校では、GPAでの撤退は含まれていない可能性があります。 – Jvinniec

+0

WまたはIの成績を持つこの課題のために、GPAの計算に使用する必要はありません。しかし、これらの場合、これらの値の1つを入力すると、プログラムはGPA計算の一部としてそれを使用し、0エラーで除算します。 –