如何修复C++中‘expected primary-expression before '.' token’编译错误?
MKD巴士票务系统C++代码编译错误修复
我编写了一个MKD巴士票务系统的C++代码(代码如下),目前尚未完成,但第56行持续出现expected primary-expression before '.' token编译错误。我尝试在Tickets()函数中添加newAccount变量,但问题仍未解决,请问该如何修复这个错误?
原代码
#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; // 获取用户名输入 cout << "Enter Username: "; getline(cin, newAccount.username); do { // 获取密码输入并验证 cout << "Enter Password (minimum 8 characters with at least 1 number and 1 special symbol): "; getline(cin, password); } while (!validatePassword(password)); newAccount.password = password; // 追加模式打开文件存储账户 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行if (pw == Account.password)存在三个核心问题:
- 非法访问结构体成员:
Account是结构体类型,不是具体的实例对象,C++不允许直接通过类型名访问成员变量,必须通过该结构体的实例(比如currentUser.password)来访问。 - 类型不匹配:用
int类型的pw存储密码,但密码是字符串类型,不仅会导致输入截断(比如密码含非数字字符时输入失败),还无法和string类型的password正确比较。 - 缺少用户上下文:
Tickets函数没有知道当前要验证哪个用户的密码,系统没有维护登录状态,无法对应到已创建的账户。
修复方案
步骤1:添加登录函数,维护用户登录状态
新增一个login函数,读取存储的账户信息,验证用户输入的账号密码,返回登录是否成功,并传递当前登录用户的信息:
bool login(Account& currentUser) { string username, password; cout << "Enter Username: "; getline(cin, username); cout << "Enter Password: "; getline(cin, password); ifstream infile("accounts.txt"); if (!infile.is_open()) { cerr << "No accounts found! Please create an account first." << endl; return false; } string storedUser, storedPass; while (getline(infile, storedUser) && getline(infile, storedPass)) { if (storedUser == username && storedPass == password) { currentUser.username = username; currentUser.password = password; cout << "Login successful!" << endl; infile.close(); return true; } } infile.close(); cout << "Invalid username or password!" << endl; return false; }
步骤2:修改Tickets函数,接收当前登录用户信息
调整Tickets函数的参数,传入已登录的用户实例,同时修正密码输入的类型为string:
void Tickets(const Account& currentUser) { int ticket; string inputPw; reset: cout << "Enter your Password: "; getline(cin, inputPw); if (inputPw == currentUser.password) { cout << "How many tickets will you order?: "; cin >> ticket; cin.ignore(); // 清除输入缓冲区的换行符,避免后续getline出错 cout << "Thank you for ordering!" << endl; } else { cout << "Invalid Password. Try Again!" << endl; goto reset; } }
步骤3:更新main函数中的购票逻辑
在选择“订购车票”时,先执行登录验证,登录成功后再调用Tickets函数:
case 2: cout << "Order Tickets" << endl; Account currentUser; if (!login(currentUser)) { break; } Tickets(currentUser); break;
修复后代码说明
- 现在
Tickets函数通过传入的currentUser实例访问密码,解决了原有的编译错误。 - 密码输入改为
string类型,解决了类型不匹配问题。 - 新增的登录逻辑让系统能识别当前操作的用户,符合票务系统的基本流程。
内容的提问来源于stack exchange,提问作者johann1220
相关产品推荐
相关产品推荐

