0

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 ?

nanobots
  • 407
  • 8
  • 21
  • @TAsk OP asked how to input "char", not "string" – ikh Mar 24 '14 at 10:34
  • yeah I know how to scan in as string but in some circumstances I only want to scan 1 character and I realised I can't think of anyway of scanning in empty space – nanobots Mar 24 '14 at 10:41

5 Answers5

0

You should use getchar()

a = getchar();
ikh
  • 9,139
  • 1
  • 28
  • 64
  • ok I tried to do that but it gave me something strange basically the program printed output like this input the first char: input the second char: input the third char: – nanobots Mar 24 '14 at 10:39
  • the input first char showed up, and I entered space it worked, however the input 2nd char and third char came up at same time for some reason – nanobots Mar 24 '14 at 10:40
  • I added my modifications to the test by using getchar something strange happens I posted modification in the question can you have a look? – nanobots Mar 24 '14 at 10:51
  • @user2948725 Well, `getchar()` and the other I/O functions performs *buffering*. when you input "a" and press enter, actually you do input two characters: 'a' and '\n'. And they is inserted into "the buffer". All input function reads from buffer, not keyboard directly. `scanf()` ignores '\n' so you don't know this until now, but `getchar()` doesn't. Thus, first `getchar()` returns 'a' and second `getchar` returns '\n'. – ikh Mar 24 '14 at 10:53
  • I see so how would I make it ignore \n request? because I want to scan in the values 1 by 1 – nanobots Mar 24 '14 at 10:55
  • @user2948725 Yes. To avoid this problem, you can ignore '\n': `do { a = getchar(); } while (a == '\n');` – ikh Mar 24 '14 at 10:56
  • @user2948725 And please notice user can input several characters at once, like this: "input the first char:abc". In this case, "abc\n" is inserted into buffer, and `getchar()` returns "a", "b", "c", "\n" in sequence. So if user input "abc", 4 calls of `getchar()` will return immediately. (If you don't understand, I recommend you try to do it and look what happens!). – ikh Mar 24 '14 at 11:00
  • so if I type abc this will happen a='a'; b='b'; c='c';? – nanobots Mar 24 '14 at 11:03
  • @user2948725 Yes. And '\n' still remains in buffer. – ikh Mar 24 '14 at 11:04
0

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.

akash61090
  • 37
  • 1
  • 4
0

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

char str[10];

str=gets();
Rahul Goyal
  • 203
  • 1
  • 8
0

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.

wacky6
  • 135
  • 3
0

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.

WileTheCoyot
  • 505
  • 1
  • 4
  • 18
  • 1 strange thing I find is that yes the code block like this works on its own program I created however if I try to use code block like this in a big program it won't take the input properly the backspace seemed to be scanned in instead of the space – nanobots Mar 24 '14 at 21:42
  • @user2948725 could you make a practial example of your issue? – WileTheCoyot Mar 24 '14 at 21:44
  • I have updated the question included the section of my code that I tried to put the getchar into. I noticed the code don't work if I put in a big program but if I ran it on its own little test program it works – nanobots Mar 24 '14 at 21:56
  • @user2948725 i have edited my answer. Please, take note that this solution works for this specific sequence of scanf/getchar. You need to understand why there could be extra chars you have not read and remove them – WileTheCoyot Mar 24 '14 at 22:13
  • ic so it checks if the value input is \n or not before entering it in so it only scans in non \n? – nanobots Mar 24 '14 at 22:27
  • @user2948725 give a look at this answer: http://stackoverflow.com/a/7898516/3240801 – WileTheCoyot Mar 25 '14 at 10:03