Problem: Variable wird nach gets 0 gesetzt.
-
Ändere bei dem Text in Zeile 14 mal die 10 in 9 um. Dann bleibt noch platz für den Terminator
sword braucht noch einen Stringterminator '\0'
Für jeden falschen Buchstaben im Suchwort, wird dir ein Lebenspunkt abgezogen. (pro Eingabe)
if (input[0]==word[k]) { .... } else if(input[0]!=word[k]) // Das if (...) ist hier überflüssig
Denn du kommst an die Stelle nur, wenn input[0]!=word[k] wahr ist
(Das tritt dann ein, wenn input[0]==word[k] falsch ist)
-
Vielen Dank für eure schnelle Hilfe.
Ich habe eure Tips soweit angewendet. Leider macht der Lebenspunkte Zähler noch immer nicht das was er soll. Er zieht bei jeder, auch bei einer richtigen, Eingabe einen Punkt ab.#include <stdio.h> #include <strings.h> int getchar(void); int main() { unsigned long length; int i=0, k=0, l=0, n, t=0; int lifepoint=10; int input,input2;//creates string for the input char word [11]; //creats a string for the input word 10 chars maximum char sword [11]; //creats a string for the * word printf("Enter a word. (max 10 characters)\n"); for (i = 0; i < 10; i++) { input = getchar(); if (input == '\n') { for (i = i; i < 10; i++) { word[i] = '\0'; } break; } word[i] = input; } for(n = 0; n < 20; n++) //clears window { printf("\n\n\n\n\n\n\n\n\n\n\n\n\n"); } length=strlen(word); //counts the length of the word while (l<length) //prints for every char of the word a star { sword[l]='*'; l++; } sword[length]='\0'; printf("The word is %lu characters long. You got 10 attempts.\n",length); //prints the length of the word printf("\n"); printf("%s\n",sword); //prints the unsolved star word printf("\n"); while (strcmp(sword, word)!=0) { printf("Enter a character:"); input2=getchar(); printf("\n"); for (k=0; k<=length-1; k++) { if (input2==word[k]) { sword[k]=input2; t=1; } else t=0; } if (t==0) { lifepoint--; } t=0; printf("\n"); puts(sword); printf("%d tries left!\n",lifepoint); } printf("\n"); if (lifepoint>0) { printf("Great! You solved the word. =)\n"); } else { printf("You are out of lifes!\n"); } }
-
rstdd schrieb:
Vielen Dank für eure schnelle Hilfe.
Ich habe eure Tips soweit angewendet. Leider macht der Lebenspunkte Zähler noch immer nicht das was er soll. Er zieht bei jeder, auch bei einer richtigen, Eingabe einen Punkt ab.Was passiert denn, wenn ein Buchstabe richtig ist und die for-Schleife in Z. 61 noch nicht zu ende?
Ist
<strings.h>
von Dir, oder meinst Du<string.h>
?
-
Die for Schleife läuft nochmal durch und überspringt das getchar wodurch das t natürlich auf 0 gesetzt wird und somit ein leben abgezogen wird.
-
tdd schrieb:
Die for Schleife läuft nochmal durch und überspringt das getchar wodurch das t natürlich auf 0 gesetzt wird und somit ein leben abgezogen wird.
Das auch. ²
Aber das meinte Furble Wurble nicht.Mal angenommen du suchst das Wort foo und gibst ein f ein.
Dann kommst du an erster Stelle stimmen die Buchstaben überein.
Aber dann überprüfst du die zweite Stelle und da stimmt es nicht mehr.Du bekommst t = 0, obwohl das Zeichen richtig war.
Noch was:
Wofür ist den Zeile 23ff da?
for (i = i; i < 10; i++) { word[i] = '\0'; }
Vor allem mit der umgebenden Schleife
for (i = 0; i < 10; i++)
²Das getchar wird nicht übersprungen, es wird das '\n' von der Entertaste eingelesen.
-
Stimmt, das else t=0 macht keinen Sinn.
So funktioniert es, allerdings muss ich den Buchstaben zwei mal eingeben:
while (strcmp(sword, word)!=0 && lifepoint>0) { printf("Enter a character:"); while (getchar() != '\n' && getchar() != EOF) { input2 = getchar(); printf("\n"); for (k=0; k<=length-1; k++) { if (input2==word[k]) { sword[k]=input2; t=1; } } } if (t==0) { lifepoint--; } t=0; printf("\n"); puts(sword); printf("%d tries left!\n",lifepoint); } printf("\n");
-
Du rufst da jetzt bis Zeile 6 bis zu dreimal
getchar()
auf. Ist das sinnvoll?Und ein Zeichen in einem String suchen gibt es auch schon in
<string.h>
:strchr()
.
-
So ich möchte mich bei Allen für die freundliche Unterstützung danken.
Das Programm macht nun endlich das was es soll.
Viele Grüße
Ich habe es wie folgt gelöst:
while (strcmp(sword, word)!=0 && lifepoint>0) { printf("Enter a character:"); scanf("%d", &input2); input2 = getchar(); char *pos=strchr (word,input2);; if (pos) { while (pos!=NULL) { sword[pos-word]=word[pos-word]; pos=strchr(pos+1,input2); } } else { lifepoint--; } printf("\n"); puts(sword); printf("%d tries left!\n",lifepoint); }
-
SeppJ schrieb:
Daher ist es auch im aktuellen Standard als "deprecated" markiert, d.h. eventuell wird es in Zukunft gar kein gets mehr geben.
Der aktuelle Standard ist C11 und der sagt (zumindest der Committee Draft):
http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf schrieb:
— removed the gets function (<stdio.h>)
-
tddrs schrieb:
printf("Enter a character:"); scanf("%d", &input2); input2 = getchar();
Abenteuerlich. Und damit meine ich nicht nur die Einrückung. Welche Art von Datentyp liest du da wie?
if (pos) { while (pos!=NULL)
Sind die beiden Zeilen vom gleichen Programmierer?