Thread: Hai to all

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    33

    Hai to all

    Dear All,
    Today i was installed Dev-C++ compiler in my PC.. i can compile successfully all codes successfully.. only it showing compilaiton is successfull
    when i will try to see the output using RUN option it gives error as follows
    Code:
     
    #include<stdio.h>
    void main()
    {
      int a,b,sum;
      printf("enter two numbers\n");
      scanf("&#37;d %d",&a,&b);
      sum=a+b;
      printf("sum=%d", sum);
    }
    it will accept two values like a and b successfully but not displaying result.. it seems to be simple qustion but plese suggest me i am new to programming concepts.. i am thankful to all of you.
    Last edited by Salem; 08-16-2007 at 08:43 AM. Reason: Code tags around the code only.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Try making the line to print something like
    Code:
    printf("sum=%d\n", d);
    With the newline, it should print the result - if there's no newline, printf may well end up being buffered, and thus not showing up before the application finishes (and being lost completely).

    An alternative problem is if you are running this from a IDE where the "application" window disappears before you have time to read the text - in which case you need to add something to stop it from "going away" before you have seen the end of it. adding
    Code:
    system("pause");
    at the end of the code would do that.

    Also: "main" is a function that returns "int", so you should really not make it a void function - and you should return something at the end of the function (customarily zero to indicate success - other return values customarily indicate failure).

    --
    Mats

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    main returns an int.

    Quote Originally Posted by Shidlingayya
    when i will try to see the output using RUN option it gives error as follows
    What error's? I see no error's mentioned by you. Perhaps the code is running and exits before you have a chance to see the output because the program has ended?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Dont use system() commands if you can help it

    getchar() is more portable
    Double Helix STL

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by swgh View Post
    Dont use system() commands if you can help it

    getchar() is more portable
    It is indeed so - the only problem with this approach is that it also involves explaining how to get rid of the remaining newline in the buffer after scanf(), and for two reasons, I suggested the "system(pause)" solution as a "better" alternative:
    1. I think the poster is quite new to programming - and adding a "flush the input buffer" would mean adding some relatively complicated code to the application.
    2. I'm lazy - just simpler to suggest the "shorter to type" answer. At least I'm honest about it ;-)

    --
    Mats

  6. #6
    Registered User
    Join Date
    Aug 2007
    Posts
    34
    use getch(); in the program
    Code:
    #include<stdio.h>
    void main()
    {
      int a,b,sum;
      printf("enter two numbers\n");
      scanf("&#37;d %d",&a,&b);
      sum=a+b;
      printf("sum=%d", sum);
      getch();
    }
    the use is to see the result in run time
    in your program you can see the result by pressing alt+F5 after compiling the program

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    main() should return an int, and by convention, should probably return a 0 to indicate all went well.

    getch() is unportable and requires conio.h or something else.

  8. #8
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    getch() is unportable and requires conio.h or something else.
    Totally agree with that. But I have noticed on some compilers ( for istance the one Dev uses ) you can include getch() without conio.h.

    Perhaps the compiler vendors bundled getch() with in definition of stdio.h.

    I am no expert but thats what I think may of happened. as on VC++ you do need to include conio.h to use getch()
    Last edited by swgh; 08-16-2007 at 12:54 PM. Reason: Brace wrong way around
    Double Helix STL

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by matsp View Post
    With the newline, it should print the result - if there's no newline, printf may well end up being buffered, and thus not showing up before the application finishes (and being lost completely).
    files are closed and flushed on program shutdown.

  10. #10
    Registered User
    Join Date
    Aug 2007
    Posts
    33

    Thanks for reply i got output

    Quote Originally Posted by matsp View Post
    Try making the line to print something like
    Code:
    printf("sum=%d\n", d);
    With the newline, it should print the result - if there's no newline, printf may well end up being buffered, and thus not showing up before the application finishes (and being lost completely).

    An alternative problem is if you are running this from a IDE where the "application" window disappears before you have time to read the text - in which case you need to add something to stop it from "going away" before you have seen the end of it. adding
    Code:
    system("pause");
    at the end of the code would do that.

    Also: "main" is a function that returns "int", so you should really not make it a void function - and you should return something at the end of the function (customarily zero to indicate success - other return values customarily indicate failure).

    --
    Mats

    Thanks for ur reply i got output

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. new baby doubt
    By cnu_sree in forum C Programming
    Replies: 7
    Last Post: 05-10-2008, 05:14 AM
  2. hai guys help on 2-dim arrays
    By zerlok in forum C++ Programming
    Replies: 4
    Last Post: 03-05-2008, 12:58 PM
  3. Hai where i will get good.......
    By Shidlingayya in forum C Programming
    Replies: 3
    Last Post: 08-14-2007, 03:39 AM
  4. Hai Friends i am getting one error
    By Shidlingayya in forum C Programming
    Replies: 16
    Last Post: 08-10-2007, 03:46 AM
  5. Hai Everybody !VC dll in VB ?
    By samudrala_99 in forum Windows Programming
    Replies: 2
    Last Post: 06-20-2003, 07:22 AM