我需要帮助在我的c++ ATM机代码,关于过滤掉字符或字符串

I need help in my C++ ATM machine code, about filtering out characters or strings

本文关键字:过滤 于过滤 字符串 字符 帮助 我的 c++ 代码 ATM      更新时间:2023-10-16

如果我真的知道答案,我真的不会问。c++的新手,我所知道的都在这里,它也是在试错的情况下,但试图赶上最后期限,这是一个家庭作业。我现在遇到的问题是,当用户输入一个字符或字符串时,我似乎找不到过滤它的解决方案,并提示"由于输入无效而再次输入"。. .什么好主意吗?char的值似乎是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<<"nt Invalid Input, Try again."<<"nnt ";   //|
               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<<"ntOutstanding Balance is P"<<money[getindex]<<"nnt ";                               // |
            system("PAUSE");                                                                                // |
         } else if (option == 2) {                                                                          // |
           int co;                                                                                          // |
           cout<<"ntEnter amount: ";                                                                      // |
                 cin>>co;                                                                                   // |
                 if(co<money[getindex]){                                                                    // |
                    money[getindex]-=co;                                                                    // |
                    cout<<"ntYour Outstanding Balance has been updated to P"<<money[getindex]<<"nnt "; // |
                 } else {                                                                                   // |
                    cout<<"nt Invalid amount, Please check again.";                                       // |
                    cout<<"nt You only have P"<<money[getindex]<<" in your account."<<"nnt ";          // |
                 }                                                                                          // |
                 system("PAUSE");                                                                           // |
         } else if (option == 3) {                                                                          // |
           int dep; //Declaration of the cin in this case;                                                  // |
                 cout<<"ntEnter amount: ";                                                                // |
                 cin>>dep;                                                                                  // |
                 money[getindex]+=dep;                                                                      // |
                 cout<<"ntYour Outstanding Balance has been updated to P"<<money[getindex]<<"nnt ";    // |
                 system("PAUSE");                                                                           // |
         } else if (option == 4) {                                                                          // |
           break;                                                                                           // |
         } else{                                                                                            // |
               cout << "nntInvalid Input, Try again.nnt ";                                            // |
               system("PAUSE");                                                                             // |
         }                                                                                                  // |
      }                                                                                                     // |
         cout<<"nnt THANK YOU FOR USING OUR ATM, YOU'LL NOW EXIT THE PROGRAM."<<endl;                    // |
         cout<<"nt This is intended for our project in c++ under Professor Machica."<<"nnt ";          // |                                                 
     }                                                                                                      // |                                                                                                          // |
}                                                                                                           // |
//end of mainmenu function-------------------------------------------------------------------------------------
//<-- EOF EXTRA SECTION FUNCTIONS-->

首先,我认为你的代码应该使用结构化数组来存储这些信息。作为C语言的初学者,你也应该尽量不要把变量设为Public,相反,你应该把它们用作参数。看起来你也不知道char字符串,你应该用strcmp来比较你的字符串来和你的用户交互。我只是在这里分享一些想法,也许你应该先学习一些关于C字符串,结构体,结构数组和指针的知识。

使用此代码强制用户输入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>

可以使用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.eof()检查eofbit是否设置

如果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;
}