我相信我的代码是正确的,但在输入持卡人姓名后,我试图输入的所有内容都会不断重复。
Print Cards by selecting the type (0 when done):
1. Basic Card
2. ID Card
3. Bank Card
Card Type: 2
Institution name: a
Cardholder name: lola
Expiration date mmddyyyy (0 if none):
Institution name: Cardholder name: Expiration date mmddyyyy (0 if none):
Institution name: Cardholder name: Expiration date mmddyyyy (0 if none):
Institution name: Cardholder name: Expiration date mmddyyyy (0 if none):
Institution name: Cardholder name: Expiration date mmddyyyy (0 if none):
Institution name: Cardholder name: Expiration date mmddyyyy (0 if none):
为了避免这种情况,我该怎么办?它是我在for循环中放入的参数吗?
#include <iostream>
#include <vector>
int main ()
{
std::vector <Card*> cardbook;
int card_type;
std::string institute_name;
std::string card_name;
int expire_date;
int identify_num;
int birthdate;
int account_num;
int secure_code;
std::cout << "Print Cards by selecting the type (0 when done): " << std::endl
<< " 1. Basic Card " << std::endl
<< " 2. ID Card " << std::endl
<< " 3. Bank Card " << std::endl;
std::cout << "Card Type: " ;
std::cin >> card_type;
while (card_type)
{
if (card_type == 1)
{
std::cout << "Institution name: ";
getline (std::cin, institute_name);
std::cin.ignore();
std::cout << "Cardholder name: ";
getline (std::cin, card_name);
std::cin.ignore();
std::cout << "Expiration date mmddyyyy (0 if none): ";
std::cin >> expire_date;
std::cout << std::endl <<std::endl;
cardbook.push_back (new Card(institute_name, card_name, expire_date));
}
else if (card_type == 2)
{
std::cout << "Institution name: ";
getline (std::cin, institute_name);
std::cin.ignore();
std::cout << "Cardholder name: ";
getline (std::cin, card_name);
std::cin.ignore();
std::cout << "Expiration date mmddyyyy (0 if none): ";
std::cin >> expire_date;
std::cout << "ID number: ";
std::cin >> identify_num;
std::cout << "DOB mmddyyyy (0 if not listed)";
std::cin >> birthdate;
std::cout << std::endl << std::endl;
cardbook.push_back (new IDCard(institute_name, card_name, expire_date, identify_num, birthdate));
}
else if (card_type == 3)
{
std::cout << "Institution name: ";
getline (std::cin, institute_name);
std::cin.ignore();
std::cout << "Cardholder name: ";
getline (std::cin, card_name);
std::cin.ignore();
std::cout << "Expiration date mmddyyyy (0 if none): ";
std::cin >> expire_date;
std::cout << "Account number: ";
std::cin >> account_num;
std::cout << "Card Security Code: ";
std::cin >> secure_code;
std::cout << std::endl << std::endl;
cardbook.push_back (new BankCard (institute_name, card_name, expire_date, account_num, secure_code));
}
else
{
break;
}
}
for(auto& v : cardbook)
{
v->print();
}
for (int i = 0; i < cardbook.size(); i++)
{
delete cardbook[i];
}
return 0;
}
注意:字符串库包含在头文件中。 另外一个问题,如果我想按照到期日期的顺序输入卡片,那么排序功能是否正确使用?或者我会使用for循环?提前谢谢!
答案 0 :(得分:2)
问题是您对变量 card_type 的输入只进行了一次而,而(card_type)只是永远进行,因为它始终为非零。我想你需要的是将输入部分带入循环。
实际上,如果您是编程的初学者,我的建议是使用IDE或某种调试器。这也是一个很好的习惯,可以缩进并避免重复代码。
答案 1 :(得分:0)
由于card_type
的输入为2,之后从未修改此值,因此while循环将永远继续。
那是因为任何非零整数都会计算为true
。作为示例,编译并执行以下代码:
int main(){
int t = 134;
while(t){
cout << "t is true !" << endl;
}
}
将继续打印t is true !
,直到您手动停止执行。这当然适用于if
,while
......这意味着整数可以被转换为布尔值。反过来也可能发生同样的事情。 false
可以投放到int
,价值为0,true
的值为1。
长话短说:基本上任何需要布尔值作为参数的东西都可以将整数解释为布尔值,0被评估为false
,所有其他整数被评估为true
。