Question

I am new to C this is something I have always been confused about let's say I have a code like this I only want to use char

    char a, b, c;
    printf("input first character: ");
    scanf(" %c", &a);
    printf("input second character: ");
    scanf(" %c", &b);
    printf("input thrid character: ");
    scanf(" %c", &c);

how ever I want to be able to read in space as well; I noticed how this would only read in non-space characters, what if I want to read space as well something like this c=' '; how do I scan this space in;

now by listening to suggestion of using getchar() I wrote this :

#include<stdio.h>

int main(void)
{
  char a,b,c;
printf("input the first char:");
a=getchar();
printf("input the second char:");
b=getchar();
printf("input the third char:");
c=getchar();
 return 0;


}

how ever something strange happens when I compile and run the program

the program output is like this

input the first char:
input the second char:input the third char:

now it never let me to input the second char it jumped straight to the third request at the end I was only asked to enter 2 inputs which is very strange because the program clearly asked for 3 in the code.

now here is a program I wrote like this I added what is suggested into the code block

int main(void)
{
    int totalHeight=0, floorWidth=0, amountOfStories, amountWindowForTop, amountWindowForMiddle, amountWindowForBottom, windowHeight, middleWindowWidth, topWindowWidth, bottomWindowWidth, minimumHeight, minimumWidth;
    int betweenDistanceTop, betweenDistanceMiddle, betweenDistanceBottom, edgeDistanceTop, edgeDistanceBottom, edgeDistanceMiddle;
    char topFloorWindowContent, middleFloorWindowContent, bottomFloorWindowContent, windowBorder, floorBorder;
    int tempMax, tempValue, tempSideDistance, tempBetweenDistance;
    printf("please enter how many stories your building would like to have: ");
    scanf("%d",&amountOfStories);
    minimumHeight=amountOfStories*6+1;
    while((totalHeight<minimumHeight)||((totalHeight%amountOfStories)!=1))
    {
        printf("please enter the totalHeight (minimum %d): ",minimumHeight);
        scanf("%d",&totalHeight);
    }
    printf("please enter how many window building would have for top floor: ");
    scanf("%d",&amountWindowForTop);
    printf("please enter how many window building would have for middle floors: ");
    scanf("%d",&amountWindowForMiddle);
    printf("please enter how many window building would have for bottom floor: ");
    scanf("%d",&amountWindowForBottom);
    tempMax=amountWindowForTop;
    if (tempMax<amountWindowForMiddle)
    {
        tempMax=amountWindowForMiddle;
    }
    if (tempMax<amountWindowForBottom)
    {
        tempMax=amountWindowForBottom;
    }
    while(floorWidth<tempMax)
    {
        printf("please enter the width of the building (Minimum %d): ",tempMax*4+1);
        scanf("%d",&floorWidth);
    }

    char a, b, c;


    printf("a:");
    a=getchar();getchar();
    printf("b:");
    b=getchar();getchar();
    printf("c:");
    c=getchar();
    printf("a=%c, b=%c, c=%c", a, b, c);
return 0;
}

now here is the funny part if I put this block of code in the big program it doesn't work the output is something like this

please enter how many stories your building would like to have: 2
please enter the totalHeight (minimum 13): 2
please enter the totalHeight (minimum 13): 2
please enter the totalHeight (minimum 13): 13
please enter how many window building would have for top floor: 2
please enter how many window building would have for middle floors: 2
please enter how many window building would have for bottom floor: 2
please enter the width of the building (Minimum 9): 9
a:
b:*
c:a=
, b=
, c=

as we can see a b c all read in \n instead of the space * and c didn't even read anything at all Why is that ?

Was it helpful?

Solution

The problem with your code is this: when you read the first char (a), you press enter (\n) for insert the next char, so now on stdin there is a \n that you haven't readed. When you try to read the next character, (b) the program read the previous \n from stdin and does not allow you to read the next char. So, when you read a char with getchar() and then press enter on the keyboard, you need a second getchar() for remove the \n. Here is a sample code that could solve your issue:

#include<stdio.h>

int main(void) {
  char a, b, c;

  printf("a:");
  a=getchar();getchar();
  printf("b:");
  b=getchar();getchar();
  printf("c:");
  c=getchar();
  printf("a=%c, b=%c, c=%c", a, b, c);
  return 0;
}

For the edited you posted, you need to put what is called "the stdin cleaner" before taking the value for a,b,c:

while(getchar()!='\n');

it just reomove all characters till \n. Please, take note that when programs like the one you posted has a lot of input from keyboard, sometime you get this issue because there are extra chars in stdin. So the general answer for this issue will be try to figure out where these extra chars (mostly there is an extra \n somewhere) could be and use a function like the one i mentioned to remove so that you can continue reading from stdin.

OTHER TIPS

You should use getchar()

a = getchar();

scanf will not scan anything until you give any data so it is not useful to scan ' ' char.

for this you have to use getchar() fun for char or gets() for string, it will scan data until you give enter. it will comes out even if either you have not provided any char, or simple ' ' char.

If u want to read the character with space then you may use the gets() functtion.

char str[10];

str=gets();

try scanf("%c", &ch). as stated in scanf format specifier:

"Whitespace character: the function will read and ignore any whitespace characters encountered before the next non-whitespace character (whitespace characters include spaces, newline and tab characters -- see isspace). A single whitespace in the format string validates any quantity of whitespace characters extracted from the stream (including none)."

getchar() gets unexpected result because in windows, newline(when you hit Enter in console) is two characters.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top