[Help] What's wrong in my program?

This is a discussion on [Help] What's wrong in my program? within the C Programming forums, part of the General Programming Boards category; Code: #include<stdio.h> #include<conio.h> void Value(float principal, float rate, int years, char choose) { int i; if(choose == 'f') { printf("The ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    16

    Question [Help] What's wrong in my program?

    Code:
    #include<stdio.h>
    #include<conio.h>
    
    void Value(float principal, float rate, int years, char choose)
    {
    int i;
    if(choose == 'f')
              {
              printf("The present amount: $ %.2f\n",principal );
              for(i=0;i<years;i++ )
              principal = principal*(1+rate);
              printf("The future value: $ %.2f",principal );
              }
    else if (choose == 'p')
            {
            printf("The future amount: $ %.2f\n",principal );
            for(i=0;i<years;i++ )
            principal = principal/(1+rate);
            printf("The present value: $ %.2f",principal );
            }
    }
    
    main(){
    
    float pr, rt;
    int ys;
    char ch;
    printf("Investment Calculator\n");
    printf("=====================\n");
    printf("\nPrincipal:");
    scanf("%f",&pr);
    printf("Annual Rate:");
    scanf("%f",&rt);
    printf("No of years:");
    scanf("%d",&ys);
    printf("Future value (f) or present value (p):");
    scanf("%c",&ch);
    Value(pr, rt, ys, ch);
    getch();
    }
    That is my program, why my program only run till line Future value (f) or present value (p): ? What's wrong in my program?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Code:
    scanf("%c",&ch);
    The problem is this scanf call and the use of the %c format specifier. Prior to this you entered the number of years (followed by the enter key). The years were stored into your variable ys but the newline character was left in the input buffer/stream. Coupled with the %c format specifier, the scanf call above read and stored the newline character (which was the first one available from the buffer as it was left over from the previous input operation) into ch instead of the f/p you were expecting.

    There are several ways to deal with this, probably the easiest is to put a space in front of the %c format specifier which will instruct the scanf call to disregard leading whitespace (i.e. newlines, etc...).
    Code:
    scanf(" %c",&ch);
    Last edited by hk_mp5kpdw; 05-19-2009 at 01:25 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    16
    thk you, i have finished my program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 02:42 AM
  2. Replies: 5
    Last Post: 01-13-2007, 02:14 AM
  3. What is wrong with my code? My first program......
    By coreyt1111 in forum C++ Programming
    Replies: 11
    Last Post: 11-14-2006, 02:03 PM
  4. Whats wrong with my program?
    By Ruflano in forum C++ Programming
    Replies: 5
    Last Post: 02-21-2002, 05:09 PM
  5. what's wrong with my newbie program??
    By insoolated in forum C++ Programming
    Replies: 1
    Last Post: 09-14-2001, 09:49 PM

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