2016-09-25 6 views
-1

Ich schreibe einen GPA-Rechner. Ich habe Probleme mit der OR (||) -Funktion, um zu überprüfen, ob die Eingabe für Guthaben korrekt oder nein ist (wenn sie falsch ist, sollte sie erneut fragen). Wenn die Eingabe für Noten "I" oder "W" ist, sollte das Programm diese Information nicht verwenden (es sollte nichts dagegen tun). Vielen Dank für die Hilfe. Hier ist der Code:GPA-Rechner. Probleme mit ODER und Division durch 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

"Ich habe Probleme mit der OR (||) -Funktion" können Sie angeben, welche Probleme Sie damit haben? –

Antwort

3

Vorsicht beim int zu char vergleichen. Zum Beispiel:

int credit = 3; 

// THIS WILL FAIL 
if (credit == '3') { 
} 

ein int von 3 und ein char von '3' wird NICHT gleich sein. Obwohl Sie Char und Int vergleichen können, haben sie nicht den gleichen Wert. Zum Beispiel

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

gibt die Ausgabe:

Int : 3 
Char: 51 

Sie sollten jedes Vorkommen von

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

zu

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

UPDATE ändern
Beachten Sie, dass Ihr Code niemals etwas tut, wenn "I" oder "W" angegeben wird (abgesehen davon, dass der Kurs count variabel ist, wie er sollte). Jedoch, wenn EVERY natürlich entweder "I" oder "W" ist (d. H. Alle Kurse sind (I) unvollständig oder (W) itsdraw), wird Ihre totalcredit Variable gleich 0 sein, was Ihnen den Fehler "dividiere durch 0" gibt. Ändern

//algebraic formulas 
gpa = ptotal/totalcredit; 

zu

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

Die else-Anweisung wahrscheinlich notwendig ist nicht (gpa bereits Null sein), aber es macht es explizit genau das, was Sie die GPA sein wollen, wenn es keine gültigen Kredite sind berechne den GPA von.

+0

Problem mit der OR-Funktion wurde behoben. Immer noch nicht wissen, was zu tun ist, wenn Benutzereingabe "I" oder "W" –

+0

Aus Ihrer Frage oben klingt es wie wenn der Benutzer gibt "Ich" oder "W" Sie wollen es grundsätzlich ignorieren, was ich glaube was ist Du machst das schon in deinem Code. Ich bin mir nicht sicher, wie GPAs für (I) ncompletes oder (W) ithdraw berechnet werden. Einige Schulen berechnen Unvollständigkeit als "F" in der GPA, bis der Kurs abgeschlossen ist. Die meisten Schulen enthalten wahrscheinlich keine Rücktritte in der GPA. – Jvinniec

+0

Für diese Zuordnung müssen Kurse mit W- oder I-Noten nicht in der GPA-Berechnung verwendet werden. Aber in diesem Fall, wenn man einen dieser Werte eingibt, verwendet das Programm es als Teil der GPA-Berechnung und gibt mir die Division durch 0 Fehler. –