Hello, I am trying to finish an assignment that reads input as a stream of characters and prints it as a decimal number followed by the ASCII code. If the code comes prior to the "space" it needs to print the control character notation.
I am having problems printing some ot the codes for characters 8-20. Mainly 8 (BS), and 13 (CR). On 13 it prints 10 (LF).
Any ideas where I'm going wrong ("pulling my few remaining hairs out!")?
Here is my code:
Code:
#include <stdio.h>
#include <ctype.h>
int main(void)
{
int ch;
int chcount = 0; //character count
printf("Please enter your characters: \n");
while ((ch = getchar()) != EOF)
{
if (chcount >= 8){
printf("\n");
chcount = 0;
}
if (isprint(ch)){
printf("%d = %c, " , ch, ch);
chcount++;
}
else if(ch == 9){
chcount++;
printf("%d = Horizontal Tab, ", ch);
}
else if(ch == 10){
chcount++;
printf("%d = Newline, ", ch);
}
else if(ch == 13){
chcount++;
printf("%d = Carriage Return, ", ch);
}
else if(iscntrl(ch)){
chcount++;
printf("%d = ^%c, ", ch, ch+64);
}
else if(isspace(ch)){
chcount++;
printf("%d = ^%c, ", ch, ch+64);
}
}
printf("program has ended\n");
getchar();
return 0;
}