Need help to apply file (fstream)

Question: Is this applicable for file ? I'm still new to file system so i doubt which part i can apply it.

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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
 #include <iostream>
#include <string>
using namespace std;
void MainMenu();
int main(){
   int password,pincode,option;
   string name;
   system("color 1F");
   cout<<"Do you have an account?"<<endl;
   cout<<"1. Yes 2. NO "<<endl;
   cin>>option;
   if(option==2){
      cout<<"Registration"<<endl;
      cout<<"Enter your name"<<endl;
      getline(cin,name);
      cout<<"Enter your pincode"<<endl;
      cin>>pincode;
   }
   else
      cout<<"\t\t WELCOME TO OUR PROJECT \t\t"<<endl;
      cout<<"****** PASSWORDS ******"<<endl;
      cout<<"1. 2222 for Puvi"<<endl;
      cout<<"2. 3333 for ShiawYen"<<endl;
      cout<<"3. 4444 for Almond"<<endl;
      cout<<"4. 5555 for Kye Peng"<<endl;
      cout<<"Enter your password:"<<endl;
      cin>>password;
   if(password==pincode){
      cout<<"Welcome"<<name<<endl;
      MainMenu();
   }
   else if(password==2222){
      cout<<"Welcome Puvi"<<endl;
      MainMenu();
   }
   else if(password==3333){
      cout<<"Welcome Shiaw Yen"<<endl;
      MainMenu();
   }
   else if(password==4444){
      cout<<"Welcome Almond"<<endl;
      MainMenu();
   }
   else if(password==5555){
      cout<<"Welcome Kye Peng"<<endl;
      MainMenu();
   }
   else 
      cout<<"Please register first"<<endl;
}
   
void MainMenu(){
   bool FirstTime=true;
   double withdraw,balance,deposit;
   int iAnswer,option;
   balance=10000;
   while(iAnswer==1||FirstTime==true){
   cout<<"\t\t Main Menu \t\t"<<endl;
   cout<<"Automated Teller Machine"<<endl;
   cout<<"1. Inquire Balance"<<endl;
   cout<<"2. Withdraw"<<endl;
   cout<<"3. Deposit"<<endl;
   cout<<"4. Quit"<<endl;
   cout<<"Choose your option"<<endl;
   cin>>option;
   switch(option){
   case 1:
      cout<<"Balance Inquiry"<<endl;
      cout<<"Your current balance is RM:"<<balance<<endl;
      break;
   case 2:
      cout<<"Withdraw"<<endl;
      cout<<"Enter the amount you want to withdraw"<<endl;
      cin>>withdraw;
      balance=balance-withdraw;
      if(withdraw>balance)
         cout<<"Insuficient money to withdraw"<<endl;
      else
         cout<<"Your current balance is RM:"<<balance<<endl;
      break;
   case 3:
      cout<<"Deposit"<<endl;
      cout<<"Enter the amount you want to deposit"<<endl;
      cin>>deposit;
      balance=balance+deposit;
      cout<<"Your current balance is RM:"<<balance<<endl;
      break;
   case 4:
      cout<<"Exit"<<endl;
      system("cls");
      break;
   default:
      cout<<"Please enter valid choice"<<endl;
      break;
   }
   cout<<"Do you still want to proceed to another transaction?"<<endl;
   cout<<"1. Yes"<<endl;
   cout<<"2. No"<<endl;
   cin>>iAnswer;
   FirstTime=false;
 }
}
Last edited on
1
2
3
  while(iAnswer==1){
      MainMenu();
   }


Every time you go round, you are calling a whole new function. The function starts and does this:

1
2
3
 double withdraw,balance,deposit;
   int iAnswer,option;
   balance=10000;


So of course balance always seems to be 10000. You are creating a whole new balance every time, over and over.

You've basically completely misunderstood how a loop works, and how functions work. Do NOT call MainMenu() over and over again.

You should have a loop inside MainMenu(). It should look something like this:



1
2
3
4
5
6
7
8
9
10
11
12
13
14
void MainMenu(){
  double balance = 10000;
   int iAnswer = 1;
   while(iAnswer==1)
  {
     // all your code
    // lots of code

   // End of MainMenu() function
   cout<<"Do you still want to proceed to another transaction?"<<endl;
   cout<<"1. Yes"<<endl;
   cout<<"2. No"<<endl;
   cin>>iAnswer;
  }


Last edited on
How about the first entry because the user doesnt need to choose whether they want to continue for the first time.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void MainMenu()
{
  bool isVeryFirstTime = true;
  double balance = 10000;
   int iAnswer = 1;
   while(iAnswer==1)
  {
     // all your code
    // lots of code

   // End of MainMenu() function
  if (isVeryFirstTime)
 {
    isVeryFirstTime = false;
  }
  else
  { 
     cout<<"Do you still want to proceed to another transaction?"<<endl;
     cout<<"1. Yes"<<endl;
     cout<<"2. No"<<endl;
     cin>>iAnswer;
   }
}
Last edited on
Thank You :)
Topic archived. No new replies allowed.