Wrong Output

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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include<fstream.h>	
#include<conio.h>
#include<stdio.h>
#include<string.h>

class ACCOUNT
{
private:
	int Acno;
	char Name[20];
	float Balance;

public:

	void Init();

	void Show()
	{
		cout<<"Account Number: "<<Acno<<endl;
		cout<<"Account Name: "<<Name<<endl;
		cout<<"Account's Balance: "<<RBalance()<<endl;
	}//void Show() closes.

	void Deposit(int Amt)
	{
		Balance+=Amt;
	}//void Deposit() closes.

	void Withdraw(int Amt)
	{
		Balance-=Amt;
	}//void Withdraw() closes.

	float RBalance()
	{
		return Balance;
	}//float Rbalance() closes.

	int RAcno()
	{
		return Acno;
	}

};//Class closes.

void ACCOUNT::Init()
{
	
	 cout<<"Enter Account number: ";
	 cin>>Acno;
	 cout<<"Enter Account Name: ";
	 gets(Name);
	 do
	 {
	cout<<"Enter the account's balance greater than or equal to Rs.500: ";
	    cin>>Balance;
	 }while(Balance<500);
	cout<<endl;
}//void Init() closes.

void Register(int n)
{
	fstream fil;
	fil.open("Accounts.dat", ios::binary|ios::out);
	ACCOUNT S[50];
	char ch;

    for(int i=0; i<n; i++)
	{
		cout<<"Enter details of account "<<i+1<<endl<<endl;
	 
		S[i].Init();
		fil.write((char*)&S,sizeof(S));
	}

	fil.close();
}//void Register() closes.

void Transact(int n)
{
	ACCOUNT S[50];
	int Acno, choice, withdraw, deposit;
	cout<<"Enter the Account Number from whom you want to Deposit or Withdraw money: ";
	cin>>Acno;
	for(int i=0; i<n; i++)
	{
		if(Acno==S[i].RAcno())
		{
		
			cout<<"1. Withdraw Money."<<endl;
			cout<<"2. Deposit Money."<<endl;
			cout<<"Enter your choice: "<<endl;
			cin>>choice;
			switch (choice)
			{
			 case 1:
				 {
					 cout<<"Enter the amount of money you want to withdraw: ";
				     cin>>withdraw;
				     if(S[i].RBalance()-withdraw<500)
					  cout<<"You cannot withdraw such an amount of money as this makes the balance of the account less than Rs.500."<<endl;
				     else
					  S[i].Withdraw(withdraw);
					 break;
				 }
			 case 2:
				 {
					 cout<<"Enter the amount you money you want to deposit: ";
					 cin>>deposit;
					 S[i].Deposit(deposit);
					 break;
				 }
			 default:
				 {
					 cout<<"Incorrect choice."<<endl;
					 break;
				 }
			}//Switch Case closes.


		}//if loop closes.
	}//for loop closes
}//void Transact() closes.

void DisplayAll(int n)
{
	ACCOUNT S[50];
	cout<<"The details of all the account holders are: "<<endl<<endl;
	for(int i=0; i<n; i++)
	{ 
	   cout<<"Details of account "<<i+1<<":"<<endl<<endl;
       S[i].Show();
	   cout<<endl;
	}
}//void DisplayAll() closes.

void main()
{
	clrscr();
	int n;
	do
	{
	  cout<<"Enter the number of accounts you want to enter less than or equal to 50: ";
	  cin>>n;
	}while(n>50);
	cout<<endl;
	Register(n);
	cout<<endl;
	DisplayAll(n);
	cout<<endl;
	Transact(n);
	cout<<"The details of all the account holder(s) after the transaction you made is/are:"<<endl<<endl;
	DisplayAll(n);
	getch();
}//void main() closes. 


But i get the follwing output which is completely wrong..

Enter the number of accounts you want to enter less than or equal to 50: 2

Enter details of account 1

Enter Account number: 1
Enter Account Name: abc
Enter the account's balance greater than or equal to Rs.500: 6554

Enter details of account 2

Enter Account number: 2
Enter Account Name: iit
Enter the account's balance greater than or equal to Rs.500: 9842


The details of all the account holders are:

Details of account 1:

Account Number: 0
Account Name:  
Account's Balance: -9.472836e-26

Details of account 2:

Account Number: 0
Account Name:
Account's Balance: -2.262968e-33


Enter the Account Number from whom you want to Deposit or Withdraw money: 1
The details of all the account holder(s) after the transaction you made is/are:

The details of all the account holders are:

Details of account 1:

Account Number: 0
Account Name:  
Account's Balance: -9.472836e-26

Details of account 2:

Account Number: 0
Account Name:
Account's Balance: -2.262968e-33



Last edited on
closed account (Dy7SLyTq)
a) should be int main not void
b)i would use std::vector or dynamic arrays instead of creating an array of 50 to hold two elements
c) look at line 73: fil.write((char*)&S,sizeof(S));. your trying to convert an ACCOUNT to a char*, which wont work. you need to either a) overload the >> operator for ofstream, or b) write getters and setters and write it all to file manually
Whatever you told is not in my school syllabus DTSCode... Can you give some other advice for this?
closed account (Dy7SLyTq)
theres not really much else you can do. the only thing that can be written to file are ints, chars, bools, floats, and char *'s. anything else has been abstracted, making it look like it can be written to file, with an overloaded << (i put >> which was wrong in this case), manually doing it, or writing a method for it. theres nothing else you can do short of messing with the compiler
I didn't understand what you are saying since most of the things aren't in what I have to know. Besides I just want to know why does the output come out to be weird? Like no account name, account number as 0, account balance is not displayed as a a number but rather a mixture of numbers and alphabets (like e)..Why does this happen? and what to do to correct it?
closed account (Dy7SLyTq)
i already told you. look at c)
i already told you. look at c)


c) is not correct.

@OP - Variables in different functions which have the same name (such as ACCOUNT in DisplayAll, Transact, and Register) are not the same variable, they just happen to have the same name. You would do well to revisit passing variables to functions.
Last edited on
closed account (Dy7SLyTq)
@cire: not arguing, but i just want to know... how am i incorrect? how does Account convert to char *?
how does Account convert to char *?


Account does not. Account* to char* is a reinterpret_cast hidden within the c-style cast.

You could've attempted compiling it yourself.
closed account (Dy7SLyTq)
but then what happens to any data thats not ints?
The same thing that happens to any data that is ints.
closed account (Dy7SLyTq)
im sorry my mistake... i reviewed the source code and realized my mistake. i thought he was attempting to display everything by reading back from the file. hes only writes to it
Another thing
1
2
3
4
5
6
7
8
9
10
//some code removed, just to make the issue clearer
void Register(int n)
{
	ACCOUNT S[50];
	for(int i=0; i<n; i++)
	{	 
		S[i].Init();
		fil.write((char*)&S,sizeof(S));
	}
}
You are writing the whole array every single time.



> You cannot withdraw such an amount of money as this makes the balance of the account less than Rs.500.
but... it is my money, ¿why shouldn't I be able to take it?
So what should I basically do to correct my code?
And you can't have less than Rs.500 as the bank requires atleast Rs. 500 to be deposited within each account. Rule of the bank.
Anybody there?
@Winkerd

So what should I basically do to correct my code?


So, what about the advice given in your other topic (which is about the same question - don't do that). Not happy with it? Don't feel you need to change your code? Wondering why your code still doesn't work?
My edited program after removing the respective run-time errors is as follows:

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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
PROGRAM:
#include<fstream.h>	
#include<conio.h>
#include<stdio.h>
#include<string.h>

class ACCOUNT
{
private:
	int Acno;
	char Name[20];
	float Balance;

public:

	void Init();

	void Show()
	{
		cout<<"Account Number: "<<Acno<<endl;
		cout<<"Account Name: "<<Name<<endl;
		cout<<"Account's Balance: "<<RBalance()<<endl;
	}//void Show() closes.

	void Deposit(int Amt)
	{
		Balance+=Amt;
	}//void Deposit() closes.

	void Withdraw(int Amt)
	{
		Balance-=Amt;
	}//void Withdraw() closes.

	float RBalance()
	{
		return Balance;
	}//float Rbalance() closes.

	int RAcno()
	{
		return Acno;
	}
};//Class closes.

void ACCOUNT::Init()
{
	
	 cout<<"Enter Account number: ";
	 cin>>Acno;
	 cout<<"Enter Account Name: ";
	 gets(Name);
	 do
	 {
	cout<<"Enter the Account's balance greater than or equal to Rs.500: ";
	    cin>>Balance;
	 }while(Balance<500);
	cout<<endl;
}//void Init() closes.

void Register(ACCOUNT myAccount[], int n)
{
	fstream fil;
	fil.open("Accounts.dat", ios::binary|ios::out);
	cout<<"Enter details of account "<<n+1<<":"<<endl<<endl;
	myAccount[n].Init();
	fil.write((char*)&myAccount,sizeof(myAccount));
	fil.close();
}//void Register() closes.

void Transact(int n)
{
	ACCOUNT S[50];
	int a, choice, withdraw, deposit;
	cout<<"Enter the Account Number from whom you want to Deposit or Withdraw money: ";
	cin>>a;
	cout<<endl;
	for(int i=0; i<n; i++)
	{
		if(a==S[i].RAcno())
		{
		
			cout<<"1. Withdraw Money."<<endl;
			cout<<"2. Deposit Money."<<endl;
			cout<<"Enter your choice: "<<endl;
			cin>>choice;
			switch (choice)
			{
			 case 1:
				 {
					 cout<<"Enter the amount of money you want to withdraw: ";
				     cin>>withdraw;
				     if(S[i].RBalance()-withdraw<500)
					  cout<<"You cannot withdraw such an amount of money as this makes the balance of the account less than Rs.500."<<endl;
				     else
					  S[i].Withdraw(withdraw);
					 break;
				 }
			 case 2:
				 {
					 cout<<"Enter the amount you money you want to deposit: ";
					 cin>>deposit;
					 S[i].Deposit(deposit);
					 break;
				 }
			 default:
				 {
					 cout<<"Incorrect choice."<<endl;
					 break;
				 }
			}//Switch Case closes.


		}//if loop closes.
	}//for loop closes
}//void Transact() closes.

void DisplayAll(ACCOUNT myAccount[], int n)
{
       myAccount[n].Show();
	   cout<<endl;
}//void DisplayAll() closes.

void main()
{
	clrscr();
	int n;
	ACCOUNT S[50];
	do
	{
	  cout<<"Enter the number of Account(s) you want to enter less than or equal to 50: ";
	  cin>>n;
	}while(n>50);
	cout<<endl;
	cout<<"Enter the details of the Accounts:"<<endl<<endl;
	for(int i=0; i<n; i++)
	{
	    Register(S,i);
        cout<<endl;
	}
	cout<<"The details of all Account(s) you entered is/are:"<<endl<<endl;
	for(i=0; i<n ; i++)
	{
		DisplayAll(S,i);
		cout<<endl;
	}
	Transact(n);
	cout<<"The details of all the account holder(s) after the transaction you made is/are:"<<endl<<endl;
	
	for(i=0; i<n; i++)
	{
	    DisplayAll(S,i);
		cout<<endl;
	}
	getch();
}//void main() closes. 




OUTPUT:

Enter the number of Account(s) you want to enter less than or equal to 50: 2

Enter the details of the Accounts:

Enter details of account 1:

Enter Account number: 1
Enter Account Name: abc
Enter the Account's balance greater than or equal to Rs.500: 10000


Enter details of account 2:

Enter Account number: 2
Enter Account Name: pqr
Enter the Account's balance greater than or equal to Rs.500: 9000


The details of all Account(s) you entered is/are:

Account Number: 1
Account Name: abc
Account's Balance: 10000


Account Number: 2
Account Name: pqr
Account's Balance: 9000


Enter the Account Number from whom you want to Deposit or Withdraw money: 1

The details of all the account holder(s) after the transaction you made is/are:

Account Number: 1
Account Name: abc
Account's Balance: 10000


Account Number: 2
Account Name: pqr
Account's Balance: 9000


Whereas the OUTPUT should have been like:

OUTPUT: 

Enter the number of Account(s) you want to enter less than or equal to 50: 2

Enter the details of the Accounts:

Enter details of account 1:

Enter Account number: 1
Enter Account Name: abc
Enter the Account's balance greater than or equal to Rs.500: 10000


Enter details of account 2:

Enter Account number: 2
Enter Account Name: pqr
Enter the Account's balance greater than or equal to Rs.500: 9000


The details of all Account(s) you entered is/are:

Account Number: 1
Account Name: abc
Account's Balance: 10000


Account Number: 2
Account Name: pqr
Account's Balance: 9000


Enter the Account Number from whom you want to Deposit or Withdraw money: 1



The details of all the account holder(s) after the transaction you made is/are:

1.Withdraw money
2.Deposit money

Enter your choice: 2

Enter the amount of money you want to deposit: 900

The details of all the account holder(s) after the transaction you made is/are:

Account Number: 1
Account Name: abc
Account's Balance: 10900


Account Number: 2
Account Name: pqr
Account's Balance: 9000


That is, the transact function is never able to execute the the statements inside the if(a==S[i].RAcno()) loop or as a matter of fact this statement itself. Why does this happen? If the "if" statement finds a match shouldn't it execute the statements which are inside it? Help please...thanks for your earlier replies I corrected almost all my errors except this one. All the errors were run-time errors and this is the last run-time error.
Last edited on
That is, the transact function is never able to execute the the statements inside the if(a==S[i].RAcno()) loop or as a matter of fact this statement itself. Why does this happen? If the "if" statement finds a match shouldn't it execute the statements which are inside it?


It happens for the same reason your earlier code didn't work. Variables with the same name in different functions are not the same variables, despite sharing the same name. Transact works on the local array S not the one defined in main.

Your code also appears to suffer from some other problems, like Register repeatedly writing to the same location in the output file on different invocations. I would really suggest working on (and testing) one aspect of the program and getting it to work before trying to fit everything together.
Topic archived. No new replies allowed.