Thread: Small Problem with Function

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

    Small Problem with Function

    CHECKBOOK PROGRAM

    I have almost everything that i want to work, working right, except I have no idea how to accumulate the value of the Balance after a Deposit, Withdrawal, etc.. How do I put that in main? or does it go in main? Any Suggestions?

    Code:
    #include <iomanip.h>
    #include <stdlib.h>
    
    float Dep(float Bal, float Amount)
         { return (Bal+Amount);}
    
    float Check(float Bal, float Amount)
         { return (Bal-Amount);}
    
    float Charge(float Bal, float Amount)
         { return (Bal-Amount);}
    
    float Withdraw(float Bal, float Amount)
         { return (Bal-Amount);}
    
    
    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')
                  {break;}
    
           }
             cout<<"\n Your New Balance is: $"<<Bal;
          system("PAUSE");
          return 0;
    }
    Thanks in advance
    "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
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    You're returning a value, use it.

    >Dep(Bal, Amount);
    should be
    Bal = Dep ( Bal, Amount );

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

  3. #3
    Registered User ProgrammingDlux's Avatar
    Join Date
    Jan 2002
    Posts
    86
    With all the time i spent on trying to figure out my first functions program, i overlooked the fact that it doesn't calculate right. When I run the program, it doesn't seem to do the corresponding actions..example:I start the balance at 25 dollars. I deposit 10 dollars, then withraw 3 dollars and I come up with 15 dollars as the new balance. Can you see what I'm not seeing?


    Code:
    #include <iomanip.h>
    #include <stdlib.h>
    
    float Dep(float Bal, float Amount)
         { return (Bal+Amount);}
    
    float Check(float Bal, float Amount)
         { return (Bal-Amount);}
    
    float Charge(float Bal, float Amount)
         { return (Bal-Amount);}
    
    float Withdraw(float Bal, float Amount)
         { return (Bal-Amount);}
    
    
    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;
    
                  switch(Comm)
                  {case 'd': Bal=Dep(Bal, Amount);
                        cout<<"\n Enter amount in dollars and cents: "; cin>>Amount; break;
                   case 'c': Bal=Check(Bal,Amount);
                        cout<<"\n Enter amount in dollars and cents: "; cin>>Amount; break;
                   case 's': Bal=Charge(Bal,Amount);
                        cout<<"\n Enter amount in dollars and cents: "; cin>>Amount; break;
                   case 'w': Bal=Withdraw(Bal,Amount);
                        cout<<"\n Enter amount in dollars and cents: "; cin>>Amount; break;
                   default:
                        cout<<"\n You must enter one of the lower case commands ";continue;}
    
           }
             cout<<"\n Your New Balance is: $"<<Bal<<endl;
    
          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

  4. #4
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    That's because in your case statements, you're calling the fuction before you even have the value inputted.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Code:
    while(Comm != 'x')
           {       
                  switch(Comm)
                  {
                      //etc.
                   }
                    cout<<"\n Enter another command: "; 
                    cin>>Comm;
                    cout < < "\n Enter amount: "
                    cin >> Amount;
           }

  6. #6
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230
    Originally posted by ProgrammingDlux
    With all the time i spent on trying to figure out my first functions program, i overlooked the fact that it doesn't calculate right. When I run the program, it doesn't seem to do the corresponding actions..example:I start the balance at 25 dollars. I deposit 10 dollars, then withraw 3 dollars and I come up with 15 dollars as the new balance. Can you see what I'm not seeing?
    Hmm, guess youre not a programmer deluxe, eh? j/p around

  7. #7
    Registered User ProgrammingDlux's Avatar
    Join Date
    Jan 2002
    Posts
    86
    haha..good one biosx...lol. I was planning for the future when I came up with my username. Hopefully, the future is not too far a way. A little optimism can't hurt right?
    "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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  2. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  3. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 06:49 AM
  4. Replies: 5
    Last Post: 02-08-2003, 07:42 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM