Please Show My Mistake

This is a discussion on Please Show My Mistake within the C Programming forums, part of the General Programming Boards category; Q) A program is required to input a series of at least three numbers from the keyboard. The first two ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    4

    Question Please Show My Mistake

    Q) A program is required to input a series of at least three numbers from the keyboard. The first two numbers are to be lower and upper limit respectively. The program is require to output to the screen the total of all the following numbers which fall between those two limits, but are not equal to those limits. Use EOF to determine the end of the number sequence.
    Ex: input 10, 20, 5, 12, 17, 23, 16,20
    Output 45 (12+17+16)
    Code:
     
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    float x,y,z,sum=0;
    clrscr();
    printf("\n Enter atleast Three numbers:");
    scanf("%f %f",&x,&y);
        {if (0<y-x)     {
            scanf("%f",&z);
                while (z==EOF){
                if(x<z && y>z)
                sum=sum+z;
                scanf("%f",&z);}
                }
            else    {
            scanf("%f",&z);
                while (z==EOF){
                if(x>z && y<z)
                sum=sum+z;
                scanf("%f",&z);}
                }
        }
        printf("\n Sum of values are :%f",sum);
    getch();
    }
    Last edited by Salem; 11-17-2012 at 11:42 AM. Reason: fixed fugly fonts

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    It is
    Code:
    int main(void)
    and not void main!!!!!!!!!!

    Also when we want to handle EOF we use to have an int variable for this...

    Also because an image is equal to one hundred words, take a look at the image of salem

    Also it good that main returns 0 before terminating...
    Last edited by std10093; 11-17-2012 at 10:47 AM.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    4
    Thankz, but I didn't get you.

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Oh ok...
    Well a typical main is like this
    Code:
    int main(void)
    {
         ....
         return 0;
    }
    You have as return value of main void and not int.Also you do not return 0 at the end.

    What is the error?

    Also mind that you should wrap your code in code tags
    [key]
    /* YOUR CODE HERE */
    [/key]
    Replace key with code in order this to work

    And about EOF, f is declared as float, where the variable to compare with EOF is usually an int

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    > scanf("%f",&z);
    > while (z==EOF)
    This should be
    while ( scanf("%f",&z) != EOF )

    This assumes the user doesn't do something stupid, like type in "hello" instead of 123.
    If they do, this will just lock up.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is my mistake
    By Dave Couture in forum C Programming
    Replies: 8
    Last Post: 08-14-2012, 11:28 PM
  2. Replies: 14
    Last Post: 07-01-2011, 01:29 PM
  3. What is my mistake ?
    By Freelander1983 in forum C++ Programming
    Replies: 10
    Last Post: 12-11-2007, 09:31 AM
  4. show() method does not show...
    By kocika73 in forum C++ Programming
    Replies: 12
    Last Post: 04-09-2006, 10:27 PM

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