0

我真的不会问我是否真的知道答案。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<<"\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-->
4

3 回答 3

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,它将设置故障位标志。

  • cin.fail() 检查是否设置了failbit 或badbit。

  • cin.bad() 检查是否设置了 badbit

  • cin.eof() 检查是否设置了 eofbit

如果 cin 失败,您应该使用 cin.ignore() 忽略输入并要求用户重试。

注意: main() 应该返回一个非 void 的 int。

该程序运行“输入:”,如果它不是 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;
}
于 2013-03-20T15:53:52.100 回答
0

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

于 2013-03-20T15:54:25.970 回答
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>
于 2013-03-21T00:12:38.047 回答