Thread: C-program to count how many times each of the numbers have been typed

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    23

    C-program to count how many times each of the numbers have been typed

    I wrote a C-program to count how many times each of the numbers 0-4 have been typed. I am suppose to use (EOF) function as part of this assignment.

    Code:
    void main (){
    	int zero,one, two, three, four=0;
    	int other_char;
    
    
    	char x;
    
    
    	while (x=getchar() !=26 && x!=EOF);
    
    
    	{
    		switch(x)
    		{
    		case'0':
    			zero++; break;
    		case'1':
    			one++; break;
    		case'2':
    			two++; break;
    		case'3':
    			three++; break;
    		case'4':
    			four++; break;
    		default:
    			other_char; break;
    		}
    	}
    
    
    			printf("Number of zero's: %d\n", zero);
    			printf("Number of one's: %d\n",one);
    			printf("Number of two's: %d\n",two);
    			printf("Number of three's: %d\n", three);
    			printf("Number of four's: %d\n",four);
    			printf("Number of other characters: %d",other_char);
    
    
    }
    Can someone give me feedback on the code? After I press Ctrl+Z nothing happens on the output. It's only worked once after entering Ctrl+D then entering Ctrl + Z. When it worked it seemed like something is incorrect in code on how it counts the numbers on the output.

    Thanks in Advance!

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    EOF is an int. x just a char it cannot hold EOF.
    Make x an int.
    Kurt

    EDIT:

    also this line
    Code:
        while (x=getchar() !=26 && x!=EOF);
    won't do what you want

    try
    Code:
        while ( ( x = getchar() ) !=26  && x != EOF) // no semicolon here
    and you should initialize the counter variables
    Last edited by ZuK; 05-12-2013 at 01:38 PM.

  3. #3
    Registered User
    Join Date
    May 2013
    Posts
    23
    It's working expect it doesn't' seem to count the character correctly.

    Attachment 12736

    Isn't this initialization the variables?
    Code:
      int zero,one, two, three, four=0;
    I have changed it though
    Code:
     
        int zero=0;    
        int one=0;
        int two=0;
        int three=0;
        int four=0;
        int other_char;
    Last edited by Zach786; 05-12-2013 at 02:29 PM.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You initialized just four to zero
    use
    Code:
    int zero = 0, one = 0, two = 0, three = 0, four = 0;
    Kurt

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Looks OK now, except that you never do anything useful with other_char.

  6. #6
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    "void main" should be "int main", and you should return 0 at the end of your code
    Fact - Beethoven wrote his first symphony in C

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    What the heck is 26?

    (self-documenting-code failure)
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    What the heck is 26?

    (self-documenting-code failure)
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Darn it, browser pulled a fast one one me. I didn't post twice.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by iMalc View Post
    What the heck is 26?
    ctrl-Z, hex 1A. It's used as end of file character in DOS. (For unix / linux ... it's ctrl-D or 4).

  11. #11
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Change the 26 for a new line character '\n' and see what happens - Also, I think that what iMalc was alluding to was that you should define, say, CTRL_Z or CTRL_D as 26, so it is easy to understand what you are looking for.
    Fact - Beethoven wrote his first symphony in C

  12. #12
    Registered User
    Join Date
    May 2013
    Posts
    23
    I also initialized other_char=0 but it doesn't seem to count other characters correctly.
    Last edited by Zach786; 05-13-2013 at 08:00 PM.

  13. #13
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Show your current code
    Fact - Beethoven wrote his first symphony in C

  14. #14
    Registered User
    Join Date
    May 2013
    Posts
    23
    Code:
    # include void main (){	int zero=0;	int one=0;	int two=0;	int three=0;	int four=0;	int other_char=0;	int x;	while ((x=getchar()) !=26 && x!=EOF)	{		switch(x)		{		case'0':			zero++; break;		case'1':			one++; break;		case'2':			two++; break;		case'3':			three++; break;		case'4':			four++; break;		default:			other_char; break;		}	}			printf("Number of zero's: %d\n", zero);			printf("Number of one's: %d\n",one);			printf("Number of two's: %d\n",two);			printf("Number of three's: %d\n", three);			printf("Number of four's: %d\n",four);			printf("Number of other characters: %d\n",other_char);}

  15. #15
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
        default:
            other_char;   // statement without effect use ++other_char; 
            break;
    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program to count how many of each letter has been typed
    By TaylorEdgell in forum C Programming
    Replies: 15
    Last Post: 06-12-2012, 09:45 AM
  2. Help: count even and odd numbers
    By taigaicon in forum C Programming
    Replies: 1
    Last Post: 03-11-2011, 02:19 PM
  3. Replies: 2
    Last Post: 07-28-2010, 02:07 PM
  4. Times function for rational numbers
    By CaliJoe in forum C++ Programming
    Replies: 1
    Last Post: 05-01-2009, 04:09 PM
  5. Functions Taking Dif. Numbers of Arguments at Dif. Times?
    By bengreenwood in forum C++ Programming
    Replies: 6
    Last Post: 03-23-2009, 04:12 PM