Thread: simple c program

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    14

    simple c program

    plz help to solve the simple c program,

    Code:
    #include <stdlib.h>
    
    int main()
    {
       float cp,sp,p,l;
       printf("Enter cost Price and selling Price:");
       scanf("%f%f",&cp,&sp");
       p=sp-cp;
       l=cp-sp;
    
    
       if (p>0)
           printf("The seller has made a profit of Rs.%f",p);
    
    
       if (l>0)
            printf("The seller has made a loss of Rs.%f",l);
    
    
        if (p==0)
            printf("There is no loss no profit");
    
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2011
    Posts
    14
    where cp= cost price , sp= selling price , l= loss , p = profit ,,

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    In the future, you need to explain what your problem is. Does the code compile? Is there an error or warning message you can't fix? Copy-paste the error message(s) exactly so we can help. If the program compiles, does it crash when you run it? Do you get no output? Do you get the wrong output? Tell us the input you provided, the output you expect and the output you actually get.

    In this case, you have a simple syntax error:
    Code:
    $ gcc -Wall -g -std=c99  sale.c   -o sale
    sale.c: In function ‘main’:
    sale.c:6: warning: implicit declaration of function ‘printf’
    sale.c:6: warning: incompatible implicit declaration of built-in function ‘printf’
    sale.c:7: warning: implicit declaration of function ‘scanf’
    sale.c:7: warning: incompatible implicit declaration of built-in function ‘scanf’
    sale.c:7:25: warning: missing terminating " character
    sale.c:7: error: missing terminating " character
    sale.c:8: error: expected ‘)’ before ‘p’
    sale.c:25: error: expected ‘;’ before ‘}’ token
    sale.c:5: warning: unused variable ‘l’
    sale.c:5: warning: unused variable ‘p’
    make: *** [sale] Error 1
    1. The "implicit declaration" messages mean you need to #include <stdio.h>.
    2. The "missing termination" message means you have an erroneous " floating around on line 7. Remove it. This error would be much easier to see if you had an editor with syntax highlighting. There are dozens of free ones available, so look into it. It's a simple but highly effective tool.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > where cp= cost price , sp= selling price , l= loss , p = profit ,,
    Here's an idea - use those names in the code, instead of being all cryptic and then having to explain yourself.

    I mean
    profit = salePrice - costPrice;
    is a hell of a lot more readable, and needs no afterthough comment, to understand what is going on.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    14
    thank you , next time I will do it

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    14
    error was in the 7th line bcoz of extra " after sp.
    thanks
    Last edited by psaikia; 11-09-2011 at 01:27 PM.

  7. #7
    Registered User oluwasegun's Avatar
    Join Date
    Nov 2011
    Location
    lagos nigeria
    Posts
    3

    Thumbs up

    your code has syntax error remove the double quote in your code in line 7
    Last edited by oluwasegun; 11-09-2011 at 01:34 PM.

  8. #8
    Registered User
    Join Date
    Nov 2011
    Posts
    14
    now its fine , it was syntax error,
    in the 7th line,

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple program, simple problem
    By KAUFMANN in forum C Programming
    Replies: 5
    Last Post: 02-16-2011, 01:16 PM
  2. simple program, simple error? HELP!
    By colonelhogan44 in forum C Programming
    Replies: 4
    Last Post: 03-21-2009, 11:21 AM
  3. Simple program...simple problem?
    By deadherorising in forum C Programming
    Replies: 2
    Last Post: 03-12-2009, 08:37 PM
  4. Simple program, not so simple problem
    By nolsen in forum C++ Programming
    Replies: 2
    Last Post: 01-18-2008, 10:28 AM
  5. Need help with simple, simple program.
    By LightsOut06 in forum C Programming
    Replies: 5
    Last Post: 09-01-2005, 08:31 PM