我已經在那裏你中所有的標誌進入和它給你的平均值,也使得它重演,但問題是一個應用程序,C++雖然環和陣列
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''不是字符串文字。 ''再''是一個單一的'字符',所以試圖比較它與兩個字符不是一個好主意。 – chris
你不應該通過重複只有一次(添加所有標記後)?也注意整數除法。 – Borgleader
你正在做的平均錯誤。難怪你錯了! – user3329166