Also, was das Programm ist, schreiben Sie einen Tag von Ihrer Tastatur und er validiert es. Ihre Eingabe kann Klein- oder Großbuchstaben sein. Das Problem ist, wenn ich es Tage donnerstags gebe. Sie können sehen, dass Contor seltsame Werte erhält. length_arr ist die Funktion, die ich verwende, um die Länge des Arrays zu messen. Irgendwelche Vorschläge?C Programmierung, char * Länge kann nicht die ganze Array abdecken
int length_arr(char* x)
{
int i, length;
for (i = 0; x[i] != '\0'; i++) {
}
return length = i - 1;
}
void valid_weekday(char* day)
{
bool incorrect = true;
char *monday = "monday", *tuesday = "tuesday", *wednesday = "wednesday", *thursday = "thursday", *friday = "friday", *saturday = "saturday", *sunday = "sunday";
while (incorrect) {
int contor1 = 0, contor2 = 0, contor3 = 0, contor4 = 0, contor5 = 0, contor6 = 0, contor7 = 0;
//printf("NOTE: Weekday can contain uppercase letters. Why? Because reasons.\n\n");
printf("Enter a valid weekday, please!:\n");
fgets(day, 20, stdin);
for (int i = 0; i < length_arr(day); i++) {
if (day[i] == monday[i] || (int)monday[i] == (int)day[i] + 32) {
contor1++;
//.. To understand where i stops.
printf("%c\n", day[i]);
}
else if (day[i] == tuesday[i] || (int)tuesday[i] == (int)day[i] + 32)
contor2++;
else if (day[i] == wednesday[i] || (int)wednesday[i] == (int)day[i] + 32)
contor3++;
else if (day[i] == thursday[i] || (int)thursday[i] == (int)day[i] + 32)
contor4++;
else if (day[i] == friday[i] || (int)friday[i] == ((int)day[i] + 32)) {
contor5++;
// ... To understand where i stops. (i stops at 2)
printf(" day %c\n", day[i]);
printf(" friday %c\n", friday[i]);
}
else if (day[i] == saturday[i] || (int)saturday[i] == (int)day[i] + 32)
contor6++;
else if (day[i] == sunday[i] || (int)sunday[i] == (int)day[i] + 32)
contor7++;
}
if (contor1 == length_arr(day) || contor2 == length_arr(day) || contor3 == length_arr(day) || contor4 == length_arr(day) || contor5 == length_arr(day) || contor6 == length_arr(day) || contor7 == length_arr(day)) {
printf("Good, you entered good.\n");
incorrect = false;
}
else
printf("Wrong day format!\n");
// Tests to understand why other contor get value too
int a = (int)day[0] - 32;
printf("%d\n", (int)day[3]);
printf("%d\n", a);
printf("%d\n", (int)friday[3]);
printf("%d\n", contor1);
printf("%d\n", contor2);
printf("%d\n", contor3);
printf("%d\n", contor4);
printf("%d\n", contor5);
printf("%d\n", contor6);
printf("%d\n", contor7);
if ((int)monday[0] == a)
printf("one");
else
printf("two");
}
}
void main()
{
char* day;
day = new char(20);
valid_weekday(day);
delete (day);
}
Ihre Funktion length_arr ist falsch, die Länge der Zeichenfolge ist nur "i"; nicht "ich - 1". Sie können [strlen] (http://www.tutorialspoint.com/c_standard_library/c_function_strlen.htm) zum Bestimmen der Länge einer Zeichenfolge verwenden. Auch das ist eine seltsame Art, Zeichenkettenvergleiche durchzuführen. Suchen Sie nach [strcmp] (http://linux.die.net/man/3/strcmp) oder nach case insensitive comparison [strcasecmp] (http://linux.die.net/man/3/strcasecmp). –
ist das c oder C++ Code? Warum benutzen Sie nicht 'strlen'? – t0mm13b
@ t0mm13b: "* ist das c oder C++ code? *" Warum fragst du? Es gibt kein C++ - Tag, und im Code ist nichts C++ - spezifisch. –