Jiskha Homework Help
FRIDAY
November 20, 2009
SCHOOL SUBJECTS
- Art
- Business
- Computers
- English
- Foreign Languages
- Health
- Home Economics
- Math
- Music
- Physical Education
- Science
- Social Studies
GRADE LEVELS
- Preschool
- Kindergarten
- Elementary School
- 1st Grade
- 2nd Grade
- 3rd Grade
- 4th Grade
- 5th Grade
- 6th Grade
- 7th Grade
- 8th Grade
- High School
- 9th Grade
- 10th Grade
- 11th Grade
- 12th Grade
- College
- Adult Education
Post a New Question | Current Questions | Chat With Live Tutors

Homework Help Forum: C Programming

Posted by Claire on Friday, November 6, 2009 at 5:34am.

I am trying to find the sum of two integers. I entered the following program into the Visual Studio and it is telling me that there are errors. Can someone tell me where they are please?

#include <stdio.h>
int main( void )
{
int integer1;
int integer2;
int sum;

printf( "Enter first integer\n" );
scanf( "&d", &integer1 );
printf( "Enter second integer\n" );
scanf( "%d", &integer2 );

sum = integer1 + integer2;
printf( "Sum is %d\n" , sum );

return 0;
}

  • C Programming - TeacherJim, Friday, November 6, 2009 at 5:52am

    Compilers usually tell you where the errors are. Some part of teh output or screen should be saying something about a line number, or if you click on the error message in an IDE, it really should bring you to the line.

    The only error I can see looks like a typo, where you typed an ampersand instead of a percent:

    scanf( "&d", &integer1 );

    should read

    scanf( "%d", &integer1 );


  • C Programming - Claire, Friday, November 6, 2009 at 7:09am

    Thanks! But even after I changed the ampersand, it still gives me an error. It says

    error C2065: integer1 undeclared identifier
    error C2065: integer2 undeclared identifier

    and refers to the lines where there is the scanf of both integers.

  • C Programming - TeacherJim, Friday, November 6, 2009 at 7:24am

    OK. That would imply that the compiler isn't seeing the lines

    int integer1;
    int integer2;

    or, more precisely, isn't seeing them as declaring local variables at that point.

    Is it possible you've changed something unintentionally in trying to find a fix?

    I just pasted the code above - your original code - into an old version of VC++ I have on my machine, fixed the percent in scanf, and the program compiles are runs perfectly.

    Try doing exactly what I did, and see whether it works now.

  • C Programming - Claire, Friday, November 6, 2009 at 7:34am

    It still won't work but thanks a lot!!

  • C Programming - Claire, Friday, November 6, 2009 at 7:46am

    I don't know what happened but it worked now when I pasted the code that you used! Thanks a lot! You were really helpful :)

  • C Programming - TeacherJim, Friday, November 6, 2009 at 8:00am

    Great! I can' see the code I pasted a minute ago now. Dunno how that happened.

    Anyway, get used to it! :-) One of the peculiarities of programming is that sometimes things mysteriously don't work, and then mysteriously do when you look at them again.

    Actually, there's no mystery.

    What happens is that, in looking at the same chunk of code again and again, you just stop seeing something wrong or obvious. Your eye sees it, but your brain ignores it.

    The best remedy is to have somebody else take a look at the same code. It will likely be obvious to them. The next best is to type the offending line or lines in again, so that you won't ignore characters.

    The effect never goes away, but you get better at handling it with practice.

    Glad I could help!

Answer this Question

First Name:
School Subject:
Answer:

For Further Reading

SEARCH

COMMUNITY
FEATURES
- Live Tutors
- Net Riddle
- Reference
- Search