printf

hey everyone, i just started learning programming and I've given a task to create this output "The given numbers are 4, 5, 9, 20 and 54
The sum of all numbers = 92" by using printf and scanf. I almost solve it but I'm stuck with this problem: when I compile & run the source file, the content of the output won't display unless i type anything and press enter. How do i make the content display immediately as i run the source file? Thank you.



#include <stdio.h>

int main()
{
int a = 4;
int b = 5;
int c = 9;
int d = 20;
int e = 54;
int sum;

scanf("%d", &a);
scanf("%d", &b);
scanf("%d", &c);
scanf("%d", &d);
scanf("%d", &e);

sum = a + b + c + d + e;

printf("The given numbers are 5, 4, 9, 20, 54\nThe sum of all numbers = %d\n", sum);

}
Last edited on
closed account (E0p9LyTq)
The scanf() function reads from stdin (usually the keyboard) and stores the data in the variable.
http://www.cplusplus.com/reference/cstdio/scanf/

You need to manually enter 4, 5, 9, 20, and 54 if you want the sum to be 92. Enter any other number(s), the order doesn't matter, and the sum won't be 92.
methinks you want this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

#include <stdio.h>

int main()
{
int a = 4;
int b = 5;
int c = 9;
int d = 20;
int e = 54;
int sum = 0; //good habit to initialize everything, even if you overwrite it later. 

//removed inputs. 

sum = a + b + c + d + e;

//removed hard-coded values from print statement and used exact values of variables. 
printf("The given numbers are %i, %i, %i, %i, %i\nThe sum of all numbers = %d\n", a,b,c,d,e, sum);

}


if you put the scans back in, you should tell the user what to type so they know what it is waiting for with an extra print statement. If you put the scans back in, it will give the right output now, say you put in 1,2,3,4,5 it will say the sum of 1,2,3,4,5 is 15 .

Last edited on
Topic archived. No new replies allowed.