where's the FUN in Functions?

This is a discussion on where's the FUN in Functions? within the C++ Programming forums, part of the General Programming Boards category; I can't figure out how to run this Check Balancing Program. It has no compile time errors, but every time ...

  1. #1
    Registered User ProgrammingDlux's Avatar
    Join Date
    Jan 2002
    Posts
    86

    where's the FUN in Functions?

    I can't figure out how to run this Check Balancing Program. It has no compile time errors, but every time I wanna see the end balance..it outputs the same balance I put in to start. Are my function bodys right? Can anyone help?
    Code:
    #include <iomanip.h>
    #include <stdlib.h>
    
    float Dep(float Bal, float Amount)
         { Bal=Bal+Amount;
           return 0;}
    
    float Check(float Bal, float Amount)
         { Bal=Bal-Amount;
           return 0;}
    
    float Charge(float Bal, float Amount)
         { Bal=Bal-Amount;
           return 0;}
    
    float Withdraw(float Bal, float Amount)
         { Bal=Bal-Amount;
           return 0;}
    
    
    main()
     {  float Bal, Amount; char Comm;
    
        cout<<"\n BALANCING CHECKBOOK PROGRAM ";
        cout<<"\n --------------------------- ";
        cout<<"\n\n My Account ";
        cout<<"\n Enter Starting Balance : "; cin>>Bal;
        cout<<"\n Command Options: "
            <<"\n d- deposit"
            <<"\n c- check "
            <<"\n s- service charge "
            <<"\n w- withdrawal "
            <<"\n x- exit "<<endl;
        cout<<"\n Enter command: "; cin>>Comm;
        cout<<"\n Enter amount in dollars and cents: "; cin>>Amount;
         while(Comm != 'x')
           {       cout<<"\n Enter another command: "; cin>>Comm;
    
                  if(Comm == 'd')
                  {Dep(Bal, Amount);cout<<"\n Enter amount in dollars and cents: "; cin>>Amount;}
             else if(Comm == 'c')
                  {Check(Bal,Amount);cout<<"\n Enter amount in dollars and cents: "; cin>>Amount;}
             else if(Comm == 's')
                  {Charge(Bal,Amount);cout<<"\n Enter amount in dollars and cents: "; cin>>Amount;}
             else if(Comm == 'w')
                  {Withdraw(Bal,Amount);cout<<"\n Enter amount in dollars and cents: "; cin>>Amount;}
             else if(Comm == 'x')
                  {cout<<"\n Your New Balance is: $"<<Bal; break;}
    
           }
    
          system("PAUSE");
          return 0;
    }
    "For in fact what is a man in Nature? A Nothing in comparison with the Infinite, an All in comparison with the Nothing, a mean between nothing and everything"- Blaise Pascal

  2. #2
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    i'll preface with my standard: i don't know much. but your functions aren't returning the float you defined them to. is that it?

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Two things. You pass the values by value to your functions yet you do not return the result of the operation nor try to assign the return value in your main function. So you take the values, change them, discard the result, and print the original value that you entered.

    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User ProgrammingDlux's Avatar
    Join Date
    Jan 2002
    Posts
    86
    How would I use that in my program...this is the first time I'm using Functions..gimme just one little example ..pleaaaaase!
    "For in fact what is a man in Nature? A Nothing in comparison with the Infinite, an All in comparison with the Nothing, a mean between nothing and everything"- Blaise Pascal

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Keep the code the same, but change the parameters to your functions to references so that you effect the values passed to the function instead of copies of those values:
    Code:
    void Dep(float &Bal, float &Amount)
    { 
      Bal=Bal+Amount;
    }
    
    void Check(float &Bal, float &Amount)
    { 
      Bal=Bal-Amount;
    }
    
    void Charge(float &Bal, float &Amount)
    { 
      Bal=Bal-Amount;
    }
    
    void Withdraw(float &Bal, float &Amount)
    { 
      Bal=Bal-Amount;
    }
    -Prelude
    My best code is written with the delete key.

  6. #6
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    could the same be achieved if he returned bal?

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >could the same be achieved if he returned bal?
    Yes
    Code:
    float Check ( float Bal, float Amount )
    { 
      return ( Bal - Amount );
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 02:35 AM
  2. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  3. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM
  4. functions - please help!!!!
    By linkies in forum C Programming
    Replies: 1
    Last Post: 08-21-2002, 07:53 AM

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