【问题标题】:C++ While loop and ArrayC++ While 循环和数组
【发布时间】:2014-03-05 21:54:03
【问题描述】:

我做了一个申请,你输入所有的分数,它会给你平均分,也会让它重复,但问题是

1) 每当执行“寻找平均值”行时,它都会给我 错误的值,我也使用数组来这样做。

2)每当我尝试 迭代应用程序,调用析构函数并弄乱我的 应用

这是我的代码

#include <iostream>
#include <string>
using namespace std;

class Grade{
private:
    int* ptr;
    int number;
public:
    Grade(const int hNumber){
        number = hNumber;
        ptr = new int[this->number];
    }
    Grade(const Grade& cpy){
        ptr = new int[cpy.number];
    }
    void get_marks(){
        for(int i = 0; i < number; ++i){
            cout << "Enter Mark " << i << ": ";
            cin >> ptr[i];
        }
    }
    const int& operator [](const int access) const{
        return ptr[access];
    }
    ~Grade(){
        cout << "Deleting memory" << endl;
        delete [] ptr;
    }
};

int main(){
//local variables
    int sum = 0;
    string name,subject;
    int repeat;
    char again = 'y';
    //user interface
    cout << "Welcome to Grade Marker" << endl;
    cout << "Enter your name: ";
    getline(cin,name);
    while(again == 'y'){
    cout << "Enter the subject name: ";
    getline(cin,subject);
    cout << "How many marks are being entered: ";
    cin >> repeat;
    //creating instance of grade
    Grade grd(repeat);
    grd.get_marks();;
    //display info
    cout << "The average mark is: ";
    for (int i = 0; i < repeat; i++){
        sum = ((sum + grd[i]) / repeat);
    }
    cout << sum << endl;
    //looping the application
    cout << "Would you like to enter another subject[y/n]: ";
    cin >> again;
    }
    //good bye message
    if (again == 'n' || again == 'no'){
        cout << "Goodbye" << endl;
    }
    system("pause");
    return 0;
}

为了简单起见,我认为给我错误的代码部分是

cout << "Would you like to enter another subject[y/n]: ";
        cin >> again;
        }
        //good bye message
        if (again == 'n' || again == 'no'){
            cout << "Goodbye" << endl;
            }

//display info
        cout << "The average mark is: ";
        for (int i = 0; i < repeat; i++){
            sum = ((sum + grd[i]) / repeat);
        }
        cout << sum << endl;

感谢您的宝贵时间

【问题讨论】:

  • 'no' 不是字符串文字。而again 是一个单一的char,因此尝试将其与两个字符进行比较并不是一个好主意。
  • 你不应该只通过重复一次(添加所有标记之后)来划分吗?还要注意整数除法。
  • 你做错了。难怪你得到错误的价值!
  • 您对'no' 的条件检查将不起作用,并且您还有整数除法问题。
  • 是的 @Borgleader 帮助我解决了 for 循环问题,但我似乎无法弄清楚 while 循环问题。它不断调用析构函数

标签: c++ arrays class for-loop iterator


【解决方案1】:

您正在进行整数除法,此外,您不会在每次迭代时将 sum 重新初始化为 0。您可以在循环内移动 sum 声明并编写:

float sum = 0;
for (int i = 0; i < repeat; i++){
    sum += grd[i];
}
cout << "The average mark is: ";
cout << sum / repeat << endl;

【讨论】:

  • 是的,我确实发现了数组问题,但 while 循环似乎无法正常工作
【解决方案2】:

std::cin.ignore() 会在这里为您提供帮助。

正在添加...

std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

...到您的while-loop 的最后,将清除输入缓冲区直到换行...您会发现这将结束看似无限的循环。

请参阅How do I flush the cin buffer? 了解更多信息。

【讨论】:

    【解决方案3】:

    由于作用域,您的 while 循环正在调用析构函数。您在运行 while 循环的每次迭代中都声明了 Grade grd。这意味着您的上一次迭代中的grd 被一次又一次地重新声明,因此调用了析构函数。但这是正常的。您想保存成绩实例吗?在这种情况下,您需要创建一个 Grade 对象数组

    【讨论】:

      猜你喜欢
      • 2020-10-28
      • 2021-07-01
      • 1970-01-01
      • 2013-07-12
      • 1970-01-01
      • 2012-09-14
      • 2016-01-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多