Like Tree3Likes
  • 1 Post By iMalc
  • 2 Post By stahta01

Dev C++

This is a discussion on Dev C++ within the C Programming forums, part of the General Programming Boards category; Dears I use Dev C++ in compiling, want to write C. i found there is some error. when i rewrite ...

  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
    2,919
    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,308
    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.
    stahta01 likes this.
    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
    289
    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
    520
    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
    22,302
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    2,770
    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.
    Matticus and laserlight like this.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

Popular pages Recent additions subscribe to a feed

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