I am writing a program for my intro to C class and keep getting some warnings when I try to compile with gcc.
Here is my code:
int main ()
{
float balance;
float beg_balance;
float withdrawal_amt;
float deposit_amt;
float amount;
int total_withdrawals;
int total_deposits;
int selection;
print_greeting ();
printf("Let's begin with your beginning balance");
beg_balance = get_positive_value();
do
{
print_menu ();
scanf("%d", &selection);
switch (selection)
{
case WITHDRAWAL:
get_positive_value();
balance = withdrawal(balance, withdrawal_amt, amount);
break;
case DEPOSIT:
get_positive_value();
balance = deposit(balance, deposit_amt, amount);
break;
case SUMMARY:
print_receipt(total_withdrawals, total_deposits, beg_balance, balance, \
withdrawal_amt, deposit_amt);
break;
case QUIT:
break;
default: printf("Invalid selection");
break;
}
}
while(selection != 4);
return 0;
The errors I am getting when compiling is this:
project.c: In function ‘main’:
project.c:46: warning: ‘withdrawal_amt’ may be used uninitialized in this function
project.c:46: warning: ‘amount’ may be used uninitialized in this function
project.c:50: warning: ‘deposit_amt’ may be used uninitialized in this function
project.c:53: warning: ‘total_withdrawals’ may be used uninitialized in this function
project.c:53: warning: ‘total_deposits’ may be used uninitialized in this function
Any ideas why? Thank you
EDIT:
Now i am having trouble creating a register function that is used to print out the transaction history of the account. It should print out the beginning and ending balances, as well as a table that shows all transactions (Deposits and Withdraws) that have occurred. Any help would be greatly appreciated
You never attribute them any value. it's like if you wrote :
You need to init them like :
The errors you are getting are not errors but warnings. They point out that you do not initialize any of your automatic storage variables, so they will start up with an unspecified value.
You can initialize your variables, say to
0
, and the warnings will disappear.I think you want to use your function
get_positive_value()
like this:withdrawal_amt = get_positive_value();
and similarly others.
You are passing
withdrawal_amt
,amount
and other variables mentioned in warnings unintialized.Note that all the variables declared inside some function are stored in some random memory (stack memory) location which compiler chooses, and that location may contain some garbage value which will be taken as initial value of your variables.
Hence compiler instructs you beforehand to initialize them to some known value, so that you do not get your bank balance
-1000.00 USD
when you 'deposited'1000.00 USD
;-)