help with errors

This is a discussion on help with errors within the C Programming forums, part of the General Programming Boards category; the question is : enter an odd number (integer) enter an even number (integer) enter the same odd number (real) ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    7

    help with errors

    the question is :

    enter an odd number (integer)
    enter an even number (integer)
    enter the same odd number (real)
    enter the same even number (real)

    so what i did so far..

    Code:
    #include <stdio.h>
    
    int main()
    {
       int number1;
       int number2;
       int sum;
       int product;
       int difference;
       int quotient;
    
       float number3;
       float number4;
       float sum;
       float product;
       float difference;
       float quotient;
    
       printf( "Enter an odd number\n" );
       scanf( "%d", &number1 );
    
       printf( "Enter an even number\n" );
       scanf( "%d", &number2 );
    
       printf( "Enter the same odd number\n" );
       scanf( "%f", &number3 );
    
       printf( "Enter the same even number\n" );
       scanf( "%f", &number4 );
    
       sum = number1 + number2;
    
       printf( "Sum of the integers is %d\n", sum );
    
       product = number1 * number2;
    
       printf( "Product of the integers is %d\n", product );
    
       difference = number1 - number2;
    
       printf( "Difference of the integers is %d\n", difference );
    
       quotient = number1 / number2;
    
       printf( "Quotient of the integers is %d\n", quotient );
    
       sum = number3 + number4;
    
       printf( "Sum of the real numbers is %f\n", sum );
    
       product = number3 * number4;
    
       printf( "Product of the real numbers is %f\n", product );
    
       difference = number3 - number4;
    
       printf( "Difference of the real numbers is %f\n", difference );
    
       quotient = number3 / number4;
    
       printf( "Quotient of the real numbers is %f\n", quotient );
    
       return 0;
    
    }
    the error is because of the declaration of sum, product, difference, quotient

    but if i get rid of int sum etc etc, or float sum etc etc that wouldnt give me the answer that i wanted, right? (one of the result for either integer or real would be zero)

    or did i do wrong right from the beginning? xD

    thanks

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You can not have the same name for two variables of different types - how will the compiler be able to tell if you mean the float or integer version of the variable - it is perfectly valid to add two integers and turn it into a float, or the other way around.

    So you need to use two variables with different names for the float and integer sum variables.

    Or you could have two differnet functions that produce the output, and use sum as a local variable in each function - but I don't think you're quite ready for that stage yet.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    7
    ah ok

    no wonder it didnt turn out well

    i didnt change everything properly earlier when i renamed

    float sum to float sum1

    and forgotten to change :

    sum1 = number3 + numer4;

    XD

    thanks for the explanation

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. global namespace errors
    By stubaan in forum C++ Programming
    Replies: 9
    Last Post: 04-02-2008, 04:11 PM
  2. Ten Errors
    By AverageSoftware in forum Contests Board
    Replies: 0
    Last Post: 07-20-2007, 11:50 AM
  3. Winsock compilation errors
    By jmd15 in forum Networking/Device Communication
    Replies: 2
    Last Post: 08-03-2005, 09:00 AM
  4. Unknown Errors in simple program
    By neandrake in forum C++ Programming
    Replies: 16
    Last Post: 04-06-2004, 03:57 PM
  5. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM

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