成绩不会显示

The grade won't display

本文关键字:显示      更新时间:2023-10-16

我已经为此工作了一整天,但仍然没有成功。只是一个接一个的错误。我正在努力让分数正确显示,但什么都没用。

(注意等级为grde)

这是我的头。

//Ashton Dreiling
//Essay class header
#ifndef ESSAY_CLASS_H_INCLUDED
#define ESSAY_CLASS_H_INCLUDED
#include <string>
using namespace std;

class EssayClass
{
//private fields to hold data
private:
    double Grammar;
    double Spelling;
    double CL;
    double Content;
    double score;
    string grade=" ";
//public methods for outside code
public:
    EssayClass(double G, double S, double corlen, double content, double Score, string Grade)
    {
        Grammar = G;
        Spelling = S;
        CL = corlen;
        Content = content;
        score =Score;
        grade=Grade;
    }//end EssayClass constructor
    //method to add score
    void addScore()
    {
        score = Grammar + Spelling + CL + Content;
    }// end add score
    //method to show final grade
    void findGrade()
    {
    if (score>=95)
            grade= "A";
        else if (score>=79)
            grade= "B";
        else if (score >=50)
            grade= "C";
        else if (score<=50)
            grade= "F";
    }//end findGrade
    double getGrammar()
    {
        return Grammar;
    }//end getGrammar
    double getSpelling()
    {
        return Spelling;
    }//end getSpelling
    double getCL()
    {
        return CL;
    }//end getCL
    double getContent()
    {
        return Content;
    }//end getContent
    double getScore()
    {
        return score;
    }//end getScore
    string getGrade()
    {
        return grade;
    }//end getGrade
};//end class
#endif // ESSAY_CLASS_H_INCLUDED

这是我的主菜。

 // Ashton Dreiling
//Essay exercise program
#include <iostream>
#include "Essay class.h"
#include <string>
#include <stdlib.h>
const int NUM_FOR_CAL1=0;
using namespace std;
void userInput(double &grammar, double &spelling, double &CL, double &content);
int main ()
{
    //some variables
    double grmmr;
    double splling;
    double correctlength;
    double con;
    string grde;

    userInput(grmmr, splling, correctlength, con);
    //displaying the scores that were inputed
    cout << "The recorded scores are:" << endl;
    cout << "Grammar: " << grmmr << endl;
    cout << "Spelling: " << splling << endl;
    cout << "Correct Length: " << correctlength << endl;
    cout << "Content: " << con << endl;
    EssayClass essayObject(grmmr, splling, correctlength, con, grde);
    //calling member functions with essay object
    userInput(grmmr, splling, correctlength, con);
    essayObject.addScore();
    essayObject.findGrade();
    essayObject.getGrade();
    string grde=essayObject.getGrade();

    //final grade
    cout << "The grade for this essay is: " << grde << endl;

    system("Pause");
    return 0;
}//end main
void userInput(double &grmmr, double &splling, double &correctlenght, double &con)
{
    //input from user to enter points
    cout << "Enter the points for an essay." << endl;
    cout << "Grammar (must be 30 or less points)." << endl;
    cin >> grmmr;
    while (grmmr>30 || grmmr<NUM_FOR_CAL1)
    {
        cout << "Grammar must be 30 or less points." << endl;
        cin >>grmmr;
    }//end while loop
    cout << "Spelling (must be 20 or less points)." << endl;
    cin >> splling;
    while(splling>20 || splling<NUM_FOR_CAL1)
    {
        cout << "Spelling must be 20 or less points." << endl;
        cin>>splling;
    }//end while loop
    cout << "Correct length (must be 20 or less points)." << endl;
    cin>>correctlenght;
    while(correctlenght>20 || correctlenght<NUM_FOR_CAL1)
    {
        cout << "Correct length must be 20 or less points." << endl;
        cin >> correctlenght;
    }//end while loop
    cout << "Correct content (must be 30 or less points)." << endl;
    cin>>con;
    while(con>30 || con<NUM_FOR_CAL1)
    {
        cout << "Content must be 30 or less points." << endl;
        cin >> con;
    }//end while loop
}// end user input

这对我来说很有效。

 class EssayClass{
     //private fields to hold data
     private:
     //Member names are best(In my opinion) given as: m_typeName. IE:double m_dblGrammar;
          double Grammar;
          double Spelling;
          double CL;
          double Content;
          double score;
          string grade;
     //public methods for outside code
     public:
          EssayClass(double G, double S, double corlen, double content, double Score, string &Grade){
         Grammar = G;
         Spelling = S;
         CL = corlen;
         Content = content;
         score = Score;
         //Set this here instead of in the private section as this is not a static variable.
         grade= "";
     }//end EssayClass constructor
//method to add score
//NOTE: You do not use the parameter; thus was omitted.
     void addScore(){
         score = Grammar + Spelling + CL + Content;
     }// end add score
//method to show final grade
     void findGrade(){
          //Another thing I'm fond of, which helps prevent typo's
         //Is to have your constant be a left hand compare. For example
        //if(5 == foo). Help you prevent from accidental if(foo = 5)'s
         if (score >= 95)
              grade = "A";
         else if (score >= 79)
              grade = "B";
         else if (score >= 50)
              grade = "C";
         else //this should probably be just "else"
              grade = "F";
     }//end findGrade
     double getGrammar(){
         return Grammar;
     }//end getGrammar
     double getSpelling(){
         return Spelling;
     }//end getSpelling
     double getCL(){
         return CL;
     }
      double getContent(){
              return Content;
          }//end getContent
      double getScore(){
             return score;
         }//end getScore
      string &getGrade(){
        return grade;
    }//end getGrade
};//end class

和:

void userInput(double &grammar, double &spelling, double &CL, double &content);
int main ()
{
    //some variables, good habit is to set these to 0, although that's probably just 'C' Mentality that I haven't gotten rid of
    double grmmr = 0.0;
    double splling = 0.0;
    double correctlength = 0.0;
    double con = 0.0;
    double scr = 0.0;
    std::string grde = "";
    //Note: 4 parameters in declaration, 4 in useage.
    userInput(grmmr, splling, correctlength, con);
    //displaying the scores that were inputed
    cout << "The recorded scores are:" << endl;
    cout << "Grammar: " << grmmr << endl;
    cout << "Spelling: " << splling << endl;
    cout << "Correct Length: " << correctlength << endl;
    cout << "Content: " << con << endl;
    EssayClass essayObject(grmmr, splling, correctlength, con, scr, grde);
    //calling member functions with essay object
    userInput(grmmr, splling, correctlength, con);
    essayObject.addScore();
    essayObject.findGrade();
    essayObject.getGrade();
    //NOTE: There is no string grde here, as I declared it above.
    grde = essayObject.getGrade();

    //final grade
    cout << "The grade for this essay is: " << grde << endl;
    //generally bad practice to use system calls -- however for homework it has it's purpose.
    system("Pause");
    return 0;
}//end main
void userInput(double &grmmr, double &splling, double &correctlenght, double &con)
{
    //input from user to enter points
    cout << "Enter the points for an essay." << endl;
    cout << "Grammar (must be 30 or less points)." << endl;
    cin >> grmmr;
    while (grmmr > 30 || grmmr < NUM_FOR_CAL1)
    {
        cout << "Grammar must be 30 or less points." << endl;
        cin >> grmmr;
    }//end while loop
    cout << "Spelling (must be 20 or less points)." << endl;
    cin >> splling;
    while(splling > 20 || splling < NUM_FOR_CAL1)
    {
        cout << "Spelling must be 20 or less points." << endl;
        cin>> splling;
    }//end while loop
    cout << "Correct length (must be 20 or less points)." << endl;
    cin>> correctlenght;
    while(correctlenght > 20 || correctlenght < NUM_FOR_CAL1)
    {
        cout << "Correct length must be 20 or less points." << endl;
        cin >> correctlenght;
    }//end while loop
    cout << "Correct content (must be 30 or less points)." << endl;
    cin>> con;
    while( con > 30 || con < NUM_FOR_CAL1)
    {
        cout << "Content must be 30 or less points." << endl;
        cin >> con;
    }//end while loop
}// end user input**

几件事:

string grade=" ";

在C++中无法做到这一点。不能像这样初始化成员。修复它!

EssayClass(double G, double S, double corlen, double content, double Score, string Grade)

所以EssayClass构造函数需要5个双精度和一个字符串。好的。

EssayClass essayObject(grmmr, splling, correctlength, con, scr, grde);

您用4个双精度、一个"scr"(没有这样的变量)和一个双精度调用EssayClass构造函数。这不是它所接受的。修复它!

顺便说一句,不要以大写字母开头变量!这被认为是一种糟糕的风格。大写字母表示宏、类名和其他类型名,有时也表示函数(包括成员函数)。

另一方面,所有getSomething函数都应该是常量,getGrade()应该返回const string&,而不是string。类似地,构造函数应该接收const string&,而不是string。从形式上讲,这不是一个错误,但这种参数传递会导致代码效率低下。

您应该始终在"="前后留出一个空格,否则您的代码将很难阅读。

您的代码有更多的问题,但首先,修复这些问题。

侧面评论:

  1. 避免在头文件中使用"namespacestd;",因为包括它在内的所有文件都将继承它
  2. 如果等级总是一个字符,则可以使用char
  3. 如果计算了等级,则不需要在构造函数中传递,而是可以在构造函数中计算