Thread: Unable to get output..........

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    45

    Unable to get output..........

    Unable to get output of this program,add bonus to employee salaries
    Code:
    #include<stdio.h>
    main()
    {
     int salary[5],i;
     printf("Enter the salaries");
     for(i=0;i<=4;i++)
     {
      scanf("%d",&salary[i]);
      bonus(salary[i]);
     }
     getch();
    }
    bonus(int a)
    {
     int net,bon;
     bon=10*a/100;
     net=a+bon;
     printf("the net salary is%d",net);
    }
    C enlightened

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    What are you inputting into the program?

    Be careful about this line:
    Code:
    bon=10*a/100;
    Remember this is integer math and there are no fractions.

    Jim

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    first, you should stop using magic numbers for your array size and loop counter.

    something more like this is in order:
    Code:
    #include<stdio.h>
    
    const int SIZE = 5;
    
    main()
    {
     int salary[SIZE],i;
     printf("Enter the salaries");
     for(i=0;i<SIZE;i++)
    second, you're using integer math to compute the bonus. there's a good chance that your bonus is simply getting reduced to zero because of this. try using float or double instead.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    45
    Quote Originally Posted by jimblumberg View Post
    What are you inputting into the program?

    Be careful about this line:
    Code:
    bon=10*a/100;
    Remember this is integer math and there are no fractions.

    Jim
    Oh,here are the values,2000,15000,40000,60000,10000.
    @Elkvis,some of the commands you used are not in my syllabus yet.
    C enlightened

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by sameertelkar View Post
    @Elkvis,some of the commands you used are not in my syllabus yet.
    they didn't cover constants yet? that seems very strange.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    There are several problems with your code.

    First it probably doesn't even compile without errors or warnings.

    You have failed to properly prototype your function bonus() so your compiler should tell you about this. Second, the use of default arguments and default return types is a very bad habit. You should always specify the return type and all parameters.

    Also using the non-standard getch() function without including the proper include file should also cause problems.


    they didn't cover constants yet? that seems very strange.
    Since the compiler the op is using seems very outdated #defining the constant would probably be a better choice.


    Code:
    #include<stdio.h>
    
    #define SIZE 5
    void bonus(int a);
    
    int main()
    {
    	int salary[SIZE],i;
    	printf("Enter the salaries");
    	for(i = 0; i < SIZE; i++)
    	{
    		scanf("%d", &salary[i]);
    		bonus(salary[i]);
    	}
    }
    
    void bonus(int a)
    {
    	int net, bon;
    	bon = 10 * a / 100;
    	net = a + bon;
    	printf("the net salary is%d", net);
    }
    I recommend you get a more up to date compiler, your's seems very outdated.

    Jim

  7. #7
    Registered User
    Join Date
    Sep 2012
    Posts
    45
    Dear Jim,My original code did get compiled without errors but I am getting a wrong output,I tried your code and I got the same output,I think there is some problem with the compiler.
    Edit-I also changed int to float but then I am getting a zero output.
    Last edited by sameertelkar; 07-18-2013 at 08:59 AM.
    C enlightened

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by sameertelkar View Post
    but I am getting a wrong output
    show us a screen capture of the input and output of your program.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  9. #9
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    It seems be working as expected. This is what I get

    Code:
    Enter the salaries2000 15000 20000
    the net salary is2200
    the net salary is16500
    the net salary is22000
    whats your output?

    ssharish2005
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    I'll bet you're dealing with 16-bit integers, that have a maximum positive value of 32767.

    get a new compiler. turbo C++ is literally the worst thing out there, because it teaches people outdated and incorrect things.

    MinGW and Visual C++ Express are two very good ones for windows.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    It would certainly help if you put some newlines in your input/output, so that we can tell the difference between what you typed in, and what you printed.

    But from the look of things, you typed in 40000 and got -25469.

    The reason is pretty simple - your fossil compiler only understands 16 bit integers, which limits you to -32768 .. 32767.

    So 40000 is instant overflow, and confused maths.

    Now, since you seem to be from India, where teaching obsolete tech is mandated at the highest levels, you'd better read my signature link if you want to really get ahead.
    You can start by getting another compiler to verify your code on (you'll still need TurboCrap to pass your exams, but you shouldn't be restricted by it).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    MinGW and Visual C++ Express are two very good ones for windows.
    Actually since this is C I don't consider Visual C++ a good choice. Also MinGW is not a compiler, it's a development environment. I'd suggest gcc for the compiler with either Code::Blocks, Orwell Dev-C++, Eclipse, or NetBeans for the IDE. Or if just interested in C, Pelles C would be a good choice.

    Jim

  13. #13
    Registered User
    Join Date
    Sep 2012
    Posts
    45
    Here
    Unable to get output..........-turboc-jpg
    C enlightened

  14. #14
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by jimblumberg View Post
    Actually since this is C I don't consider Visual C++ a good choice.
    MS Visual C++ does just fine with C.

    Quote Originally Posted by jimblumberg View Post
    Also MinGW is not a compiler, it's a development environment.
    MinGW is the windows port of gcc. gcc is automatically implied when I say MinGW. you may be confusing MinGW with MSYS.

    Quote Originally Posted by jimblumberg View Post
    either Code::Blocks, Orwell Dev-C++, Eclipse, or NetBeans for the IDE. Or if just interested in C, Pelles C would be a good choice.
    I'd never recommend dev-c++ to anyone. the newer versions haven't proved themselves yet. the other IDEs are all good choices. I know almost nothing about Pelles C, but some of the things I've read about it suggest to me that it's not the most standards-compliant thing out there.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  15. #15
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    MS Visual C++ does just fine with C.
    Only if you consider a compiler that only supports a C90 just fine, I don't. There have been two more recent standards that they refuse to comply with, namely C99 and C11. In my opinion that refusal makes their compiler outdated, on a par with Turbo-C.

    I know almost nothing about Pelles C, but some of the things I've read about it suggest to me that it's not the most standards-compliant thing out there.
    Pelles C offers complete C90 support and it's C99 and C11 support availability makes this a much better choice than the Microsoft compiler you say is just fine.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unable to get output..........
    By sameertelkar in forum C Programming
    Replies: 7
    Last Post: 07-03-2013, 10:53 AM
  2. Replies: 25
    Last Post: 12-27-2012, 11:19 PM
  3. Unable to print the output of a conversion problem
    By abhishekcoder in forum C Programming
    Replies: 1
    Last Post: 04-13-2012, 11:16 AM
  4. Seem unable to use EOF?
    By black_stallion in forum C Programming
    Replies: 18
    Last Post: 01-02-2012, 08:23 PM
  5. unable to print
    By nehal in forum C Programming
    Replies: 2
    Last Post: 03-09-2011, 08:01 AM