-6

I can't seem to find the issue in my code. It seems like everything is fine. I'm supposed to output the product and sum of two integers.

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>

main() {


int number1, number2, productResult, sumResult;

printf("Enter first number: ");
scanf("%i", &number1);
productResult = number1 * number2;

printf("Enter second number: ");
scanf("%i", &number2);
sumResult = number1 + number2;

printf("The product is: %i \n", productResult);
printf("The sum is:s %i \n", sumResult);

system("pause");
} // end of main
Jake
  • 59
  • 4
  • 3
    You calculate the product before the second number is defined. – tripleee Sep 12 '16 at 03:13
  • Describing how your code is not working would help focus our efforts. For detailed posting guidelines, please review the [help]. – tripleee Sep 12 '16 at 03:15
  • 1
    This question was dead on arrival. Sigh. – Tim Biegeleisen Sep 12 '16 at 03:15
  • 1
    You cannot use both numbers in the calculation until you've first obtained both numbers. This should be clear to you when you see `printf("Enter first numbers"); scanf("%i", &number1); productResult - number1 * **number2** - you haven't obtained a value for number2 yet.It's important to learn to actually read the code you're writing.; – Ken White Sep 12 '16 at 03:15
  • Please read about how to create an MCVE ([MCVE]). Amongst other things, an MCVE would show sample input data, the expected output and the actual output you're getting. You've done a lot with the code, but you need to give the context too. (Also, you should be coding to at least C99 standard, preferably C11, and therefore should be specifying the return type on `main()` — see [What should `main()` return in C and C++?](http://stackoverflow.com/questions/204476/) – Jonathan Leffler Sep 12 '16 at 03:18
  • The execution of a normal computer program is sequential, as in it happens in the order you type it. This means that it doesn't make any sense to calculate results before all parameters have been read. And that's it. – Lundin Sep 12 '16 at 09:01

4 Answers4

1

I encourage you to either get a better compiler or listen to the warnings from your compiler:

blah.c:5:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
main() {
^
blah.c:10:31: warning: variable 'number2' is uninitialized when used here [-Wuninitialized]
    productResult = number1 * number2;
                              ^~~~~~~
blah.c:6:25: note: initialize the variable 'number2' to silence this warning
    int number1, number2, productResult, sumResult;
                        ^
                         = 0
2 warnings generated.

You're using number2 before you've asked the user for its contents.

Bill Lynch
  • 72,481
  • 14
  • 116
  • 162
0

You were computing the product before the second number was defined. Get the inputs first, then do the calculations:

int number1, number2, productResult, sumResult;

printf("Enter first number: ");
scanf("%i", &number1);
printf("Enter second number: ");
scanf("%i", &number2);

productResult = number1 * number2;
sumResult = number1 + number2;

printf("The product is: %i \n", productResult);
printf("The sum is:s %i \n", sumResult);
Tim Biegeleisen
  • 387,723
  • 20
  • 200
  • 263
0

You are computing productResult before the input of number2. Such mistake will always lead to undefined behavior(you will get the some random number as value of number2 followed by productResult).

printf("Enter first number: ");
scanf("%i", &number1);
printf("Enter second number: ");
scanf("%i", &number2);
productResult = number1 * number2;
sumResult = number1 + number2;
Shravan40
  • 6,849
  • 5
  • 21
  • 43
-1

You had a bug. Please try this instead.

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int main() {
    int number1, number2, productResult, sumResult;
    printf("Enter first number: ");
    scanf("%i", &number1);
    printf("Enter second number: ");
    scanf("%i", &number2);
    sumResult = number1 + number2;
    productResult = number1 * number2;
    printf("The product is: %i \n", productResult);
    printf("The sum is:s %i \n", sumResult);
    return 0;
} 

Test

Enter first number: 2
Enter second number: 4
The product is: 8 
The sum is:s 6 
Niklas R.
  • 22,209
  • 67
  • 202
  • 380