#I'm beginner | switch case and reading of char string problem

17 messages · Page 1 of 1 (latest)

wild hornet
#

Hello,

Im new to coding sorry,
The problem is that if I enter a string greater than the specified LENGTH, in the switch case function leads to the default which should not. But a few exercises before it was said to use it that way until I added a few new cases to the switch case function.
I write the expected output in new message due to limits.

Code:
`#include<stdio.h>

#define LENGTH 20

void readChar(char string[], int length);
void printP(char string[]);
void printA(char string[], int length);
int findChar(char string[],char a[]);

int main(void) {
char String0[LENGTH]="";
char String1[LENGTH]="";
char a[1]="";
do {
printf("What is the command? ");
readChar(String0, LENGTH);
switch (String0[0]) {
case 'q':
printf("End!!!\n");
break;
case 'p':
printf("The current string is:");
printP(String1);
break;
case 'a':
printA(String1, LENGTH);
break;
case 'e':
printf("String? ");
readChar(String1, LENGTH);
break;
case 'f':
printf("Which character are you looking for? ");
readChar(a, 1);
int k=findChar(String1, a);
printf("The index when '%c' first appears is %d \n",a[0],k);
break;
default:
printf("This is unknown! '%c'\n", String0[0]);
break;
}
} while (String0[0] != 'q');
return 0;
}

void readChar(char string[], int length) {
int i;
for (i = 0; i <length; i++) {
char c;
c = getchar();
if (c == '\n') {
string[i] = '\0';
break;
}

else {
string[i] = c;
}
}
}

void printP(char string[]) {
for (int i = 0; string[i] != '\0'; i++) {
if (string[i] == '\n') {
putchar('\n');
} else {
putchar(string[i]);
}
}
putchar('\n');
}

void printA(char string[], int length) {
printf("The current array is: [");
for (int i = 0; i < length; i++) {
if (string[i] == '\0') {
printf("\0");
} else if (string[i] == '\n') {
printf("\n");
} else {
printf("%c", string[i]);
}
if (i != length - 1) {
printf(", ");
}
}
printf("]\n");
}
int findChar(char string[],char a[]){
int i;
for(i=0;i<LENGTH;i++){
if(string[i]==a[0]){
return (i);
break;
}
else
continue;
}
if(i==LENGTH)
return -1;
}`

faint stag
#

use code block please

wild hornet
#

hm, i cant use tab to make code more readable because of character limit.

#

If the input is like:
e
testtesttest
a
p
e
testtesttesttest12345678
p
e
test
f
e
f
t
f
a

The output should be like:
What is the command? e
String? testtesttest
What is the command? a
The current array is: [t,e,s,t,t,e,s,t,t,e,s,t,\0,\0,\0,\0,\0,\0,\0,\0]
What is the command? p
The current string is: "testtesttest"
What is the command? e
String? testtesttesttesttesttest
The current string is: "testtesttesttest1234"
What is the command? e
String? test
What is the command? f
Which character are you looking for? e
The index when 'e' first appears is 1
What is the command? f
Which character are you looking for? t
The index when 't' first appears is 0
What is the command? f
Which character are you looking for? a
The index when 'a' first appears is -1

faint stag
#

with .c extension

wild hornet
#

i will look into the extension (nevermind it does it automatically)

faint stag
#

what is the input that is producing the undesired result?

wild hornet
#

if the input is greater than the specified length, here it is 20.

#

just any input thats greater than 20 characters and it goes back to the default in the switch case function

wild hornet
faint stag
#

hmmm, i don't see why it would do that

wild hornet
#

depending on how many and what characters are above the specified LENGTH, i sometimes get multiple of those messages

faint stag
#

oh

wild hornet
#

i tried ina few compilers too

faint stag
#

when you write a command line like abcdefg then enter newline. Let's say LENGTH=4. You will read abcd and command will run. Next loop iteration defg are still in the stdin. Your program then will read defg next. To avoid this, have readChar call getchar until it gets to the '\n, regardless if it has enough space for the whole line.

wild hornet
#

ok thanks, i will try to do that ^^