You are using a version without Ads of this website. Please, consider donating:

### Wrong Output

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155`` ``````#include #include #include #include class ACCOUNT { private: int Acno; char Name[20]; float Balance; public: void Init(); void Show() { cout<<"Account Number: "<>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<>Acno; for(int i=0; i>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."<>deposit; S[i].Deposit(deposit); break; } default: { cout<<"Incorrect choice."<>n; }while(n>50); cout<

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
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?
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?
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
@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.
but then what happens to any data thats not ints?
The same thing that happens to any data that is ints.
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
 ``12345678910`` ``````//some code removed, just to make the issue clearer void Register(int n) { ACCOUNT S[50]; for(int i=0; i
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:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156`` ``````PROGRAM: #include #include #include #include class ACCOUNT { private: int Acno; char Name[20]; float Balance; public: void Init(); void Show() { cout<<"Account Number: "<>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<>a; cout<>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."<>deposit; S[i].Deposit(deposit); break; } default: { cout<<"Incorrect choice."<>n; }while(n>50); cout<

 ``` 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.

You are using a version without Ads of this website. Please, consider donating: