Help with (should be easy) homework

This has to be in stdio.h and I've tried figuring out what I'm doing wrong here by looking at several example. The function will execute but it comes out to 0 and it doesn't prompt me to put in values like I'm trying to get it to do. I'm obviously new to this any help would be greatly appreciated, I cant help but feel like I'm close and missing something simple. The problem I'm doing is explained in the code as per teacher instruction.

#include <stdio.h>

/* HW2 Challenge 2. Given a=5, b=1, x=10, and y=5, create a program
that outputs the result of the formula f=(a-b)(x-y), But asks the user to put in the values of a,b,x, and y*/

int main() {

int a,b,x,y=0;

printf("\n Enter the value of a:");
scanf("%d", &a);
printf("\n Enter the value of b:");
scanf("%d", &b);
printf("\n Enter the value of x:");
scanf("%d", &x);
printf("\n Enter the value of y:");
scanf("%d", &y);

int f=(a-b)*(x-y);

printf("\nThe value of f is %d", f);
return 0;
}
Last edited on
Please use code tags.

The code works fine with me, it outputs the result 20.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>

int main() {

int a = 0, b = 0, x = 0, y = 0;

printf("\n Enter the value of a:");
scanf("%d", &a);
printf("\n Enter the value of b:");
scanf("%d", &b);
printf("\n Enter the value of x:");
scanf("%d", &x);
printf("\n Enter the value of y:");
scanf("%d", &y);

int f=(a-b)*(x-y);

printf("\nThe value of f is %d", f);
return 0;
}


1
2
3
4
5
6
7
8
9
10
5
1
10
5

 Enter the value of a:
 Enter the value of b:
 Enter the value of x:
 Enter the value of y:
The value of f is 20 
I have tried running this in a few different places. It did make you manually input those number? I even tried to copy and paste what you typed to see if it would work for me. And thanks again.





Last edited on
Your program appears to be correct. Evidence:
http://coliru.stacked-crooked.com/a/cbbb62a392a21186

Why isn't the prompt showing up, you might ask?
This is because standard output is buffered.
Before characters you put into printf() show up on standard output, they get shoved into a block of memory somewhere, which is written to standard output when (among other things):
- a newline is reached
- you tell the computer to flush the buffer.

The rules are slightly different in C++ (your program is C -- hopefully you know this), and the behavior may vary cross-system.

After each invocation of printf() that doesn't end with a newline, call fflush(stdout);. This will flush the internal buffer into standard output:

1
2
3
4
5
6
7
8
printf("\n Enter the value of a:"); fflush(stdout);
scanf("%d", &a);
printf("\n Enter the value of b:"); fflush(stdout);
scanf("%d", &b);
printf("\n Enter the value of x:"); fflush(stdout);
scanf("%d", &x);
printf("\n Enter the value of y:"); fflush(stdout);
scanf("%d", &y);



There are two minor issues with your code:
int a,b,x,y=0;
Declares four integers but only initializes y. In order to initialize all four variables you need to either duplicate the initializer:
int a = 0, b = 0, x = 0, y = 0;
Or do something like this:
int a,b,x,y; a=b=x=y=0;

In this context it is NOT acceptable to leave the variables uninitialized, because printf() can potentially fail to assign a value to each of those variables, which would cause your program to use an uninitialized value. (But maybe you don't care about errors now; if that's the case, just ignore me.)

Also,
int f=(a-b)*(x-y);
Should never be modified by your program after its' value is initialized. Mark it const. Doing this consistently will help prevent subtle bugs in the long run:
int const f=(a - b)*(x - y);

---

The buffering allows the computer to make just one big write instead of many tiny ones, which can potentially speed things up significantly.
Last edited on
Thanks again both of you, I believe this answered all of my questions I really appreciate it.
Topic archived. No new replies allowed.