我真的不会问我是否真的知道答案。新的c ++和我所知道的就是在这里,它也处于试验和错误状态但是试图超越截止日期,这是一个功课。我现在遇到的问题是当用户输入一个字符或字符串时,我似乎无法找到过滤掉它的解决方案并提示“由于输入无效而再次键入”。 .. 有任何想法吗?字符似乎值为0.试图键入有关的条件,但仍然无法正常工作。不知道该怎么做。
#include <iostream>
#include <conio.h>
using namespace std;
/*--------------------------------------------------
---------------------------------------------------------
Accounts are under pinnumbers: |
1001 increment of 1 |
Passcodes are under the codes: |
1101 increment of 1 |
|
limit of 10 as instructed. |
|
i.e : (valid pin number ) 1001 = (valid pin code) 1101 |
---------------------------------------------------------
*/
int option, pinnumber, pincode, vip[10], pc[10], money[10], accountno[10], getindex;
string name[10];
void mainmenu();
void assignaccounts();
void checkaccounts();
void start();
void options();
bool logged=false;
char a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z;
//<--START OF THE MAIN FUNCTION -->
main() {
assignaccounts();
start();
system("PAUSE"); //more of a getch() pero naa xa'y press any key. w/c is better!
}
//<--EOF MAIN FUNCTION-->
//<-- START OF EXTRA FUNCTION SECTION -->
//---Start of checkaccounts function --> -----------------------------
void checkaccounts() { //|
for(int x=0; x<10; x++) { //|
if(( vip[x] == pinnumber) && ( pc[x] == pincode )) { //|
logged=true; //|
getindex=x; //|
mainmenu(); //|
break; //|
} else if ((pinnumber == 'a') && (pincode == 'A' )) {
cout<<"Invalid Input, Try again."<<endl;
cin.clear();
main();
break;
} else {
;
} //|
} //|
if(!logged){ //|
cout<<"\n\t Invalid Input, Try again."<<"\n\n\t "; //|
system("PAUSE"); //|
start(); //|
} //|
//|
} //|
//end of checkaccounts function --------------------------------------
//Start of Assignaccounts function------------------------------------
void assignaccounts() { //|
for(int x=0; x<10; x++) { //|
vip[x]=1001+x; //|
pc[x]=1101+x; //|
} //|
name[0] = "John Do Doe"; //|
money[0] = 10000; //|
accountno[0] = 1; //|
//|
name[1] = "Zsaza Pa Ligid"; //|
money[1] = 20000; //|
accountno[1] = 2; //|
//|
name[2] = "Robin Pa Didit"; //|
money[2] = 30000; //|
accountno[2] = 3; //|
//|
name[3] = "lrem ipm"; //|
money[3] = 40000; //|
accountno[3] = 4; //|
//|
name[4] = "Daniel Pa Slide"; //|
money[4] = 50000; //|
accountno[4] = 5; //|
//|
name[5] = "James Bond"; //|
money[5] = 60000; //|
accountno[5] = 6; //|
//|
name[6] = "Lara Croft Llara"; //|
money[6] = 70000; //|
accountno[6] = 7; //|
//|
name[7] = "John Ziggy Pa ( GOLD MEMBER )"; //|
money[7] = 90000; //|
accountno[7] = 11; //|
//|
name[8] = "Loren Joy Chris ( GOLD MEMBER )"; //|
money[8] = 110000; //|
accountno[8] = 10; //|
//|
name[9] = "Jayson Derulo"; //|
money[9] = 120000; //|
accountno[9] = 10; //|
//|
} //|
//End of Assignaccounts function--------------------------------------
//---Start of start function --> -------------------------------------
void start() { //|
system("cls"); //|
cin.clear();
cout<<"------------------------------------------"<<endl; //|
cout<<" |** WELCOME TO CAMARILLO & ALJON ATM **|"<<endl; //|
cout<<"------------------------------------------"<<endl; //|
cout<<"\nEnter valid identification pin number (VIP): "; //|
cin>>pinnumber; //|
cout<<"\nEnter valid pincode (PC): "; //|
cin>>pincode; //|
checkaccounts(); //|
//|
} //|
//end of Start function ----------------------------------------------
//start of mainmenu function ----------------------------------------------------------------------------------
void mainmenu() { // |
int choice; // |
if(logged) { // |
while(true){ // |
system("cls"); //clearscreen // |
cout<<"Log-in successful!"<<endl; // |
cout<<"\nWelcome: "<<name[getindex]; // |
cout<<"\nAccount No.: "<<accountno[getindex]<<endl;
cout<<"\nChoose a Transaction:\n"; // |
cout<<"\n"; // |
cout<<"[1] Inquire Balance \n" // |
<<"[2] Withdraw \n" // |
<<"[3] Deposit \n" // |
<<"[4] Quit \n" // |
<<"\n"<<endl; // |
cout<<"Enter Option: "; // |
cin>>option; // |
// |
if (option == 1) { // |
cout<<"\n\tOutstanding Balance is P"<<money[getindex]<<"\n\n\t "; // |
system("PAUSE"); // |
} else if (option == 2) { // |
int co; // |
cout<<"\n\tEnter amount: "; // |
cin>>co; // |
if(co<money[getindex]){ // |
money[getindex]-=co; // |
cout<<"\n\tYour Outstanding Balance has been updated to P"<<money[getindex]<<"\n\n\t "; // |
} else { // |
cout<<"\n\t Invalid amount, Please check again."; // |
cout<<"\n\t You only have P"<<money[getindex]<<" in your account."<<"\n\n\t "; // |
} // |
system("PAUSE"); // |
} else if (option == 3) { // |
int dep; //Declaration of the cin in this case; // |
cout<<"\n\tEnter amount: "; // |
cin>>dep; // |
money[getindex]+=dep; // |
cout<<"\n\tYour Outstanding Balance has been updated to P"<<money[getindex]<<"\n\n\t "; // |
system("PAUSE"); // |
} else if (option == 4) { // |
break; // |
} else{ // |
cout << "\n\n\tInvalid Input, Try again.\n\n\t "; // |
system("PAUSE"); // |
} // |
} // |
cout<<"\n\n\t THANK YOU FOR USING OUR ATM, YOU'LL NOW EXIT THE PROGRAM."<<endl; // |
cout<<"\n\t This is intended for our project in c++ under Professor Machica."<<"\n\n\t "; // |
} // | // |
} // |
//end of mainmenu function-------------------------------------------------------------------------------------
//<-- EOF EXTRA SECTION FUNCTIONS-->
答案 0 :(得分:0)
您可以使用if(cin.fail())或if(!cin)
来检查cin是否没有获得正确的类型int n;
cin >> n;
if(!cin)
{
// user didn't input a number
}else{
// user did input number
}
如果输入不是int,则会设置failbit标志。
cin.fail()检查是否设置了failbit或badbit。
cin.bad()检查badbit是否已设置
如果cin失败,你应该使用cin.ignore()忽略输入并要求用户重试。
注意:main()应该返回一个int而不是void。
这个程序运行'input:',如果它不是int,它将循环。
#include <iostream>
using namespace std;
int main(int argc, const char * argv[])
{
int ival;
input:
if(cin >> ival)
{
cout << ival << endl;
// do something (input is a number)
}
if(!cin >> ival)
{
cin.clear();
cin.ignore();
cout << "error, retry" << endl;
goto input;
}
return 0;
}
答案 1 :(得分:0)
首先,我认为你的代码应该使用结构化数组来存储这些信息。作为C语言的初学者,你也应该尽量不要使用变量Public,相反,你应该将它们用作参数。它看起来像你也不知道char字符串,你应该使用strcmp
来比较你的字符串以便与你的用户进行交互。我只是在这里分享一些想法,也许你应该首先学习一些关于C字符串,结构,结构的知识首先是数组和指针。
答案 2 :(得分:0)
使用此代码强制用户输入int
。
int value;
std::cout << "Enter a number: ";
for (std::string input; std::getline(std::cin, input);) { // Read all characters.
std::istringstream iss(input);
if (iss >> value && iss.eof()) { // Try to parse to int.
break;
}
std::cout << "Invalid number, try again: ";
}
std::cout << "Number was: " << value << std::endl;
请记住包括:
#include <iostream>
#include <sstream>
#include <string>