When I run this code and enter in a statement that makes the "if" statement true, it runs the if statement and the else statement and not just the if statement and the code after the "else". I am using Virtual Studio community 2017 on Windows 10. Thanks for any help.
#include <iostream>
#include <string>
#include <stdlib.h>
#include <sstream>
usingnamespace std;
int main()
{
int bal = 451;
string addbal;
string pass;
string addtake;
string add;
string take;
string checkbal;
string checkname;
string checkgivekeep;
do {
cout << "What is your password: ";
cin >> pass;
if (pass == "Example") {
system("cls");
cout << "Welcome, Example." << endl << "You currently have $" << bal << " in your account." << endl;
}
else {
system("cls");
cout << "Wrong password, try again." << endl;
}
} while (pass != "yellow123");
cout << "Enter how much money you would like to add to your account: ";
cin >> addbal;
system("cls");
cout << "You now have $" << bal + stoi(addbal) << " in your account." << endl;
cout << "Type \"Add\" to add money to your account." << endl;
cout << "Type \"Take\" to take money from your account." << endl;
cin >> addtake;
if (addtake == "Add" || addtake == "add") {
system("cls");
cout << "Enter how much money you would like to add to your account: ";
cin >> add;
system("cls");
cout << "You now have $" << bal + stoi(addbal) + stoi(add) << " in your account." << endl;
}
else (addtake == "Take" || addtake == "take"); {
system("cls");
cout << "Enter how much money you would like to take from your account: ";
cin >> take;
system("cls");
cout << "You now have $" << bal + stoi(addbal) - stoi(take) << " in your account." << endl;
}
cout << "You may now write a check. Enter your desired balance: ";
cin >> checkbal;
system("cls");
cout << "Now enter who the check is for: ";
cin >> checkname;
system("cls");
cout << "You now have a check for " << checkname << " that is worth $" << checkbal << "." << endl;
cout << "Would you like to give the check now? Type \"Yes\" or \"No\": ";
cin >> checkgivekeep;
if (checkgivekeep == "Yes" || checkgivekeep == "yes") {
system("cls");
cout << "You have given " << checkname << " $" << checkbal << "." << endl;
cout << checkname << " currently has " << checkbal << " in their bank account.";
}
else (checkgivekeep == "No" || checkgivekeep == "no"); {
system("cls");
cout << "Ok, the check will be held for you.";
}
cin.clear();
cin.ignore();
cin.get();
return 0;
}
else should not have a condition next to it because it takes in all other exceptions. You also put a semicolon after the else condition ending that and creating a new line of code not associated to the if else statement.
It should be like this for all your if else statements:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
if (addtake == "Add" || addtake == "add") {
system("cls");
cout << "Enter how much money you would like to add to your account: ";
cin >> add;
system("cls");
cout << "You now have $" << bal + stoi(addbal) + stoi(add) << " in your account." << endl;
}
else {
system("cls");
cout << "Enter how much money you would like to take from your account: ";
cin >> take;
system("cls");
cout << "You now have $" << bal + stoi(addbal) - stoi(take) << " in your account." << endl;
}