โค้ดเกี่ยวกับ การเช่าหนังสือ
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
struct Member {
int Type1;
int Type2;
int Type3;
int QTY;
float Amount;
};
float calculateAmount(int type1, int type2, int type3) {
const float priceType1 = 5.0;
const float priceType2 = 6.0;
const float priceType3 = 7.0;
return (type1 * priceType1 + type2 * priceType2 + type3 * priceType3);
}
int main() {
int n;
int a;
char name[50];
cout << "Enter number of customers: ";
cin >> n;
Member members[n] = {
{0, 0, 0, 0, 0}
};
for (int i = 0; i < n; i++) {
cout << "Member ID: ";
cin >> a;
cin.ignore();
cout << "Member name: ";
cin.getline(name, 50);
cout << "Number of book types 1 (cartoon): ";
cin >> members.Type1;
cout << "Number of book types 2 (novel): ";
cin >> members.Type2;
cout << "Number of book types 3 (magazine): ";
cin >> members.Type3;
members.Amount = calculateAmount(members.Type1, members.Type2, members.Type3);
members.QTY = members.Type1 + members.Type2 + members.Type3;
}
cout << "\nResults of income calculation:" << endl;
cout << left << setw(12) << "ID.Number" << setw(15) << "Name"
<< setw(10) << "Type 1" << setw(10) << "Type 2"
<< setw(10) << "Type 3" << setw(10) << "QTY"
<< setw(10) << "Amount" << endl;
float totalIncome = 0;
for (int i = 0; i < n; i++) {
totalIncome += members.Amount;
cout << left << setw(12) << (i+1) << setw(15) << name
<< setw(10) << members.Type1 << setw(10) << members.Type2
<< setw(10) << members.Type3 << setw(10) << members.QTY
<< setw(10) << members.Amount << endl;
}
cout << right << setw(47) << "Total " << setw(10) << totalIncome << endl;
return 0;
}
ช่วยแก้โค้ด ภาษา c++ หน่อยครับ ชื่อมันไม่เรียงตามลำดับที่ระบุไว้ ปล.ไม่ได้เรียนเกี่ยวกัยด้านนี้ แต่อยากเขียนเป็นครับ
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
struct Member {
int Type1;
int Type2;
int Type3;
int QTY;
float Amount;
};
float calculateAmount(int type1, int type2, int type3) {
const float priceType1 = 5.0;
const float priceType2 = 6.0;
const float priceType3 = 7.0;
return (type1 * priceType1 + type2 * priceType2 + type3 * priceType3);
}
int main() {
int n;
int a;
char name[50];
cout << "Enter number of customers: ";
cin >> n;
Member members[n] = {
{0, 0, 0, 0, 0}
};
for (int i = 0; i < n; i++) {
cout << "Member ID: ";
cin >> a;
cin.ignore();
cout << "Member name: ";
cin.getline(name, 50);
cout << "Number of book types 1 (cartoon): ";
cin >> members.Type1;
cout << "Number of book types 2 (novel): ";
cin >> members.Type2;
cout << "Number of book types 3 (magazine): ";
cin >> members.Type3;
members.Amount = calculateAmount(members.Type1, members.Type2, members.Type3);
members.QTY = members.Type1 + members.Type2 + members.Type3;
}
cout << "\nResults of income calculation:" << endl;
cout << left << setw(12) << "ID.Number" << setw(15) << "Name"
<< setw(10) << "Type 1" << setw(10) << "Type 2"
<< setw(10) << "Type 3" << setw(10) << "QTY"
<< setw(10) << "Amount" << endl;
float totalIncome = 0;
for (int i = 0; i < n; i++) {
totalIncome += members.Amount;
cout << left << setw(12) << (i+1) << setw(15) << name
<< setw(10) << members.Type1 << setw(10) << members.Type2
<< setw(10) << members.Type3 << setw(10) << members.QTY
<< setw(10) << members.Amount << endl;
}
cout << right << setw(47) << "Total " << setw(10) << totalIncome << endl;
return 0;
}