Thread: Reading an unknown numbers of words

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    2

    Reading an unknown numbers of words

    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.

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    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.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    2
    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;
    }

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading numbers from console
    By gerrard in forum C Programming
    Replies: 3
    Last Post: 10-23-2009, 04:14 AM
  2. Reading in an unknown number of numbers
    By cboard_member in forum C++ Programming
    Replies: 3
    Last Post: 02-01-2006, 04:21 AM
  3. Beginners Contest #2 For those who wanted more!!
    By ILoveVectors in forum Contests Board
    Replies: 16
    Last Post: 08-12-2005, 12:03 AM
  4. converting numbers into words
    By Kozam in forum C Programming
    Replies: 2
    Last Post: 09-30-2003, 07:49 AM