The following program should take 4 numbers but it takes 5 numbers and prints first 4 number entered. How to correct the problem???

#include<stdio.h>
#include<conio.h>

void main()
{ 
	int a1,a2,a3,a4;
	printf("Enter 4 numbers \n");
	scanf_s("%d %d %d %d ", &a1,&a2, &a3, &a4);
	printf("The 4 numbers entered are %d %d %d and %d",a1,a2, a3, a4);
	_getch();
}

Output

Enter 4 numbers
6
7
8
9
1
The four numbers entered are 6 7 8 and 9

First: this is not C++ program!
Second: scanf_s - what kind of function is that? I don't know, maybe it exists, but I've heard only of scanf.
Third: solution is simple, remove blank space between last %d and "

scanf_s("%d %d %d %d", &a1,&a2, &a3, &a4);
// not (%d "), but (%d")

Fourth: Don't write void main() ! Write int main() instead.

scanf_s is some kind of secure version of scanf... its available in msdn...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.