0

the output window commandI am trying to use switch case in C to figure out the amount of characters, words, newlines in a user input. The code seems legit, no errors raised, however, the output does not work as expected. Please take a look and tell me what I did wrong. Thanks in advance! Here is the code:

#include <stdio.h>
int main()
{
    char a, words = 1, characters = 0, newlines = 0;
    printf("What do you have in mind? ");
    a = getchar();

    while ((a=getchar()) && a != EOF)
    {
        switch (a)
        {
        case '1':
            if (a >= 'a' && a <= 'z' || a >= 'A' && a <= 'Z')
                characters++;
            printf("The amount of character is %c ", characters); 
        
        case '2':
            if (a == ' ' || a == '\t')
                words++;
            printf("The amount of word is %c ", words); 

        case '3':
            if (a == '\t')
                newlines++;
            printf("The amount of newlines is %c ", newlines); 
        default:
            if (a == EOF)
                break;
            
        }
    }
    
    return 0;
}
Calvin Lee
  • 11
  • 2
  • 1
    Welcome to stackoverflow! In order to help, we need a better understanding of your problem. Saying "the output does not work as expected" is too vague. Please show example input, expected output, and actual output. – Ryan Haining Jul 01 '20 at 01:16
  • 1
    Step through the code in a debugger to see why it's not working as you expect. The debugger is an excellent tool to figure out errors in your program logic, and it's never too early to learn to use it. – Ken White Jul 01 '20 at 01:17
  • 1
    One problem: [getchar](https://linux.die.net/man/3/getchar) returns an `int`. So `a` needs to be an `int` otherwise the comparison with `EOF` will not work as expected. – kaylum Jul 01 '20 at 01:19
  • Another problem: `%c` will print the value as a character and not a numeric value as you require. Use `%d` instead. – kaylum Jul 01 '20 at 01:24
  • @kaylum. getchar can be both int and char depends on the situation. I tried int a, ... but it is still not working. – Calvin Lee Jul 01 '20 at 01:25
  • @kaylum. Yes, tried int and %d – Calvin Lee Jul 01 '20 at 01:25
  • 1
    No you are wrong. The return value of `getchar` is always an `int`. If you want to compare it with `EOF` it must be assigned to an `int`. And I said that is **one** problem not the only problem. – kaylum Jul 01 '20 at 01:26
  • Did you press "enter" in your input? – kaylum Jul 01 '20 at 01:27
  • press enter to a new line. not return anything until control Z to exit the loop – Calvin Lee Jul 01 '20 at 01:29
  • @CalvinLee [Why **must** the variable used to hold getchar's return value be declared as int?](https://stackoverflow.com/q/18013167/995714) – phuclv Jul 01 '20 at 01:53

2 Answers2

0

you misunderstand what switch /case means. It does not means 'first case','second case' .. of some conditions, it means (in your specific situation), if the user just typed '1' then do this, it the users just typed '2' then do this,... well you are not typing 1 or 2 or 3.

Simply do this

while ((a=getchar()) && a != EOF)
{

        if (a >= 'a' && a <= 'z' || a >= 'A' && a <= 'Z')
            characters++;
        printf("The amount of character is %c ", characters); 
    
        if (a == ' ' || a == '\t')
            words++;
        printf("The amount of word is %c ", words); 

        if (a == '\t')
            newlines++;
        printf("The amount of newlines is %c ", newlines); 
}

Also you must change a to be an int

pm100
  • 48,078
  • 23
  • 82
  • 145
  • The goal of the exercise is to count words, characters given in a string. How do I make them into cases? Like case ... ; case ... instead of case 1 case 2. VS tells case need to be a constant – Calvin Lee Jul 01 '20 at 01:33
  • @CalvinLee move the printfs to after the while. Take out the cases - just like I showwed – pm100 Jul 01 '20 at 01:48
  • Thank you for your input. That is one way to do it. My teacher asks me to use switch statement to do it – Calvin Lee Jul 01 '20 at 02:03
0
#include<stdio.h>

int
main ()
{
  int ch_count = 0, line_count = 0, word_count = 0, choice;
  char ch;

  FILE *fp;

  fp = fopen ("wordcount.txt", "r");    //make a seperate file "wordcount.txt" and Read the Test content from it.

  if (fp == NULL)       // Show error if previous step is not done i.e wordcount.txt file is not made.
    {
      perror ("FILE NOT FOUND.");
      return (-1);
    }
  else              // If file is opened properly then ask for NO. of counts.
    {
      printf ("Select the Following Option-------\n"
          "1 - Number of Characters\n"
          "2 - Number of Words\n" 
          "3 - Number of Lines\n");
      scanf ("%d", &choice);
    }

    {
      switch (choice)       // switch to desired case as per choice of user.
    {
    case 1:     // CASE 1 - To count the characters in the file.
      {
        while ((ch = fgetc (fp)) && ch != EOF)

          if (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z')
        ch_count++;
      }
      printf ("Number of Characters : %d\n", ch_count);
      break;

    case 2:     // CASE 2: To count total number of words
      while ((ch = fgetc (fp)) && ch != EOF)

        if ((ch == ' ') || (ch == '\t') || (ch == '\n') || (ch == '\0'))
          {
        word_count++;
          }
      printf ("Numbers of Words : %d\n", word_count);
      break;

    case 3:     // CASE 3: To count total number of lines
      while ((ch = fgetc (fp)) && ch != EOF)

        if ((ch == '\n') || (ch == '\0'))
          {
        line_count++;
          }
      printf ("Numbers of lines : %d\n", line_count);
      break;
    }           //switch closed
    }
  fclose (fp);          //file closed
  return 0;
}