-4

I am currently trying to count the digits of a number using getchar().

If i do it with getchar (condition to not count the point or comma) and I put a number like 345.234 does it count 6 like 3-4-5-2-3-4 or does it count 4 like 34-5-23-4?

i=0
while((c=getchar())!=',' && c!=EOF)
i++;

SIMPLE QUESTION HOW MUCH DOES IT COUNT 345.234 and ctrl+z on the input all at once Does it count 4 or does it count 6

Lind
  • 277
  • 2
  • 5
  • 16

2 Answers2

1

i will be incremented for each call to getchar() as long as not EOF or ',' is returned.

If you enter 345.234 and then hit Ctrl-Z this would lead to a value of 7 for i when leaving the while loop.

3 numbers + 1 dot + 3 numbers = 7 characters.

alk
  • 69,737
  • 10
  • 105
  • 255
  • So it doesnt get the 34,5,23,4.Ok thats what I wanted to know. – Lind Mar 31 '13 at 16:35
  • @Lind: Again, just out of curiosity: How do you get the idea it could count two characters as one? – alk Mar 31 '13 at 16:37
  • since a char can take up to 127,I thought it might get 34 instead of 3,4.Seems I was wrong. – Lind Mar 31 '13 at 18:39
  • The characters you enter are represented by numbers, even the numbers themself are nothing more then characters. Also any other sign is treated the same way. Each character has a code between 0 and 127. `'0'` is 48, `'1'` is 49, ... `'A'`is 65, `'B` is 66, ..., `'.' ` is 46. This code is read and stored in the variable `c` of your source. – alk Mar 31 '13 at 18:44
0

Maybe

#include <stdio.h>

int main(){
    int i=0,c;
    while(EOF!=(c=getchar())){
        if(c != ',' && c != '.' && c!= '\n')
            ++i;
    }
    printf("number count is %d\n", i);
    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70