Basic Programm giving Error

This is a discussion on Basic Programm giving Error within the C Programming forums, part of the General Programming Boards category; HeLLo. i M new & have written just a simple programm, but it is giving error. it compiles perfectly without ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    15

    Basic Programm giving Error

    HeLLo.
    i M new & have written just a simple programm, but it is giving error.
    it compiles perfectly without errors but when i run it, it doesnt give the value as desired.

    PlS. Help
    i M attaching the C file which i have problem with

    Code:
    /*Ramesh's Basic Salary is input through Keyboard.
    His dearness allowance is 40% of basic salary, & 
    house rent is 20% of his basic salary.
    WAP to calculate his Gross Salary*/
    #include <stdio.h>
    #include <conio.h>
    int main ()
    {
    	float BS, GS, DA, HR;
    	clrscr ();
    	printf ("\nEnter your Basic Salary\n");
    	scanf ("%f",&BS);
    	DA = 40/100*BS;
    	HR = 20/100*BS;
    	printf ("\nYour Dearness Allowence is %f \n", DA);
    	printf ("\nYour House Rent Allowance is %f \n", HR);
    	GS = BS+HR+DA;
    	printf ("\nYour GROSS SALARY is %f\n", GS);
    	getch();
    	return 0;
    }
    Which compiler to use ???
    i M using Turbo C++, but having gr8 troubles with it.
    'coz i m using Windows 7 & in that dos doesnt support fullscreen.
    So its a big problem.

    PlS. tell me a good compiler
    Last edited by dharmil007; 05-26-2010 at 08:55 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,158
    Hint: with integer division, 40/100 = 0.
    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

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Also, use int main(void) instead of int main().
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    15
    Quote Originally Posted by claudiu View Post
    Also, use int main(void) instead of int main().


    y wats the difference between both ??
    & wats difference btween

    void main ()
    &
    int main ()

    in coll they tell us to write void main ()
    while in books & online tuts they tel us to write int main ()

    i dont know wats the difference between both

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    15
    Quote Originally Posted by laserlight View Post
    Hint: with integer division, 40/100 = 0.


    Actually the answer is 0.4
    & so due to this i have given it in float so it should support float points.
    But it is giving 0.
    Can u pls. tell me y ??

  6. #6
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    No. It is not. You're dividing two integer constants, and the result of that expression is an integer -- 0. This zero is then assigned to the float type variable.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,158
    Quote Originally Posted by dharmil007
    y wats the difference between both ??
    For a function declaration that is not also a definition, the void in the parameter list specifies that the function takes no arguments, but if you put nothing at all for the parameter list, then no information about the parameter list is supplied. In this case, you have a function definition, so whether you put the void in or not, it specifies that the function takes no arguments. However, you might as well be consistent, especially since a function definition is also a function declaration.

    Quote Originally Posted by dharmil007
    & wats difference btween

    void main ()
    &
    int main ()

    in coll they tell us to write void main ()
    while in books & online tuts they tel us to write int main ()

    i dont know wats the difference between both
    Read void main(void) - the Wrong Thing. That said, also read void main() is not legal in C++ but is legal in C.
    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
    Infant of C
    Join Date
    May 2010
    Location
    Karachi, Pakistan
    Posts
    118
    see..
    you have not mentioned the error. Apparently your code is ok!

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    15
    Thank u
    laserLight & msh for your reply.
    i added a dot after digits & the it worked flawlessly.

    But one more thing
    When i replaced float & %f with int & %d respectively.

    Then when i compiled it gave a warning
    "This Code has no effect"
    with this 2 lines
    Code:
    DA = 40/100*BS;
    HR = 20/100*BS;
    So y now ??
    Wats the problem here ???

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    15
    Which compiler to use ???
    i M using Turbo C++, but having gr8 troubles with it.
    'coz i m using Windows 7 & in that dos doesnt support fullscreen.
    So its a big problem.

    PlS. tell me a good compiler

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,158
    Quote Originally Posted by dharmil007
    But one more thing
    When i replaced float & %f with int & %d respectively.
    Why did you do that?
    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

  12. #12
    Infant of C
    Join Date
    May 2010
    Location
    Karachi, Pakistan
    Posts
    118
    Code::Block is much better to use!

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    15
    Quote Originally Posted by laserlight View Post
    Why did you do that?
    Just like that.
    Wanted to check that out, that it works with int or not

  14. #14
    Registered User
    Join Date
    May 2010
    Posts
    15
    hey everybuddy thanks for your reply.
    i M now using Code::Blocks instead of Turbo C++
    But 1 thing i cant understand.
    As from when i have learnt C, i have learnt to write clrscr ()
    i Have leanrt using Turbo C++ 3.0
    But whenevr i compile this programe & try to write clrscr (), it rejects the programme, giving clrscr () as error.

    & whenever i compile a programme, i get this msg after successful compilation
    http://i47.tinypic.com/3539mhg.png

    So can u pls. explain me y ???

  15. #15
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    That's because Turbo C is a compiler whereas CodeBlocks is an IDE that uses some different compiler (probably mingw, or gcc). You need to learn the difference between compilation, linkage and how a development environment integrates these processes.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  2. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  3. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 09:26 AM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 09:39 AM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM

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