等级不会显示

时间:2016-08-02 05:06:23

标签: c++

我整天都在努力,但仍然没有成功。错误后只是错误。我试图让等级正确显示,但没有工作。

(注意等级为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

3 个答案:

答案 0 :(得分:1)

这对我有用..

 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**

答案 1 :(得分:0)

有几件事:

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个双打,一个&#34; scr&#34;来调用EssayClass构造函数。 (没有这样的变量)和一个双。这不是它所接受的。修复它!

在旁注中,不要用大写字母开始变量!这被认为是一种糟糕的风格。大写字母用于宏,类名和其他类型名称,有时用于函数(包括成员函数)。

另一方面,所有getSomething函数都应该是常量,getGrade()应该返回const string&,而不是string。同样,您的构造函数应该收到const string&,而不是string。形式上它不是错误,但这样的参数传递会使代码效率低下。

您应该始终在&#34; =&#34;之前和之后创建空格,否则您的代码将难以阅读。

您的代码存在更多问题,但首先要解决这些问题。

答案 2 :(得分:0)

侧面评论:

  1. 避免&#34;使用命名空间std;&#34;在头文件中,因为包含它的所有文件都将继承此文件。
  2. 如果成绩总是一个字符,则可以使用字符
  3. 如果计算成绩,你不需要在构造函数中传递它,你可以在构造函数中计算它。