Question: I need help, when I put the preloaded user, the program works normally, but I need that if I put a wrong user it gives

I need help, when I put the preloaded user, the program works normally, but I need that if I put a wrong user it gives me the option to add user, and asks me for the user again, so when I put the one I added as new, it works normally.
Here my code
int main(){
vector items ={
Item("Paper",50),
Item("Feather",20),
Item("Laptop",100),
Item("Pencil",10),
Item("Draft",5)
};
vector cart;
string inputUser, inputPass;
cout << "User: ";
cin >> inputUser;
cout << "Password: ";
cin >> inputPass;
if (User::validateUser(inputUser, inputPass)){
cout <<"--- Welcome to my online store ---"<> choice;
switch (choice){
case 1: {
int subChoice;
cout <<"1. View items below a certain price" << endl;
cout <<"2. View items above a certain price" << endl;
cout <<"3. Search by name" << endl;
cout << "Enter your choice: ";
cin >> subChoice;
if (subChoice ==1|| subChoice ==2){
double priceLimit;
cout << "Enter price limit: ";
cin >> priceLimit;
cout << "Available items:
";
for (int i =0; i < items.size(); i++){
if ((subChoice ==1 && items[i].getPrice()< priceLimit)||(subChoice ==2 && items[i].getPrice()> priceLimit)){
cout << i +1<<"."<< items[i].getName()<<"- Price: $"<< items[i].getPrice()<< endl;
}
}
}
else if (subChoice ==3){
string itemName;
cout << "Enter item name: ";
cin >> itemName;
for (const auto & item : items){
if (item.getName()== itemName){
cout << "Item: "<< item.getName()<<", Price: $"<< item.getPrice()<< endl;
}
}
}
char addToCart;
cout <<"Do you want to add an item to your cart? (Y/N): ";
cin >> addToCart;
if (addToCart =='Y'|| addToCart =='y'){
int itemNumber;
int itemQuantity;
cout << "Enter item number: ";
cin >> itemNumber;
cout << "Enter the amount: ";
cin >> itemQuantity;
if (itemNumber >0 && itemNumber <= items.size()){
items[itemNumber -1].setQuantity(itemQuantity);
cart.push_back(items[itemNumber -1]);
cout << "Item added to cart." << endl;
}
else {
cerr << "Invalid item number." << endl;
}
string cont;
cout <<"Do you want to exit to the main menu or continue adding items?(Menu/Add): ";
cin >> cont;
while (cont == "Add"){
int itemNum, qty;
cout << "Enter item number: ";
cin >> itemNum;
cout << "How many pieces?";
cin >> qty;
items[itemNum -1].setQuantity(qty);
cart.push_back(items[itemNum -1]);
cout << "Item added to cart.
";
cout <<"Do you want to exit to the main menu or continue adding items?(Menu/Add): ";
cin >> cont;
}
}
break;
}
case 2: {
cout << "Your cart:" << endl;
double total =0;
for (size_t i =0; i < cart.size(); i++){
cout <<(i +1)<<"."<< cart[i].getName()<<", Price: $"<< cart[i].getPrice()<<", Quantity: "<< cart[i].getQuantity()<< endl;
total += cart[i].getPrice()* cart[i].getQuantity();
}
cout << "Total cost: $"<< total << endl;
string checkout;
cout <<"Do you want to pay or continue shopping?? (Pay/Continue): ";
cin >> checkout;
if (checkout == "Checkout"){
cout << "Press Ok to close the sale..
";
string Ok;
cin >> Ok;
if (Ok =="Ok"){
cout << "Sale successfully closed. Thank you for shopping with us!"<< endl;
cart.clear();
}
}
break;
}
case 3:
cout << "Goodbye!" << endl;
break;
default:
cerr << "Invalid choice." << endl;
break;
}
}
while (choice !=3);
}
else {
cerr << "Unregistered user." << endl;
cout <<""<< endl;
string register;
cin >> register;
if (register =="*"){
string username, password;
cout << "Enter your username: ";
cin >> username;
cout << "Enter your password: ";
cin >> password;
}
}
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!