Confusion with code problems

Asad45

Honorable
Dec 9, 2013
3
0
10,510
0
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?
 

Charles A Peirce

Estimable
May 19, 2015
6
0
4,520
2
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.
 

Asad45

Honorable
Dec 9, 2013
3
0
10,510
0


Thanks for the explanation :D

 
Thread starter Similar threads Forum Replies Date
botguy Apps General Discussion 2
P Apps General Discussion 0
Z Apps General Discussion 0
M Apps General Discussion 0
W Apps General Discussion 2
clutchc Apps General Discussion 2
K Apps General Discussion 2
Q Apps General Discussion 1
W Apps General Discussion 2
rodion15 Apps General Discussion 3
C Apps General Discussion 2
vivaladino Apps General Discussion 2
deadfish Apps General Discussion 1
V Apps General Discussion 4
huilun02 Apps General Discussion 1
C Apps General Discussion 2
T Apps General Discussion 7
L Apps General Discussion 3
O Apps General Discussion 1
G Apps General Discussion 9

ASK THE COMMUNITY