Simple Program not working right (in C)

This is a discussion on Simple Program not working right (in C) within the C Programming forums, part of the General Programming Boards category; I just made this C program in Codewarrior for mac using the "PPC console C App:" #include <stdio.h> #include <stdlib.h> ...

  1. #1
    DruzeTito
    Guest

    Angry Simple Program not working right (in C)

    I just made this C program in Codewarrior for mac using the "PPC console C App:"

    #include <stdio.h>
    #include <stdlib.h>

    int main()
    {

    float fuel_flow;
    float total_fuel;
    float TAS;

    char TAS1[6];
    char total_fuel1[16];
    char fuel_flow1[12];
    char yn='y';

    while(yn=='y')
    {

    printf("What is your fuel flow in pounds? ");
    gets(fuel_flow1);
    fuel_flow=atoi(fuel_flow1);

    printf("What is your total fuel availible in pounds? ");
    gets(total_fuel1);
    total_fuel=atoi(total_fuel1);

    printf("What is your true airspeed in knots? ");
    gets(TAS1);
    TAS=atoi(TAS1);

    printf("\nEndurance: %.2f hrs\n",total_fuel/fuel_flow);
    printf("Range: %.1f nm\n",total_fuel/fuel_flow*TAS);
    printf("\nCalculate again? y/n ");
    yn=getchar();

    }
    printf("Done!");
    return 0;

    }

    Here is my problem. The first run of the program is good. After the program asks "Calculate Again?" , i type in "y" and then on the next line it prints the first two printfs; printf("What is your fuel flow in pounds? "); and printf("What is your total fuel availible in pounds? "); on the same line and everything gets messed up after that until i re run the program. Could there be something wrong in my while loop? It would be great if anyone can help. thank you.

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    63
    Hey man..
    no problem..I think I fixed the problem..you had to FLUSH at the end of the loop..the revised code is here now!
    Btw..you had a typo.."availible" should be spelt "available"..I fixed that too..
    If you want a better understanding of FLUSH reply back and I'll post a previous msg..Okee..
    A

    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    #define FLUSH while(getchar() != '\n')
    
    int main() 
    { 
    
    float fuel_flow; 
    float total_fuel; 
    float TAS; 
    
    char TAS1[6]; 
    char total_fuel1[16]; 
    char fuel_flow1[12]; 
    char yn='y'; 
    
    while(yn=='y') 
    { 
    
    printf("What is your fuel flow in pounds? "); 
    gets(fuel_flow1); 
    fuel_flow=(float) atoi(fuel_flow1); 
    
    printf("What is your total fuel available in pounds? "); 
    gets(total_fuel1); 
    total_fuel=(float) atoi(total_fuel1); 
    
    printf("What is your true airspeed in knots? "); 
    gets(TAS1); 
    TAS= (float) atoi(TAS1); 
    
    printf("\nEndurance: %.2f hrs\n",total_fuel/fuel_flow); 
    printf("Range: %.1f nm\n",total_fuel/fuel_flow*TAS); 
    printf("\nCalculate again? y/n "); 
    yn=getchar(); 
    
    FLUSH;
    
    } 
    printf("Done!"); 
    return 0; 
    
    }

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    71
    FLUSH ur buffer stream

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    85
    Your program's problem is similar to so many programs posted in
    the past. Do a search if ya like.

    Here is my fix to the problem:
    int main()
    {

    float fuel_flow;
    float total_fuel;
    float TAS;

    char TAS1[6];
    char total_fuel1[16];
    char fuel_flow1[12];
    char yn; /* declare a variable */

    do
    {

    printf("What is your fuel flow in pounds? ");
    gets(fuel_flow1);
    fuel_flow=atoi(fuel_flow1);

    printf("What is your total fuel availible in pounds? ");
    gets(total_fuel1);
    total_fuel=atoi(total_fuel1);

    printf("What is your true airspeed in knots? ");
    gets(TAS1);
    TAS=atoi(TAS1);

    printf("\nEndurance: %.2f hrs\n",total_fuel/fuel_flow);
    printf("Range: %.1f nm\n",total_fuel/fuel_flow*TAS);
    printf("\nCalculate again? y/n ");
    yn=getch(); /*if you use getchar(), you need to use fflush() */

    } while (yn == 'y' || yn == 'Y')
    printf("Done!");
    return 0;

    }
    Let me know if it does'nt work.
    DV007

  5. #5
    DruzeTito
    Guest

    Thumbs up

    WOW! You guys are the greatest! I didn't expect help this quick. Any way it works yes! I really didn't know I had to use the flush stuff. I am new to progamming in C, of course.. I am using my C for dummies book :-). To dv007: I dont think that the getch(); will work on a Mac with codewarrior. To aspand: yes I would like to know what the flush is used for! Thanks.

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    63
    Hey DruzTito,
    This is the link to a msg I posted asking help about FLUSH..pay close attention to Prelude and esp. Hammer's comments..should be helpful! I am glad the program worked just fine for you now
    Aspan

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Blackjack Program
    By saber1357 in forum C Programming
    Replies: 1
    Last Post: 03-28-2009, 04:19 PM
  2. Replies: 10
    Last Post: 09-08-2008, 12:38 AM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. simple frontend program problem
    By gandalf_bar in forum Linux Programming
    Replies: 16
    Last Post: 04-22-2004, 07:33 AM
  5. unable to get simple program working
    By toom in forum Linux Programming
    Replies: 1
    Last Post: 10-11-2003, 06:05 AM

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