Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. calculation error

calculation error

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
9 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kbury
    wrote on last edited by
    #1

    hello, i am having a problem with this code. I do not know what is wrong. I enter the begbal, the ourdep and the withdraw. But in the end when goes to get the final balance. The answer is always the same as the input of begin bal. Any help would be greatful.

    #include <stdio.h>

    #define SENTINEL 0

    float endbal,
    finbal;
    int date,
    outdep,
    withdraw;

    int main (void)

    {

    	  printf("BANK RECONCILIATION FOR: 10/21/98");
    
    	  printf("\\n\\nEnding balance on statement:");
    	  scanf ("%f", & endbal);
    
    
    	  printf("Enter Outstanding dep (or %d to quit)>", SENTINEL);
    	  scanf ("%d", & outdep);
    

    while (outdep!= SENTINEL)
    {
    finbal = finbal + outdep;
    printf("Enter outstanding dep (%d to quit)>", SENTINEL);
    scanf("%f", & outdep);

    }
    

    printf("Outstanding check/withdrawal (or %d to quit)>", SENTINEL);
    scanf ("%d", & withdraw);

    while (withdraw!=SENTINEL)
    {
    finbal = finbal - withdraw;
    printf("Enter withdrawal (%d to quit)>", SENTINEL);
    scanf("%f", & withdraw);

    }
    
    finbal = endbal + outdep - withdraw;
    
    printf("Your final balance in your checkbook should be $ %f" , finbal);
    
    return 0;
    

    }

    CPalliniC D P L 4 Replies Last reply
    0
    • K kbury

      hello, i am having a problem with this code. I do not know what is wrong. I enter the begbal, the ourdep and the withdraw. But in the end when goes to get the final balance. The answer is always the same as the input of begin bal. Any help would be greatful.

      #include <stdio.h>

      #define SENTINEL 0

      float endbal,
      finbal;
      int date,
      outdep,
      withdraw;

      int main (void)

      {

      	  printf("BANK RECONCILIATION FOR: 10/21/98");
      
      	  printf("\\n\\nEnding balance on statement:");
      	  scanf ("%f", & endbal);
      
      
      	  printf("Enter Outstanding dep (or %d to quit)>", SENTINEL);
      	  scanf ("%d", & outdep);
      

      while (outdep!= SENTINEL)
      {
      finbal = finbal + outdep;
      printf("Enter outstanding dep (%d to quit)>", SENTINEL);
      scanf("%f", & outdep);

      }
      

      printf("Outstanding check/withdrawal (or %d to quit)>", SENTINEL);
      scanf ("%d", & withdraw);

      while (withdraw!=SENTINEL)
      {
      finbal = finbal - withdraw;
      printf("Enter withdrawal (%d to quit)>", SENTINEL);
      scanf("%f", & withdraw);

      }
      
      finbal = endbal + outdep - withdraw;
      
      printf("Your final balance in your checkbook should be $ %f" , finbal);
      
      return 0;
      

      }

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      Change

      kbury wrote:

      printf("\n\nEnding balance on statement:"); scanf ("%f", & endbal);

      to

      printf("\n\nEnding balance on statement:");
      scanf ("%f", & finbal);

      and then remove the following line

      kbury wrote:

      finbal = endbal + outdep - withdraw;

      :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      In testa che avete, signor di Ceprano?

      K 1 Reply Last reply
      0
      • CPalliniC CPallini

        Change

        kbury wrote:

        printf("\n\nEnding balance on statement:"); scanf ("%f", & endbal);

        to

        printf("\n\nEnding balance on statement:");
        scanf ("%f", & finbal);

        and then remove the following line

        kbury wrote:

        finbal = endbal + outdep - withdraw;

        :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        K Offline
        K Offline
        kbury
        wrote on last edited by
        #3

        I made the changes and did the execute, this is what came out. still not the right answer. I do not know what else to do with this. I have tried so many things. BANK RECONCILIATION FOR: 10/21/98 Ending balance on statement:20 Enter outstanding dep (0 to quit)>10 Enter outstanding dep (0 to quit)>10 Enter outstanding dep (0 to quit)>10 Enter outstanding dep (0 to quit)>0 Enter outstanding dep (0 to quit)>0 Outstanding check/withdrawal (or 0 to quit)>10 Enter withdrawal (0 to quit)>10 Enter withdrawal (0 to quit)>0 Your final balance in your checkbook should be $ 2185232384.000000 "c:\cop2000\a5\lcc2\score.exe" Return code 0 Execution time 12.438 seconds Press any key to continue...

        CPalliniC 1 Reply Last reply
        0
        • K kbury

          I made the changes and did the execute, this is what came out. still not the right answer. I do not know what else to do with this. I have tried so many things. BANK RECONCILIATION FOR: 10/21/98 Ending balance on statement:20 Enter outstanding dep (0 to quit)>10 Enter outstanding dep (0 to quit)>10 Enter outstanding dep (0 to quit)>10 Enter outstanding dep (0 to quit)>0 Enter outstanding dep (0 to quit)>0 Outstanding check/withdrawal (or 0 to quit)>10 Enter withdrawal (0 to quit)>10 Enter withdrawal (0 to quit)>0 Your final balance in your checkbook should be $ 2185232384.000000 "c:\cop2000\a5\lcc2\score.exe" Return code 0 Execution time 12.438 seconds Press any key to continue...

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          Well, I didn't notice you made a bad mix of floats and ints. Try

          #define SENTINEL 0

          int endbal,
          finbal,
          date,
          outdep,
          withdraw;

          #include <stdio.h>
          int main (void)

          {

          printf("BANK RECONCILIATION FOR: 10/21/98");
          
          printf("\\n\\nEnding balance on statement:");
          scanf ("%d", & finbal);
          
          
          printf("Enter Outstanding dep (or %d to quit)>", SENTINEL);
          scanf ("%d", & outdep);
          
          while (outdep!= SENTINEL)
          {
          	finbal = finbal + outdep;
          	printf("Enter outstanding dep (%d to quit)>", SENTINEL);
          	scanf("%d", & outdep);
          
          }
          
          printf("Outstanding check/withdrawal (or %d to quit)>", SENTINEL);
          scanf ("%d", & withdraw);
          
          while (withdraw!=SENTINEL)
          {
          	finbal = finbal - withdraw;
          	printf("Enter withdrawal (%d to quit)>", SENTINEL);
          	scanf("%d", & withdraw);
          
          }
          
          
          
          printf("Your final balance in your checkbook should be $ %d" , finbal);
          
          return 0;
          

          }

          :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          In testa che avete, signor di Ceprano?

          1 Reply Last reply
          0
          • K kbury

            hello, i am having a problem with this code. I do not know what is wrong. I enter the begbal, the ourdep and the withdraw. But in the end when goes to get the final balance. The answer is always the same as the input of begin bal. Any help would be greatful.

            #include <stdio.h>

            #define SENTINEL 0

            float endbal,
            finbal;
            int date,
            outdep,
            withdraw;

            int main (void)

            {

            	  printf("BANK RECONCILIATION FOR: 10/21/98");
            
            	  printf("\\n\\nEnding balance on statement:");
            	  scanf ("%f", & endbal);
            
            
            	  printf("Enter Outstanding dep (or %d to quit)>", SENTINEL);
            	  scanf ("%d", & outdep);
            

            while (outdep!= SENTINEL)
            {
            finbal = finbal + outdep;
            printf("Enter outstanding dep (%d to quit)>", SENTINEL);
            scanf("%f", & outdep);

            }
            

            printf("Outstanding check/withdrawal (or %d to quit)>", SENTINEL);
            scanf ("%d", & withdraw);

            while (withdraw!=SENTINEL)
            {
            finbal = finbal - withdraw;
            printf("Enter withdrawal (%d to quit)>", SENTINEL);
            scanf("%f", & withdraw);

            }
            
            finbal = endbal + outdep - withdraw;
            
            printf("Your final balance in your checkbook should be $ %f" , finbal);
            
            return 0;
            

            }

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            kbury wrote:

            finbal = finbal + outdep;

            finbal is an r-value which has not been properly initialized.

            "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

            1 Reply Last reply
            0
            • K kbury

              hello, i am having a problem with this code. I do not know what is wrong. I enter the begbal, the ourdep and the withdraw. But in the end when goes to get the final balance. The answer is always the same as the input of begin bal. Any help would be greatful.

              #include <stdio.h>

              #define SENTINEL 0

              float endbal,
              finbal;
              int date,
              outdep,
              withdraw;

              int main (void)

              {

              	  printf("BANK RECONCILIATION FOR: 10/21/98");
              
              	  printf("\\n\\nEnding balance on statement:");
              	  scanf ("%f", & endbal);
              
              
              	  printf("Enter Outstanding dep (or %d to quit)>", SENTINEL);
              	  scanf ("%d", & outdep);
              

              while (outdep!= SENTINEL)
              {
              finbal = finbal + outdep;
              printf("Enter outstanding dep (%d to quit)>", SENTINEL);
              scanf("%f", & outdep);

              }
              

              printf("Outstanding check/withdrawal (or %d to quit)>", SENTINEL);
              scanf ("%d", & withdraw);

              while (withdraw!=SENTINEL)
              {
              finbal = finbal - withdraw;
              printf("Enter withdrawal (%d to quit)>", SENTINEL);
              scanf("%f", & withdraw);

              }
              
              finbal = endbal + outdep - withdraw;
              
              printf("Your final balance in your checkbook should be $ %f" , finbal);
              
              return 0;
              

              }

              P Offline
              P Offline
              Patcher32
              wrote on last edited by
              #6

              None of the values have been initialized and it always causes problem as the programs works on a random garbage value. 1. endbal is not initialized. Either set it to 0 or some other positive value at the start of your program. 2. finbal must be set to 0 at the start of main() function. You can initialize these at the time of declaration : float endbal = 0, finbal = 0; Also the statement finbal = endbal + outdep - withdraw; is wrong. You have already added deposit amount and subtracted withdrawn amount in the variable finbal. So all you need is endbal = endbal + finbal. Finally, you are displaying the wrong value. You should display endbal in the last printf statement. I hope this helps you.

              modified on Wednesday, October 14, 2009 6:59 PM

              1 Reply Last reply
              0
              • K kbury

                hello, i am having a problem with this code. I do not know what is wrong. I enter the begbal, the ourdep and the withdraw. But in the end when goes to get the final balance. The answer is always the same as the input of begin bal. Any help would be greatful.

                #include <stdio.h>

                #define SENTINEL 0

                float endbal,
                finbal;
                int date,
                outdep,
                withdraw;

                int main (void)

                {

                	  printf("BANK RECONCILIATION FOR: 10/21/98");
                
                	  printf("\\n\\nEnding balance on statement:");
                	  scanf ("%f", & endbal);
                
                
                	  printf("Enter Outstanding dep (or %d to quit)>", SENTINEL);
                	  scanf ("%d", & outdep);
                

                while (outdep!= SENTINEL)
                {
                finbal = finbal + outdep;
                printf("Enter outstanding dep (%d to quit)>", SENTINEL);
                scanf("%f", & outdep);

                }
                

                printf("Outstanding check/withdrawal (or %d to quit)>", SENTINEL);
                scanf ("%d", & withdraw);

                while (withdraw!=SENTINEL)
                {
                finbal = finbal - withdraw;
                printf("Enter withdrawal (%d to quit)>", SENTINEL);
                scanf("%f", & withdraw);

                }
                
                finbal = endbal + outdep - withdraw;
                
                printf("Your final balance in your checkbook should be $ %f" , finbal);
                
                return 0;
                

                }

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                sorry, but that is horrible code. you have amounts in int and float types, it might be wise and safe to be consistent. you have an int withdraw yet you fill it with a %f in scanf. you have two balance variables, you only need one. you don't initialize variables that need it. you have duplicated several lines, indicating you have chosen the wrong loop construct (use a do while instead!) and what happens when the input isn't a number (say an empty line, or some text)? :~

                Luc Pattyn


                I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


                Local announcement (Antwerp region): Lange Wapper? Neen!


                modified on Wednesday, October 14, 2009 7:50 PM

                K 1 Reply Last reply
                0
                • L Luc Pattyn

                  sorry, but that is horrible code. you have amounts in int and float types, it might be wise and safe to be consistent. you have an int withdraw yet you fill it with a %f in scanf. you have two balance variables, you only need one. you don't initialize variables that need it. you have duplicated several lines, indicating you have chosen the wrong loop construct (use a do while instead!) and what happens when the input isn't a number (say an empty line, or some text)? :~

                  Luc Pattyn


                  I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


                  Local announcement (Antwerp region): Lange Wapper? Neen!


                  modified on Wednesday, October 14, 2009 7:50 PM

                  K Offline
                  K Offline
                  kbury
                  wrote on last edited by
                  #8

                  I am still learning. I was not required to write a code for this but just a draw a flowchart. Any pointers would be appreciated.

                  L 1 Reply Last reply
                  0
                  • K kbury

                    I am still learning. I was not required to write a code for this but just a draw a flowchart. Any pointers would be appreciated.

                    L Offline
                    L Offline
                    Luc Pattyn
                    wrote on last edited by
                    #9

                    Hi, not tested:

                    // read a number, return it in delta; return TRUE if more to be handled, FALSE when done (with delta=0!)
                    float getNumber(char* prompt, float* delta) {
                    float number;
                    printf(prompt);
                    scanf("%f", number);
                    ... add whatever checking you consider important
                    *delta=number;
                    return number!=0;
                    }

                    int main(void) {
                    float balance, delta;
                    int more;
                    printf("BANK RECONCILIATION FOR: 10/21/98");
                    printf("\n\nEnding balance on statement:");
                    scanf("%f", &balance);
                    do {
                    more=getNumber("Enter Outstanding dep (or 0 to quit)", &delta);
                    balance+=delta;
                    while(more);
                    do {
                    more=getNumber("Outstanding check/withdrawal (or 0 to quit)");
                    balance-=delta;
                    while(more);
                    printf("Your final balance in your checkbook should be $ %f" , balance);
                    return 0;
                    }

                    :)

                    Luc Pattyn


                    I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


                    Local announcement (Antwerp region): Lange Wapper? Neen!


                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups