有谁知道如何修复“[错误]预期的主表达式在'.'之前”令牌”问题?

问题描述 投票:0回答:1

这是我编写的代码。

#include <iostream>
#include<bits/stdc++.h>

using namespace std;

struct Account {
  string username;
  string password;
};

struct Schedule{
    char place[100];
    char time[100];
};

bool validatePassword(const string& password) {
  const regex pattern("^(?=.*[A-Za-z])(?=.*\\d)(?=.*[@$!%*?&])[A-Za-z\\d@$!%*?&]{8,}$");
  return regex_match(password, pattern);
}

void createAccount() {
  Account newAccount;
  string password;

  // Get user input for username
  cout << "Enter Username: ";
  getline(cin, newAccount.username);

  do {
    // Get user input for password with validation
    cout << "Enter Password (minimum 8 characters with at least 1 number and 1 special symbol): ";
    getline(cin, password);
  } while (!validatePassword(password));

  newAccount.password = password;

  // Open file for writing account in append mode
  ofstream outfile("accounts.txt", ios::app);

  if (outfile.is_open()) {
    outfile << newAccount.username << endl;
    outfile << newAccount.password << endl;
    outfile.close();
    cout << "Account created successfully!" << endl;
  } else {
    cerr << "Error opening file for account storage!" << endl;
  }
}

void Tickets(){
    int ticket, pw;
    
    reset:
    cout << "Enter your Password: ";
    cin >> pw;
    if (pw == Account.password){
        cout << "How many tickets will you order?: ";
        cin >> ticket;
        
        cout << "Thank you for ordering!"<<endl;
    }
    else {
        cout << "Invalid Password. Try Again!" << endl;
        goto reset;
        system ("pause");
    }
}

void Book(){
    Schedule location[100] ={   
        {"San Mateo", "5:00"},
        {"Sta. Lucia", "5:30"},
        {"Santolan", "5:35"},
        {"Cubao", "4:00"},
        {"Philcoa", "4:30"},
    };
    
    for(int i = 0; i < 6; i++){
        cout << location[i].place << endl;
    }
}
int main(){
    int choice;
    
    do{
        cout << "=== MKD Bus Ticket System ===" << endl;
        cout << "1. Create Account" << endl;
        cout << "2. Order Ticket" << endl;
        cout << "3. Book Schedule" << endl;
        cout << "4. View Schedule" << endl;
        cout << "5. Display Account" << endl;
        cout << "6. EXit" << endl;
        
        cin >> choice;
        cin.ignore();
        
        switch (choice){
        case 1:
            cout << "Account Creation screen" << endl;
            createAccount ();
            break;
        case 2:
            cout << "Order Tickets" << endl;
            Tickets ();
            break;
        case 3:
            cout << "Booking" << endl;
            Book ();
            break;
        case 4:
            cout << "Schedule Viewer" << endl;
            break;
        case 5:
            cout << "Account Information" << endl;
            break;
        case 6:
            cout << "Exit" << endl;
            break;

        }
    }while (choice != 6);
}

它还没有完成,但我想指出一些困扰我的事情。第 56 行,出现“.”之前需要主表达式的错误令牌不断弹出。有办法解决这个问题吗?

我尝试在 Tickets() 函数中添加 newAccount 。但它不起作用。我期望它能工作,但它没有修复错误。

c++ function struct void
1个回答
1
投票

第 56 行,出现“.”之前需要主表达式的错误令牌不断弹出。有办法解决这个问题吗?

第56行是这样的:

    if (pw == Account.password){

Account
type 的名称,
password
是该类型的 instance 字段。因此,
Account.password
无法识别可以与
pw
进行比较的对象。据推测,您想要与某些现有
password
实例的
Account
成员进行比较,但不清楚是哪一个。即使您的
createAccount()
函数也不提供在该函数完成后仍然存在的
Account
实例。

© www.soinside.com 2019 - 2024. All rights reserved.