//ex6.cpp The exercise 6 in 6.10 of C++ Primer Plus //2005-10-24, WizCas #include <iostream> #include <cctype> using namespace std; const int ArSize = 20;
struct person { char name[ArSize]; double money; };
int main() { cout << "Enter the number of the patron: "; int n; cin >> n; cin.get(); person *payer = new person [n]; int i; cout << "Enter the name and the amount for each patron: \n"; for (i = 0; i < n; i++) { cout << "Patron #" << i + 1 << endl; cout << "Name: "; cin.get(payer[i].name, ArSize).get(); if (payer[i].name == " " || payer[i].name == "\n") payer[i].name = "none"; cout << "Amount: "; cin >> payer[i].money; cin.get(); cout << "\n"; } cout << "Here is the namelist of the patrons: \n" "==Grand Patrons==\n"; for (i = 0; i < n; i++) if (payer[i].money > 10000) cout << payer[i].name << " " << payer[i].money << endl; cout << "==Patrons==\n"; for (i = 0; i < n; i++) if (payer[i].money <= 10000) cout << payer[i].name << " " << payer[i].money << endl; system ("pause"); return 0; }
到 payer[i].name = "none"; 这一行就不行了,用Dev C++编译,显示错误 incompatible types in assignment of 'const char[5]' to 'char[20]'
请问这是怎么回事?还有这句 if (payer[i].name == " " || payer[i].name == "\n") 我的本意是当payer[i].name的值为空,这样写是不是麻烦了?有没有什么更好的解决办法?我试了 if (isspace(payer[i].name)) 可是报告错误invaild convertion from 'char*' to 'int'和 initializing argument 1 of 'int isspace(int)' 请各位帮我解决一下!谢谢!!!
[此贴子已经被作者于2005-10-24 19:07:43编辑过]