预期语句和预期声明错误 c++

Expected statement and expected declaration error c++

本文关键字:错误 c++ 语句 声明      更新时间:2023-10-16
// Check the account number validity function
void chk_ac_num(string &ac_num, string a_num[], int cnt)
{
    // Declare variables
    int check=0;
    // Checking for 8 digit
    do
    {
        cout<<"Enter Desired Account Number (8 Digit):"<<endl;
        getline(cin, ac_num);
        check=ac_num.length();
        if(check==8)
        {
            for(int i=0;i<cnt;i++)
            {
                if(ac_num==a_num[i]) // Checking same account number existed or not
                {
                    check=1;
                    cout<<"Account Number already exist, please enter another desired number !"<<endl;  // Same account number detected
                    break;
                }
            }
        }
        else
            if(check==0)
                system("cls");
            else
                cout<<"Please enter 8 digit number !"<<endl;
    }while(check!=8); // Loop it when the account number is existed or not valid
}

我在第二个其他情况下预期了语句错误,在我的while条件下也有预期的声明错误......我该如何修复它们?

所以我在你的函数周围放了一些代码。

#include <iostream>
#include <stdlib.h>
using namespace std;
void chk_ac_num(string &ac_num, string a_num[], int cnt)
{
    // Declare variables
    int check=0;
    // Checking for 8 digit
    do
    {
        cout<<"Enter Desired Account Number (8 Digit):"<<endl;
        getline(cin, ac_num);
        check=ac_num.length();
        if(check==8)
        {
            for(int i=0;i<cnt;i++)
            {
                if(ac_num==a_num[i]) // Checking same account number existed or not
                {
                    check=1;
                    cout<<"Account Number already exist, please enter another desired number !"<<endl;  // Same account number detected
                    break;
                }                }
            }
        }
        else
            if(check==0)
                system("cls");
            else
                cout<<"Please enter 8 digit number !"<<endl;
    }while(check!=8); // Loop it when the account number is existed or not valid
}
int main()
{
string acnum = "12345678";
string acnumarray[4] = ("12345670", "23456679", "98764432", "56565656");
chk_ac_num(acnum, acnumarray, 4);
return 0;
}

它在 g++ 下对我来说编译得很好。我建议在您的 else 块中使用大括号以防止意外行为,如果您决定将这些控制块扩展到超过一行长。

编辑以修复。括号 != 大括号。