Closed. This question is off-topic。它当前不接受答案。
想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
3年前关闭。
我整天都在努力,但仍然没有成功。只是一个接一个的错误。我正在尝试获得成绩以正确显示,但是没有任何效果。
(注意等级)
这是我的标题。
这是我的主力。
和:
想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
3年前关闭。
我整天都在努力,但仍然没有成功。只是一个接一个的错误。我正在尝试获得成绩以正确显示,但是没有任何效果。
(注意等级)
这是我的标题。
//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**
关于c++ - 成绩不会显示,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38712130/
10-12 16:49