#include <iostream>
#include <fstream>
#include <ctime>
#define deposit 1;
#define withdraw 2;
usingnamespace std;
int main()
{
ofstream inputFile ("Account Balance.txt");
int choice;
int bankmoney;
int money;
int balance;
int answer, reply;
srand (time(0));
bankmoney = rand() % 1000 + 1;
cout << "Welcome to Wendigo Bank!" << endl;
cout << "What would you like to do today?" << endl;
cout << "(1) Deposit " << endl;
cout << "(2) Withdraw " << endl;
cin >> choice;
switch (choice)
{
case 1:
cout << "Let me take you to your safe. Follow me please!" << endl;
cout << "Here we are, SAFE #231!" << endl;
balance = bankmoney;
cout << "Currently your balance is " << balance << " dollars." << endl;
cout << "How much would you like to deposit?" << endl;
cin >> money;
cout << "Depositing: " << money << endl;
cout << "Are you sure? (Y) or (N)" << endl;
cin.get();
cin >> answer;
if (answer == 'N' || answer == 'n')
{
cout << "Leaving the bank now. Please press a button." << endl;
cin.get();
return(0);
}
else
{
cout << "Thank you. Your balance is now " << balance + money << " dollars." << endl;
cout << "Would you like a receipt? (Y) or (N)" << endl;
cin >> reply;
if (reply == 'N' || reply == 'n')
{
cout << "Right, no receipt." << endl;
cout << "We will now leave. Thanks for visiting Wendigo Bank!" << endl;
system("PAUSE");
return(0);
}
else
{
cout << "I got that. I will now print a receipt named 'Account Balance." << endl;
inputFile.open("Account Balance.txt");
if (inputFile.is_open())
{
inputFile << "**** Wendigo Bank - Safe #231 Deposit Receipt ****" << endl;
inputFile << "Current Account Balance - " << balance << "." << endl;
inputFile << "Thank you for visiting Wendigo Bank! Come again!" << endl;
inputFile.close();
cout << "Your receipt is now saved. We will now leave. Thank you!" << endl;
}
}
}
}
}
When I enter 'Y' for the "Are you Sure?" bit, it closes. In fact, it closes whenever I enter anything, regardless of if it is good or not. It worked a minute ago, but after I changed something, it didn't.
Thanks ^^
Something that could be causing trouble is that you have your variable reply declared as an int and not a char as it should be. Try changing that and see what happens.
int money;
int balance;
int answer, reply; //incorrect
should be char answer, reply;
also
1. Line 34 cin.get(); //is harmless but redundant.
2. In my opinion - If you give someone some options to choose from -
then checks should be carried out against all those options - for example if you say Y yes N No then anyother input should be treated as invalid.
On line 36 you only check against N and take any other input for example Z as a Yes
Ah okay. I fixed it and it worked. Thanks! But I have 1 more question - it doesn't save the receipt onto a text file. It creates the .txt file but nothing is keyed in.