Thread: Need help correcting problem with C program

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    2

    Need help correcting problem with C program

    I finished my program, but it has an error at the last three lines. I want to repair those errors so I can compile and test the program. If anyone can help me, I would appreciate it. my program is as follows:

    Code:
     #include <stdio.h>
    void main ( ) 
    {
    int Month; 
    double Total = 0.0; 
    double total_interest = 0.0; 
    int Count=0; 
    int amount; 
    int withdrawal; 
    int total;
    float interest;
    
    printf("For how many months do you have savings account? \n");
     scanf("%d", & Month); 
    while (Count++<Month) 
    { 
    printf("Enter the amount deposited for Month %d: ", Count); 
    scanf("%f", & amount); 
    total+=amount; 
    if ((amount%-1)==0); 
    printf("Amount should be subtracted from balance.");
     printf("Enter amount withdrawn from savings account? \n"); 
     scanf("%f", & amount);
     if ((withdrawal +=amount)&& (total-=amount)); 
     interest= total * 7.75 /12 ;
     (total_interest += interest);
    
     printf("The ending balance is %d , total");
     printf("Total amount of deposits %d , total"); 
     printf("Total amount of withdrawal %d, total");
     
     }

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    printf("The ending balance is %d", total);

    Note position of quotes.

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Please don't use void main(), it burns.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166
    As already noted by XSquared use:
    Code:
    int main()
    {
        ...
        return 0;
    }
    int amount;
    scanf("%f", &amount);

    What do you want? Integer or float?
    Code:
    if ((withdrawal +=amount)&& (total-=amount)); 
     interest= total * 7.75 /12 ;
     (total_interest += interest);
    Why are you putting a semicolon right after the if-expression? And what about the following two lines? Do you want them or just the first line to be executed only if ((withdrawal +=amount)&& (total-=amount)) is true?
    And why are you putting brackets around (total_interest += interest)?

    Code:
    if ((amount%-1)==0);
    And what is this supposed to do? Currently it does nothing, but Hammer already pointed that out in this thread of yours:
    http://cboard.cprogramming.com/showt...threadid=44375
    And where is the sense in using %-1?

    @scanf:
    You should empty stdin after each call to scanf to ensure that stdin is empty.
    You should check out this thread:
    http://cboard.cprogramming.com/showt...threadid=35056
    Look at Prelude's posting.
    Last edited by Sargnagel; 09-08-2003 at 07:18 AM.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >You should empty stdin after each call to scanf to ensure that stdin is empty.
    scanf is good for quick and dirty programs, but if you want good error checking and ease of maintenance/use then forgetting scanf exists would be a good idea. scanf has enough problems and inconsistencies with other input functions that using it causes more headaches than the slightly longer fgets/sscanf combination:
    Code:
    char line[LINELEN];
    float amount;
    
    if (fgets(line, sizeof line, stdin) == NULL) {
      perror("fgets(line)");
      exit(EXIT_FAILURE);
    }
    if (sscanf(line, "%f", &amount) != 1) {
      perror("sscanf(line)");
      exit(EXIT_FAILURE);
    }
    /* Use amount */
    Also note that exit(EXIT_FAILURE) should be read as "insert your error recovery code here".
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multi Thread Program Problem
    By ZNez in forum C Programming
    Replies: 1
    Last Post: 01-03-2009, 11:10 AM
  2. Program Termination Problem
    By dacbo in forum C Programming
    Replies: 3
    Last Post: 01-23-2006, 02:34 AM
  3. Replies: 20
    Last Post: 06-12-2005, 11:53 PM
  4. Console Program Problem
    By Breach23 in forum C++ Programming
    Replies: 3
    Last Post: 10-19-2001, 12:35 AM