C ++程序给出了错误的结果! [英] C++ program gives wrong result !!

查看:85
本文介绍了C ++程序给出了错误的结果!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究C ++如何编程第8版,第5章中有一个关于switch-case的例子。

Hi, i have been working on C++ How to Program 8th edition and there is an example in chapter 5 about switch-case.

#include <iostream>
#include <string>
#include <cstdio>

using namespace std;

class GradeBook
{
public:
    GradeBook(string);
    void setCourseName(string);
    string getCourseName();
    void displayMessage();
    void inputGrades();
    void displayGradeReport();
private:
    string courseName;
    int aCount;
    int bCount;
    int cCount;
    int dCount;
    int fCount;
};

GradeBook::GradeBook(string name)
{
    setCourseName(name);
    int aCount=0;
    int bCount=0;
    int cCount=0;
    int dCount=0;
    int fCount=0;
}

void GradeBook::setCourseName(string name)
{
    if(name.length() <=25)
        courseName = name;
    else
    {
        courseName = name.substr(0,25);
        cout<<"Name \""<<name<<"\" exceeds maximum length (25)."<<endl<<"Limiting coursName to first 25 character."<<endl;
    }
}

string GradeBook::getCourseName()
{
    return courseName;
}

void GradeBook::displayMessage()
{
    cout<<"Welcome to the grade book for\n"<<getCourseName()<<endl;
}
void GradeBook::inputGrades()
{
    int grade;


    cout<<"Enter the letter grades."<<endl<<"Enter the EOF character to end input."<<endl;
    while((grade = cin.get()) !=EOF)
    {
        switch ( grade )
        {
            case 'A':
            case 'a':
            ++aCount;
            break;

            case 'B':
            case 'b':
            ++bCount;
            break;

            case 'C':
            case 'c':
            ++cCount;
            break;

            case 'D':
            case 'd':
            ++dCount;
            break;

            case 'F':
            case 'f':
            ++fCount;
            break;

            case '\n':
            case '\t':
            case ' ':
            break;

            default:
            cout<<"Incorrect letter grade entered. Enter a new grade."<<endl;
            break;
        }

    }

}

void GradeBook::displayGradeReport()
{
    cout<<"\n\nNumber of students who received each letter grade:"
    << "\nA: " << aCount
    << "\nB: " << bCount
    << "\nC: " << cCount
    << "\nD: " << dCount
    << "\nF: " << fCount
    <<endl;
}

int main()
{
    GradeBook myGradeBook("CS 101 C++ Programming");

    myGradeBook.displayMessage();
    myGradeBook.inputGrades();
    myGradeBook.displayGradeReport();
}





我正在使用codeblocks我认为它可能会导致问题,但在visual studio中也是如此这是代码的结果;



http://imageshack.us/photo/my-images/405/as9q.jpg/ [ ^ ]



我怎样才能得到正确的结果。它可能是关于初始化但我无法找到解决方案。



I am using codeblocks i thought it may cause a problem but its same in the visual studio 2012.

This is the result of the code;

http://imageshack.us/photo/my-images/405/as9q.jpg/[^]

How can i get the right results. Its probably about initialization but i couldnt find the solution.

推荐答案

问题出在 GradeBook 构造函数中。 br />
The problem is in the GradeBook constructor.
GradeBook::GradeBook(string name)
{
    setCourseName(name);
    int aCount=0;
    int bCount=0;
    int cCount=0;
    int dCount=0;
    int fCount=0;
}





这里发生的是你没有初始化 GradeBook的成员变量类,而是创建局部变量。

这里读一读这个来解释范围 [ ^ ]。



因为成员变量没有初始化你得到输出中的'怪异'数字。当程序开始使用它们时,它们具有未确定的起始值。



要正确初始化成员变量,您需要删除' int '来自构造函数中的语句。



What is happening here is that you are not initializing the member variables of the GradeBook class but instead are creating local variables.
Here a read through this to explain the concept of scope[^].

Because the member variables are not initialized you get the 'weird' numbers in your output. When the program start to use them they have an undetermined start value.

To correctly initialize the member variables you need to remove the 'int' from the statements in the constructor.

GradeBook::GradeBook(string name)
{
    setCourseName(name);
    aCount=0;
    bCount=0;
    cCount=0;
    dCount=0;
    fCount=0;
}



您还可以使用会员初始化列表


You can also use member initialization list

GradeBook::GradeBook(string name)
    : aCount( 0 ),
      bCount( 0 ),
      cCount( 0 ),
      dCount( 0 ),
      fCount( 0 )
{
    setCourseName(name);
}



以下是有关的一些信息初始化列表 [ ^ ]。


类构造只做初始化局部变量。

所以你错误地想要声明变量。
class construction only do initialize the local variable.
so u r wrongly wants to declar variables.


这篇关于C ++程序给出了错误的结果!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆