count digits and others of user input

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. My teacher can'really help me because if you ask something he will make the programm for you and he can't explain what he did...

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.
If the user would put in like:
1a2b3c
The program would say: 3 digits and 3 others.....

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...
HELP ME!!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
   char c;

   ifstream inFile;
   inFile.open("digit.txt");

   cout << "Digits: ";
   while(inFile >> c)
   {
      if(isdigit(c))
         putchar(c);
   }

   inFile.close();
   inFile.open("digit.txt");

   cout << "\n\nOther: ";
   while (inFile >> c)
   {
       if (!isdigit(c))
         putchar(c);
   }
   
   return 0;
}
I can't count the characters from a other file.
I have to count digits and others from the user input.

So if I input:
1a2b3c4d66:
the program would say:

6 digits and 4 others
Last edited on
1
2
int digit;
int other;


should read
1
2
int digit = 0;
int other = 0;


instead of
1
2
if(isdigit(c))
         putchar(c);


have
1
2
3
4
if(isdigit(c))
         digit++;
    else
        other++


in the while loop then print these using cout
When you post code encase that in "[code]" tag
But when I do this:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>
#include <ctype.h>
 
int main()
 {
 
int digit=0;
 int other=0;
 int c;
 
while((c=getchar()) !=EOF)
 if(isdigit(c))
                   digit++;
else
                   other++
putchar(c);
putchar(digit);
putchar(other);



 }


My program output is nothing....
I think I don't clearly understand what you mean..
Topic archived. No new replies allowed.