Hey,
I want to use scanf (or something similar) to put the full sentence in one single variable (char variable).
I know you can do this with gets, but for some reason in this program that doesn't work at all.
Can someone help me, thanks in advance.
This is a discussion on Reading an unknown numbers of words within the C Programming forums, part of the General Programming Boards category; Hey, I want to use scanf (or something similar) to put the full sentence in one single variable (char variable). ...
Hey,
I want to use scanf (or something similar) to put the full sentence in one single variable (char variable).
I know you can do this with gets, but for some reason in this program that doesn't work at all.
Can someone help me, thanks in advance.
Well unless you post the program your guess as to why it doesn't work is as good as ours. Although I do suspect that the use of gets() which should be avoided like the plague, combined with trying to store the input in a "char variable" might have something to do with it.
Scanf should also be avoided when reading in strings, particularly when they are of unknown length. The f in scanf stands for format.
1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
3. Get rid of conio.h and other antiquated DOS crap headers.
4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.
This is the code i'm using. I'm sorry the code is mostly written in dutch.
I'd put some !!!! at the place where I want to read a string (a question in type in stdin) with gets() or something similar.
Code:#include "bindtree.h" #include <stdio.h> Bdtnode daal_af(Bdtnode data); void main(void){ int x; char antwoord[MAX_LENGTH]; int juistantwoord; char hulp; char dier[MAX_LENGTH]; /* dit is een string, dus van de vorm char* */ char vraag[MAX_LENGTH]; Bdtnode daling; x=1; FILE* animal = fopen("/Users/kevinkenis/Desktop/School/2010-2011/Programmeren/Project programmeren/dieren.txt","r"); if(animal==NULL) printf("verplaats bestand naar map waarin je werkt!"); /*let op dat je het txt bestand kunt openen! */ else{ Bdtree animals = fread_tree(animal); while (x==1){ daling = daal_af(animals); if (is_leaf(daling)== 1){ printf("Was dit het dier dat je in gedachten had?\n"); scanf("%s",antwoord); if (antwoord[0] == 'j' && antwoord[1]== 'a' && antwoord[2] == '\0'){ printf("Ben ik niet geniaal!"); /*Goed zo, het spel is gedaan, je kan opnieuw beginnen*/ x=0; /*while lus wordt beëindigd*/ } else{ printf("Welk dier had je dan wel in gedachten?\n"); scanf("%s",dier); printf("Geef een vraag waarmee ik dit dier kan onderscheiden:\n"); gets(vraag); // !!!!!!! Here does it go wrong!!!!!!!! printf("Wat is het antwoord op '%s' om '%s' te bekomen?\n, 0/1",vraag, dier); scanf("%d",&juistantwoord); } } else printf("probleem"); } insert_new_item(animals,animals,dier,vraag,juistantwoord); free_tree(animals); fclose(animal); } } Bdtnode daal_af(Bdtnode node){ char antwoord[MAX_LENGTH]; /*naamgeving voor afdaling boom*/ printf("%s\n",node->data); if(is_leaf(node)== 0){ scanf("%s",antwoord); if(antwoord[0] =='j' && antwoord[1] =='a' && antwoord[2] == '\0'){ return daal_af(node->leftchild); } else{ return daal_af(node->rightchild); } } return node; }
Learn how to use fgets() (google for it) and do all the input using that. Drop scanf, and especially drop gets(). NEVER NEVER use gets.
Also main should be int main(void) and should return 0.
1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
3. Get rid of conio.h and other antiquated DOS crap headers.
4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.