I am brand new to programming (2 weeks strong ). I have to use a switch statement to output characters, lines and words. I have been using the same if statements for th assignment (while, do-while) without any issue.
Can someone help point me in the correct direction. When I run the code I enter text and if I hit enter it immediately breaks and prints output - expect it does not increment anything. All results are 0.
If I type the word 'test' in the other loops hitting enter would bring me to a new line to continue entering text. Now it just breaks and terminates. I tried removing the break; after each case but still get the same result.
I appreciate the help.
Code:
#include <stdio.h>
#include <ctype.h>
#define IN 1
#define OUT 0
int main()
{
int c, line_count, word_count, character_count, state, choice;
state = OUT;
line_count = word_count = character_count = 0;
c = getchar();
switch (c)
{
if(isalpha(c) && c != ' ' && c != '\n')
choice = 1;
case '1':
++character_count;
break;
if(c == ' ' || c == '\n' || c == '\t')
state = OUT;
else if(isalpha(c) && state == OUT)
{
state = IN;
}
choice = 2;
case '2':
++ word_count;
break;
case '\n':
++line_count;
break;
default:
if(c == '4')
break;
}
getchar();
printf("Lines:%d\nWords:%d\nChraracters:%d\n", line_count, word_count, character_count);
}