![]() |
| | #1 |
| Registered User Join Date: Jan 2004
Posts: 50
| whats wrong with this? ![]() I know this is probably really simple to you lot but I need a bit of help with some coursework basically I've got a simple switch which initalises the first part of the code: Code: case 1:
printf ("1. Deposit funds \n");
balance = deposited (deposit);
break;
Code: float deposited (float balance)
{
float deposit;
float bal = (balance);
printf ("Please enter how much you wish to deposit \n");
scanf ("%f", &deposit);
if ((deposit + balance < 999999) && (deposit + balance > 0))
{
printf ("Thank you %f has being entered into your account \n", deposit);
}
if ((deposit + balance > 999999) && (deposit + balance < 0))
{
printf ("I'm sorry but the number you have entered is either too large or too small/n");
}
balance = deposit + balance;
return balance;
}
Apologies for the shoddily written code but I'm new to this. Thanks for your help |
| petedee is offline | |
| | #2 | |
| Code Goddess Join Date: Sep 2001
Posts: 9,661
| >Hey everyone, spot the newbie Too easy. Newbies always announce themselves as such for some strange reason. Probably a lack of confidence. We honestly don't care what your experience is as long as you don't act like a loser. Quote:
Deposit implies the amount to be added, not the current balance as the deposited function implies. This alone seems suspect. >scanf ("%f", &deposit); You should always check interactive input for success. >balance = deposit + balance; You change balance even when the value is out of range. Not good for keeping your promises. >float bal = (balance); Parentheses really aren't needed, but what is the point of the bal variable?
__________________ My best code is written with the delete key. | |
| Prelude is offline | |
| | #3 |
| Registered User Join Date: Jan 2004
Posts: 50
| heh, yea you got me, I've changed the code slightly since i posted this so: The call: Code: case 1:
printf ("1. Deposit funds \n");
balance = deposited (balance, new_balance);
break;
Code:
float deposited (float balance, float new_balance)
{
float deposit;
printf ("Please enter how much you wish to deposit \n");
scanf ("%f", &deposit);
if ((deposit + balance < 999999) && (deposit + balance > 0))
{
new_balance = deposit + balance;
printf ("Thank you %f has being entered into your account \n", deposit);
printf ("Your current balance wibble = %f \n", &new_balance);
}
if ((deposit + balance > 999999) && (deposit + balance < 0))
{
printf ("I'm sorry but the number you have entered is either too large or too small/n");
}
return (new_balance);
}
Last edited by petedee; 01-06-2004 at 01:01 PM. |
| petedee is offline | |
| | #4 |
| Code Goddess Join Date: Sep 2001
Posts: 9,661
| Your code is still a little goofy. Without the use of pointers to modify your passed argument, it would be better simply to pass the balance, make the modifications, then return the modified balance and save the return of deposited to balance. Something more like this: Code: float deposited (float balance)
{
float deposit;
printf ("Please enter how much you wish to deposit \n");
scanf ("%f", &deposit);
if ((deposit + balance < 999999) && (deposit + balance > 0))
{
balance = balance + deposit;
printf ("Thank you %.2f has been entered into your account\n", deposit);
printf ("Your current balance wibble = %.2f\n", balance);
}
else
printf ("I'm sorry but the number you have entered out of range/n");
return (balance);
}
Code: balance = deposited (balance);
__________________ My best code is written with the delete key. |
| Prelude is offline | |
| | #5 |
| Registered User Join Date: Mar 2003
Posts: 3,844
| >> if ((deposit + balance > 999999) && (deposit + balance < 0)) Can't have a number less than 0 and greater than 999999. Or just use a simple "else" as Prelude posted. gg |
| Codeplug is offline | |
| | #6 |
| Registered User Join Date: Jan 2004
Posts: 50
| Thanks for that but for some reason it just doesn't want to call the new balance up when i use a printf in the main function Code: case 3:
printf ("3. Show balance \n");
printf ("Your current balance = %f \n", &balance);
break;
this is really starting to do my nut in now |
| petedee is offline | |
| | #7 |
| Code Goddess Join Date: Sep 2001
Posts: 9,661
| >printf ("Your current balance = %f \n", &balance); You don't use the address operator when printing values. A type mismatch results in undefined behavior, but your compiler seems to prefer printing 0. Change the print to: Code: printf ("Your current balance = %f \n", balance);
__________________ My best code is written with the delete key. |
| Prelude is offline | |
| | #8 |
| Registered User Join Date: Jan 2004
Posts: 50
| omg, thanks thats what has being driving me mental for about 4 hours, i can't belive it was that obvious. Thanks fella! |
| petedee is offline | |
| | #9 |
| Registered User Join Date: Jan 2004
Posts: 50
| Wierdess ever error, new part of the code the call: Code: printf ("3. Show balance \n");
printf ("Your current balance = %.2f \n", balance);
break;
Code: float withdrawn (float balance)
{
float withdraw = 0 ;
int transtype;
printf ("Please enter how you wish to withdraw \n");
printf ("1. Cash \n");
printf ("2. Cheque\n");
scanf ("%d", &transtype);
if (transtype = 2)
{
printf ("Please enter how a value up to a maximum of 500 \n");
scanf ("%f", &withdraw);
if (withdraw > 500)
printf ("You are limited to a maximum of 500 \n");
else
{
printf ("%.2f has being withdrawn from your account \n", withdraw);
balance = balance - withdraw;
}
}
return (balance);
}
If you would like it I've uploaded the code i have so far http://petedeeuk.8bit.co.uk/code.html |
| petedee is offline | |
| | #10 |
| Code Goddess Join Date: Sep 2001
Posts: 9,661
| >if (transtype = 2) You compare two values with ==, you assign a value with =. This is a sneaky error, so you have to watch out for it. >does anyone know why i will always get an answer of 45 when i try to withdraw anything I don't have this problem, your code seems to work correctly. Can you give us a detailed session that causes the problem as well as what compiler you use?
__________________ My best code is written with the delete key. |
| Prelude is offline | |
| | #11 |
| Just Lurking Join Date: Oct 2002
Posts: 4,990
| Turn up the warning level on your compiler. Code: #include <stdio.h>
float deposited (float);
float withdawn (float);
/* [Info 752] local declarator 'withdawn(float)' not referenced */
int main()
{
int menu_choice = 0;
float deposit = 0;
float withdraw = 0;
int transactions;
float balance, new_balance;
while ( menu_choice != 4 )
{
printf ("Your current balance = %.2f \n", balance);
/* [Warning 530] Symbol 'balance' not initialized */
printf ("1. Deposit funds \n");
printf ("2. Withdraw funds \n");
printf ("3. Show balance \n");
printf ("4. Quit Program \n");
printf ("Please select what you wish to do (1-4): \n");
scanf ("%d", &menu_choice);
switch ( menu_choice )
{
case 1:
printf ("1. Deposit funds \n");
balance = deposited (balance);
transactions ++;
/* [Warning 530] Symbol 'transactions' not initialized */
break;
case 2:
printf ("2. Withdraw funds \n");
balance = withdrawn (balance);
/* [Info 718] Symbol 'withdrawn' undeclared, assumed to return int */
/* [Info 746] call to function 'withdrawn()' not made in the presence of a prototype */
break;
case 3:
printf ("3. Show balance \n");
printf ("Your current balance = %.2f \n", balance);
break;
case 4:
printf ("Exiting... \n");
break;
default:
printf("Please Enter a Choice from 1-4\n" );
break;
}
}
return 0;
/* [Warning 550] Symbol 'transactions' not accessed */
/* [Warning 529] Symbol 'deposit' not subsequently referenced */
/* [Warning 529] Symbol 'new_balance' not subsequently referenced */
/* [Warning 529] Symbol 'withdraw' not subsequently referenced */
}
float deposited (float balance)
{
float deposit;
printf ("Please enter how much you wish to deposit \n");
scanf ("%f", &deposit);
if ( (deposit + balance < 999999) || (deposit + balance > 0) )
{
balance = balance + deposit;
printf ("Thank you %.2f has been entered into your account\n", deposit);
}
else
printf ("I'm sorry but the number you have entered out of range/n");
return(balance);
}
float withdrawn (float balance)
/* [Error 18] Symbol 'withdrawn()' redeclared (basic, Arg. no. 1: promotion) conflicts with line 37 */
/* [Warning 516] 'Symbol withdrawn(float)' has arg. type conflict (arg. no. 1 -- float vs. double) with line 37 */
{
float withdraw = 0 ;
int transtype;
printf ("Please enter how you wish to withdraw \n");
printf ("1. Cash \n");
printf ("2. Cheque\n");
scanf ("%d", &transtype);
if ( transtype = 2 )
/* [Info 720] Boolean test of assignment */
/* [Warning 506] Constant value Boolean */
/* [Info 774] Boolean within 'if' always evaluates to True */
{
printf ("Please enter how a value up to a maximum of 500 \n");
scanf ("%f", &withdraw);
if ( withdraw > 500 )
printf ("You are limited to a maximum of 500 \n");
else
{
printf ("%.2f has being withdrawn from your account \n", withdraw);
balance = balance - withdraw;
}
}
return(balance);
}
__________________ 7. It is easier to write an incorrect program than understand a correct one. 40. There are two ways to write error-free programs; only the third one works.* |
| Dave_Sinkula is offline | |
| | #12 |
| Registered User Join Date: Jan 2004
Posts: 50
| Damn thats alot of errors I've uploaded the code again, (check the same link) its being compiled with Dev C++ Code: float withdrawn (float balance)
{
float cashwithdraw, chequewithdraw;
int transtype;
printf ("Please enter how you wish to withdraw \n");
printf ("1. Cash \n");
printf ("2. Cheque\n");
scanf ("%d", &transtype);
switch (transtype)
{
case 1:
{
printf ("How much would you like to withdraw? \n");
scanf ("%f", &cashwithdraw);
balance = balance - cashwithdraw;
printf ("%.2f has being withdrawn from your account \n", cashwithdraw);
break;
}
case 2:
{
printf ("How much would you like to withdraw? (maximum of 500) \n");
scanf ("%f", &chequewithdraw);
if (chequewithdraw > 500)
printf ("You are limited to a maximum of 500 \n");
else if (chequewithdraw < 0)
printf ("Please enter a positive number \n");
else
{
printf ("%.2f has being withdrawn from your account \n", chequewithdraw);
balance = balance - chequewithdraw;
}
break;
default:
printf("Please Enter a Choice from either 1 or 2\n" );
break;
}
}
return (balance);
}
When you withdraw money with cash or if you draw anything (cash or cheque) out twice the new balance is always 45, its an odd little error |
| petedee is offline | |
| | #13 |
| Code Goddess Join Date: Sep 2001
Posts: 9,661
| >Damn thats alot of errors Yet you haven't bothered to fix any of them. >float balance, new_balance; balance needs to have a value, 0 would be a good starting point. >int transactions; Ditto >float withdawn (float); This must be changed to Code: float withdrawn (float);
__________________ My best code is written with the delete key. |
| Prelude is offline | |
| | #14 |
| Registered User Join Date: Jan 2004
Posts: 50
| The reason i didn't fix any of them was that i only just got the post and haven't got round to doing it yet, gimme chance fella and Code: float withdrawn (float); |
| petedee is offline | |
| | #15 |
| Just Lurking Join Date: Oct 2002
Posts: 4,990
| >fella [edit] Code: float withdawn (float); float withdrawn (float balance) >its being compiled with Dev C++ You may want to add -Wall -ansi -pedantic to your compiler options.
__________________ 7. It is easier to write an incorrect program than understand a correct one. 40. There are two ways to write error-free programs; only the third one works.* |
| Dave_Sinkula is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Problem with input choice... Somethings wrong... really wrong.... | greenferoz | C++ Programming | 9 | 07-15-2004 03:30 PM |
| Debugging-Looking in the wrong places | JaWiB | A Brief History of Cprogramming.com | 1 | 11-03-2003 10:50 PM |
| Confused: What is wrong with void?? | Machewy | C++ Programming | 19 | 04-15-2003 12:40 PM |
| God | datainjector | A Brief History of Cprogramming.com | 746 | 12-22-2002 12:01 PM |
| Whats wrong? | Unregistered | C Programming | 6 | 07-14-2002 01:04 PM |