mardi 19 janvier 2021

How do I make multiple if statements inside one another taking user input in C++?

so I am new to programming, especially with C++ and I am trying to write a bank program, where I am currently trying to make the person an "account" asking them if they want to make a Basic, Premium, or Elite account, and then confirming the account with a yes or no answer from the user. Here is my code:

#include <iostream>
using namespace std;

int main()
{

string mystr;
cout << "Hello there, which account would you like to start today? Basic, Premium or Elite? ";
getline (cin, mystr);

string yn_answer;
if (mystr == "Elite") {
    cout << "You have chosen the Elite account option. Is this correct? (y/n): ";
    getline (cin, yn_answer);
if (yn_answer == "y")
cout << "Great, setting up account... \n" ;
cout << "25% complete. \n";
cout << "50% complete. \n";
cout << "75% complete. \n";
cout << "100% complete. \n";
cout << "Process complete. \n"; }
else {
    cout << "I'm sorry for the inconvenience, please restart the program. Thanks! ";
}


if (mystr == "Premium") {
    cout << "Hello, Premium";
}

if (mystr == "Basic") {
    cout << "Hello, Basic";
}

if (mystr != "Basic", "Elite", "Premium") {
cout << "I'm sorry, you did not choose one of the following options above. Please try again.";
}

return 0;
}

Currently I only have the first one, Elite, done and the Basic and Premium will be copied over when I can figure out how to get it to work. However, when I try to run this by typing Elite in as the user input I get the character output, "I'm sorry, you did not choose one of the following options above. Please try again.", from the last line trying to check if the user entered in something other than Elite, Basic, or Premium.

If I type in something other than the three options, I should get "I'm sorry, you did not choose one of the following options above. Please try again". However, I get the first "error" message that is supposed to be checking for the user confirming the account type as well as the actual error message I am supposed to be getting.

I'm very new to programming, and I don't really know how to explain these things. If you don't understand something I'm trying to do, please let me know and I'll try to explain it better. Also, is there a way that I can do this better, or is there a way to fix what I am doing here? Thanks.

*Also, I've heard that using namespace for std isn't a good practice to do, but I'm currently using an online compiler and that was how it was originally and I don't want to change things around now after working on this and mess it up more than it already is. I've been working on this for a while and have been fixing it up until this point where I don't know how to fix it.

Aucun commentaire:

Enregistrer un commentaire