Thread: Dev C++

  1. #1
    Registered User kw42chan's Avatar
    Join Date
    Jan 2013
    Location
    Hong Kong, China
    Posts
    18

    Dev C++ in compiling in C and C++

    Dears

    I use Dev C++ in compiling, want to write C. i found there is some error. when i rewrite them, i find it regard me as wrong in C.

    Original code.
    Code:
    Main ()
    {
    int revenue;
    int price;
    int quantity;
    printf(“Please input the price”)
    scanf(“%d”, &price)
    printf(“Please input the quantity”)
    scanf(“%d”, &quantity)
    revenue = price* quantity
    printf(“Total revenue is”, &revenue)
    }
    Code as correct in Dev C++
    Code:
    # include <stdio.h>int Main ()
    {
     int revenue;
     int price;
     int quantity;
     printf("Please input the price %c");
     scanf("%d", &price);
     printf("Please input the quantity %c");
     scanf("%d", &quantity);
     revenue = price*quantity;
     printf("Total revenue is %c", &revenue);
    }
    PS I m newbie
    Last edited by kw42chan; 01-04-2013 at 09:46 AM. Reason: More Clear topic illustration

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    In the following snippet:
    Code:
    printf("Total revenue is %c", &revenue);
    You have a couple of problems. First the "%c" specifier is used when printing a single character, not an int. Second you don't need to use the ampersand since you want to print the variables value, not the address.

    Jim

  3. #3
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    Code:
    printf("Please input the quantity %c");
    Why did you decide to put a %c here?

    int main is case sensitive. it should also be int main(void).

    int main(void) should not be on the same line as #include <stdio.h>, this will cause problems. not sure if this is the way it is in your code though or it was just a copying problem.

    Format specifier for your last printf is wrong. Also you don't need to use the & operator when supplying (most) arguments to printf. view the documentation for printf to correct this one.

    int main( void ) must 'return 0;' at the end.

    Good try, despite the few errors it looks like a great program.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    The first version has been through something like Microsoft Word that turns the ordinary double-quotes into "smart quotes", which compilers do not understand. You can tell this easily from the lack of string colouring by the syntax hilighter.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    also my compilier been yelling at me for
    Code:
    int main(void)
    says "warning #2117: old-style function definition for 'main'"
    so now i use
    Code:
    int main ( int argc, char *argv[] )
    //edit//
    NVM, only yells at me when i dont use the void...i think....
    Last edited by Crossfire; 01-04-2013 at 01:53 PM. Reason: NVM!!!

  6. #6
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    One piece you are still missing, when you define a function with a return value...you have to return a value in it.

    Code:
    int main(void)
    {
    ...
    }
    Your compiler should warn about this also..example:
    Code:
    $ gcc -Wall test.c 
    test.c: In function ‘main’:
    test.c:3:1: warning: control reaches end of non-void function
    It's stating that you declared "main" as a function returning "int". So you need to return an "int" value in your "main" function.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Crossfire
    only yells at me when i dont use the void...i think
    Probably, otherwise your compiler has a bug since the version with void is not old-style.

    Quote Originally Posted by nonpuz
    Your compiler should warn about this also
    It should not when compiling with respect to C99 or later because as a special case, a missing return statement in the main function is as if there was a return 0; at the end.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Jan 2013
    Posts
    24
    no he has main as Main in both
    no int in front of the first file.

    before the last }
    you may want to put getchar();
    and put a return();
    if you actually want to see your programs outputs in dos. if not the window will just close to fast to read.

    also for your string output you may want to use a space at the end so when you enter a number as input it wont look strange like so


    Code:
    printf("Please input the price: ");
     scanf("%d", &price);
     printf("\nPlease input the quantity:");
     scanf("%d", &quantity);
    instead of
    Code:
       printf("Please input the price %c");
       scanf("%d", &price);
       printf("Please input the quantity %c");
       scanf("%d", &quantity);
    
    Last edited by jamesedmonds; 01-05-2013 at 11:04 AM.

  9. #9
    Registered User
    Join Date
    Jan 2013
    Posts
    24
    Code:
    //
    //
    include<stdio.h>
    int main(void)
    {
       
    
    
       getchar()
       return 0;
    }
    just use this as your template while learning stuff. just fill in main.

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by jamesedmonds View Post
    Code:
    //
    //
    include<stdio.h>
    int main(void)
    {
       
    
    
       getchar()
       return 0;
    }
    just use this as your template while learning stuff. just fill in main.
    Forgetting the # in front of the include will not help the newbie.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed