Program Trouble

I tried to make a program but after I edited something it suddenly changed.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <fstream>
#include <ctime>
#define deposit 1;
#define withdraw 2;
using namespace 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 ^^
First, what did you change in this new version?

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.
1
2
3
    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.
The problem is a clash between line 10 ofstream inputFile ("Account Balance.txt");and line 57 inputFile.open("Account Balance.txt");.

Just get rid of line 57.
Oh many thanks! It worked.
Topic archived. No new replies allowed.