Confusion with code problems

Asad45

Honorable
Dec 9, 2013
3
0
10,510
I just started learning C language, please help me with my problem.
**Using getchar() the program after executed and done with input doesn't close until I press enter. Q1.) In the code below the getchar() doesn't work but why?

Code:
#include <stdio.h>
int main(void)
{
	int height, length, width, volume, weight;

	printf("Enter height of box: ");
	scanf("%d", &height);
	printf("Enter length of box: ");
	scanf("%d", &length);
	printf("Enter width of box: ");
	scanf("%d", &width);
	volume = height * length * width;
	weight = (volume + 165) / 166;

	printf("Volume (cubic inches): %d\n", volume);
	printf("Dimensional weight (pounds): %d\n", weight);
    
	getchar(); 
	return 0;
}

But after adding another getchar() [ shown below] cmd doesn't close now.

Code:
#include <stdio.h>
int main(void)
{
	int height, length, width, volume, weight;

	printf("Enter height of box: ");
	scanf("%d", &height);
	printf("Enter length of box: ");
	scanf("%d", &length);
	printf("Enter width of box: ");
	scanf("%d", &width);
	volume = height * length * width;
	weight = (volume + 165) / 166;

	printf("Volume (cubic inches): %d\n", volume);
	printf("Dimensional weight (pounds): %d\n", weight);
    
	getchar(); getchar();
        return 0;
}
Q2.) Why, is that typing two getchar() doesn't close the cmd?
 
Solution
You problem lies in what happens when you press the "enter" key, and how scanf handles it.
(That's right - most programming problems happen a few lines earlier in the code than they appear. It's just one of those things.)
When you press "enter" on some computers the characters #13 and #10 enter the keyboard buffer. The scanf function only takes the first character, because that is all it needs to complete its purpose. Later calls to scanf either flush the keyboard buffer or ignore characters they aren't looking for.

When getchar is called, iot finds a character waiting for it, so it returns immediately. You can prove this by changing your code like this:

#include <stdio.h>

int main(void)

{

int height, length...

Charles A Peirce

Estimable
May 19, 2015
6
0
4,520
You problem lies in what happens when you press the "enter" key, and how scanf handles it.
(That's right - most programming problems happen a few lines earlier in the code than they appear. It's just one of those things.)
When you press "enter" on some computers the characters #13 and #10 enter the keyboard buffer. The scanf function only takes the first character, because that is all it needs to complete its purpose. Later calls to scanf either flush the keyboard buffer or ignore characters they aren't looking for.

When getchar is called, iot finds a character waiting for it, so it returns immediately. You can prove this by changing your code like this:

#include <stdio.h>

int main(void)

{

int height, length, width, volume, weight;
int c;

printf("Enter height of box: ");

scanf("%d", &height);

printf("Enter length of box: ");

scanf("%d", &length);

printf("Enter width of box: ");

scanf("%d", &width);

volume = height * length * width;

weight = (volume + 165) / 166;


printf("Volume (cubic inches): %d\n", volume);

printf("Dimensional weight (pounds): %d\n", weight);


c = getchar();

printf("exit character = %d\n", c);

return 0;

}

When you add a second getchar, the buffer has been emptied by the first call, so it waits for your key press.
 
Solution

Asad45

Honorable
Dec 9, 2013
3
0
10,510


Thanks for the explanation :D