我今天用c++做了一个支付小程序,结果报错了,代码如下:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
struct User {
std::string username;
std::string password;
double balance;
};
int main() {
std::vector<User> users;
std::string username, password;
int choice;
double amount;
// 从文件中读取用户数据
std::ifstream input("users.txt");
while (input >> username >> password >> amount) {
users.push_back({username, password, amount});
}
input.close();
while (true) {
std::cout << "1.注册 2.登录 3.退出\n请选择操作:";
std::cin >> choice;
if (choice == 1) {
std::cout << "请输入用户名:";
std::cin >> username;
std::cout << "请输入密码:";
std::cin >> password;
users.push_back({username, password, 0});
std::cout << "注册成功!";
} else if (choice == 2) {
std::cout << "请输入用户名:";
std::cin >> username;
std::cout << "请输入密码:";
std::cin >> password;
bool found = false;
for (const auto& user : users) {
if (user.username == username && user.password == password) {
found = true;
std::cout << "登录成功!";
while (true) {
std::cout << "1.充值 2.支付 3.查看余额 4.退出登录\n请选择操作:";
std::cin >> choice;
if (choice == 1) {
std::cout << "请输入充值金额:";
std::cin >> amount;
User.balance += amount;
std::cout << "充值成功!";
} else if (choice == 2) {
std::cout << "请输入支付金额:";
std::cin >> amount;
if (User.balance >= amount) {
User.balance -= amount;
std::cout << "支付成功!";
} else {
std::cout << "余额不足!";
}
} else if (choice == 3) {
std::cout << "当前余额:" << User.balance << std::endl;
} else if (choice == 4) {
break;
} else {
std::cout << "无效的选择,请重新输入!";
}
}
break;
}
}
if (!found) {
std::cout << "用户名或密码错误!";
}
} else if (choice == 3) {
break;
} else {
std::cout << "无效的选择,请重新输入!";
}
}
// 将用户数据写入文件
std::ofstream output("users.txt");
for (const auto& user : users) {
output << user.username << " " << user.password << " " << user.balance << std::endl;
}
output.close();
return 0;
}
报错信息:
C:\Users\cxy\Desktop\c++\支付小程序\index.cpp In function 'int main()':
54 33 C:\Users\cxy\Desktop\c++\支付小程序\index.cpp [Error] expected unqualified-id before '.' token
59 37 C:\Users\cxy\Desktop\c++\支付小程序\index.cpp [Error] expected unqualified-id before '.' token
60 37 C:\Users\cxy\Desktop\c++\支付小程序\index.cpp [Error] expected unqualified-id before '.' token
66 62 C:\Users\cxy\Desktop\c++\支付小程序\index.cpp [Error] expected primary-expression before '.' token
(注:请不要用AI回答,最好发修改后的代码)