1

Just wondering what it is that I am doing wrong here?? It's very simple programming and I really should know this but just can't see the problem! Scanf is not reading the results of the equations into the respective positions in the array.

#include <stdio.h>

int main(void)
{
int numbers[4];

printf("Please enter 2 numbers:\n");
scanf("%d %d", &numbers[0], &numbers[1]);

printf("Your 2 numbers added are: %d\n", numbers[0] + numbers[1]);
scanf("%d", &numbers[2]);

printf("Your 2 numbers subtracted are: %d\n", numbers[0] - numbers[1]);
scanf("%d", &numbers[3]);

printf("The results of your program are: %d, %d\n", &numbers[2], &numbers[3]);

return 0;
}
2
  • 3
    Are you supposed to do numbers[2] = numbers[0] + numbers[1];? scanf is used to get input from a user not to get results from internal calculations. Commented Jan 3, 2012 at 12:07
  • That's exactly it Joachim hit the nail on the head. I had a suspicion that scanf was the problem. What is the best way to read directly from the console line though? I know I can just do numbers[2] = numbers[0]+numbers[1] but I'm just experimenting with different things Commented Jan 3, 2012 at 12:11

5 Answers 5

3
    #include <stdio.h>

    int main(void)
    {
    int numbers[4];

    printf("Please enter 2 numbers:\n");
    scanf("%d %d", &numbers[0], &numbers[1]);

    printf("Your 2 numbers added are: %d\n", numbers[0] + numbers[1]);
    numbers[2] = numbers[0] + numbers[1];

    printf("Your 2 numbers subtracted are: %d\n", numbers[0] - numbers[1]);
    numbers[3] =  numbers[0] - numbers[1];

    printf("The results of your program are: %d, %d\n", numbers[2], numbers[3]);

    return 0;
    }

Try this..

Sign up to request clarification or add additional context in comments.

Comments

2

Unless you are piping the output of stdout back to stdin, you can't read back what you're printing on the output like you're attempting to-do here:

printf("Your 2 numbers added are: %d\n", numbers[0] + numbers[1]);
scanf("%d", &numbers[2]);

You should be doing this:

printf("Your 2 numbers added are: %d\n", numbers[2] = numbers[0] + numbers[1]);
printf("Your 2 numbers subtracted are: %d\n", numbers[3] = numbers[0] - numbers[1]);

Then on the final output, take out the address-of operators when printing your array values:

printf("The results of your program are: %d, %d\n", numbers[2], numbers[3]);

1 Comment

That's exactly what I was looking for Jason thank you. And it's funny because I know this stuff. When working on more advanced things the basics can be tricky because I assume that the answer should be more difficult than it is! :)
2

Look at your last printf you have the &'s there when you don't want them.

1 Comment

And that error would have been found, if you use the gcc compiler, by the -Wall flag (which you should always use for gcc).
2
printf("The results of your program are: %d, %d\n", &numbers[2], &numbers[3]);

should be

printf("The results of your program are: %d, %d\n", numbers[2], numbers[3]);

The & operator gets the address of the given variable. Here you just want to print the value stored in the variable and not the address of the variable.

OTOH, you don't need the scanf()'s for number[2] and number[3] as you are computing them! And the array size need not be 4 instead it can be 2.

Comments

1
printf("The results of your program are: %d, %d\n", numbers[2], numbers[3]);

this will give the garbage value in number[2] and number[3] because you are not giving any values to number[2] and number[3].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.