C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 01-06-2004, 12:20 PM   #1
Registered User
 
Join Date: Jan 2004
Posts: 50
whats wrong with this?

Hey everyone, spot the newbie

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;
after this i have a simple error check to make sure the data is within range:

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;
}
Now when this code comes back into the main part of the program the balance, i was wondering why this is and whats going to be the best way to fix it?

Apologies for the shoddily written code but I'm new to this.

Thanks for your help
petedee is offline   Reply With Quote
Old 01-06-2004, 12:44 PM   #2
Code Goddess
 
Prelude's Avatar
 
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:
when this code comes back into the main part of the program the balance I imagine your description of the problem was supposed to go here, i was wondering why this is and whats going to be the best way to fix it?
>balance = deposited (deposit);
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   Reply With Quote
Old 01-06-2004, 12:53 PM   #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;
The actual function:

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);
}
The problem is that it won't pass the result back into the main function meaning i can't use the value any further than the function, i need to pass the new_balance back.

Last edited by petedee; 01-06-2004 at 01:01 PM.
petedee is offline   Reply With Quote
Old 01-06-2004, 01:06 PM   #4
Code Goddess
 
Prelude's Avatar
 
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);
}
And you would call it like so:
Code:
balance = deposited (balance);
Now you can update balance if the deposit is within range, or refrain from updating balance otherwise with the same call to deposited. Which I imagine is what you wanted in the first place.
__________________
My best code is written with the delete key.
Prelude is offline   Reply With Quote
Old 01-06-2004, 01:09 PM   #5
Registered User
 
Codeplug's Avatar
 
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   Reply With Quote
Old 01-06-2004, 01:13 PM   #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;
still returns a value of 0, the value just doesn't seem to get passed into the main function

this is really starting to do my nut in now
petedee is offline   Reply With Quote
Old 01-06-2004, 01:21 PM   #7
Code Goddess
 
Prelude's Avatar
 
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);
and see what happens.
__________________
My best code is written with the delete key.
Prelude is offline   Reply With Quote
Old 01-06-2004, 01:24 PM   #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   Reply With Quote
Old 01-06-2004, 01:54 PM   #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;
the function:
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);
}
I know this is really cheeky but does anyone know why i will always get an answer of 45 when i try to withdraw anything, there seems to be logical reason for it

If you would like it I've uploaded the code i have so far http://petedeeuk.8bit.co.uk/code.html
petedee is offline   Reply With Quote
Old 01-06-2004, 02:20 PM   #10
Code Goddess
 
Prelude's Avatar
 
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   Reply With Quote
Old 01-06-2004, 02:22 PM   #11
Just Lurking
 
Dave_Sinkula's Avatar
 
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   Reply With Quote
Old 01-06-2004, 02:28 PM   #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);
}
New last section, replaced if's with a switch, however still known errors:

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   Reply With Quote
Old 01-06-2004, 02:36 PM   #13
Code Goddess
 
Prelude's Avatar
 
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   Reply With Quote
Old 01-06-2004, 02:40 PM   #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);
^^ isn't that what i put?
petedee is offline   Reply With Quote
Old 01-06-2004, 02:48 PM   #15
Just Lurking
 
Dave_Sinkula's Avatar
 
Join Date: Oct 2002
Posts: 4,990
>fella


[edit]
Code:
float withdawn (float);
float withdrawn (float balance)
[/edit]

>its being compiled with Dev C++

You may want to add -Wall -ansi -pedantic to your compiler options.
Attached Images
 
__________________
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   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 08:48 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22