Thread: Basic Programm giving Error

  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 07:55 AM.

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

  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
    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
    28,413
    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.
    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
    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
    28,413
    Quote Originally Posted by dharmil007
    But one more thing
    When i replaced float & %f with int & %d respectively.
    Why did you do that?
    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

  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.

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, 08:26 AM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM