count digits in user input

Oct 5, 2012 at 8:02pm
My English is very bad but I hope you can understand me!
Furthermore: I have never programmed before and this year I started with a study where programming is essential.

Hello readers,

For a homework assignment I have to make a program that counts digits and other charachters from the keyboard (what the user types in) and I have to make use of the function getchar until a EOF occurs.
Furthermore I have to use the variables "Digits" and "Others" to count the inputted digits and other characters.
Also I am not allowed to use strings. I can only use getchar and putchar and loops.
To make sure if it is a digit or a other character I have to use the function isdigit in the ctype header.

This is my program so far:

#include <stdio.h>
#include <ctype.h>

int main()
{

int digit;
int other;
int c;

while((c=getchar()) !=EOF)
if(isdigit(c))
putchar(c);
}

I dont know how to go further....
I tried very many things but nothing worked I only got weird charachters like smileys and other stuff...
I hope someone can help me!!

Dutchman
Last edited on Oct 7, 2012 at 11:49am
Oct 5, 2012 at 8:38pm
when you read characters from anywere you have to store them in character data types ofcourse.
so you need
char c;
Oct 6, 2012 at 8:31pm
So I need to replace int c for char c?
And how do I go further?
Oct 10, 2012 at 2:36pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>
#include <ctype.h>

int main()
{
  int digit = 0;
  int other = 0;
  char c;

  while((c=getchar()) !=EOF)
  {
    if(isdigit(c))
    {
       putchar(c);
       digit ++;
    }
    else
      other++;
  }
}

this stores the number of digits in the variable digit and the number of other chars in the variable other, becourse everytime isdigit(c) returns true the number of digits is incremented by one.
i think you should be able to gues the rest.
Oct 10, 2012 at 2:40pm
Yes I got it! Thank you!
Topic archived. No new replies allowed.