Thread: It is not Runnig

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    7

    Unhappy It is not Runnig

    Hi:

    I did cut and paste this example from a tuterial and I was hoping that it will work. But my compiler, miracle C, did not even give me an error message. it just did not let it run. Will appriciate your help.

    Sam


    main()
    {
    float cost,tax,luxury,total;
    luxury=0.0;

    printf("Enter the cost of the item: ");
    scanf("%f", &cost);

    tax=cost*0.06;

    if(cost>40000.0)
    luxury=cost*0.005;
    total=cost+tax+luxury;
    printf("the total cost is %0.2f",total);
    }

  2. #2
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    Don't use scanf. What you can do is use fgets to store the input into a char array and then turn this into a float using atof.
    Code:
    int main()
    {
    float cost, tax, luxury, total;
    char Input[128];
    luxury = 0;
    
    printf("Enter the cost of the item:");
    fgets(Input, 128, stdin);
    cost = atof(Input);
    
    tax=cost*0.06; 
    
    if(cost>40000.0) 
    luxury=cost*0.005; 
    total=cost+tax+luxury; 
    printf("the total cost is %0.2f",total); 
    
    return 0;
    }
    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Seems OK to me

    The only minor suggestion would be
    printf("the total cost is %0.2f",total);

    to
    printf("the total cost is %0.2f\n",total);

  4. #4
    Cicero
    Guest

    works fine

    i got some warnings, but it works good. heres the program with the problems fixed.
    PHP Code:
    #include    <stdio.h>

    void main() 
        { 
            
    float cost,tax,luxury,total
            
    luxury=0.0

            
    printf("Enter the cost of the item: "); 
            
    scanf("%f", &cost); 

            
    tax=cost*0.06f

            if(
    cost>40000.0)
                {
                    
    luxury=cost*0.005f
                }
            
    total=cost+tax+luxury
            
    printf("the total cost is %0.2f",total); 
        } 

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Was OK up to the void main bit
    main()

    or the more preferred
    int main()

    Is the only way to go

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    If you feel you must use scanf(), check it's return code to ensure it did it's job before you use the variables.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Runnig A Clock??????????
    By database_528 in forum C Programming
    Replies: 1
    Last Post: 02-02-2008, 06:44 AM