Thread: Looping problem

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    35

    Looping problem

    Where am I going wrong?
    I am reading a file using gets() with a for loop and printing the string using strtok with a while() loop.
    Can anyone tell me where I am going wrong?

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
    	
    	char customerString[1047];
    	char *tokenPtr;
    	int k;
    	char ch;
    
    	printf("Login Id  User Id  Group Id  User Name  User Home               Directory   Starting Shell\n",
    	 "", "", "", "");
    	for( k = 0; k < 7; k++)
    	{
    		gets(customerString);
    	    
    	}
    	
    	tokenPtr = strtok(customerString, ":");
    
    	while ( tokenPtr != NULL)
    	{
    		printf("%s \t", tokenPtr);
    		tokenPtr = strtok(NULL, ":");
    	}
    		
    	ch = getchar();
    	
    	return 0;
    }//End main
    Example input:
    [code]
    ceva8710:x:1007:100:ahmadmateen:/home/students/ceva8710:/bin/sh
    [\code]
    Imagination at Work

  2. #2
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    Code:
    for( k = 0; k < 7; k++)
    	{
    		gets(customerString);
    	    
    	}
    I think that will only get the last input. You need to use strcat to append input to an array. Try that first. I'm not sure if the strtok is correct because I have not used it in a while.
    I compile code with:
    Visual Studio.NET beta2

  3. #3
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284

    Re: Looping problem

    for( k = 0; k < 7; k++)
    {
    gets(customerString);

    }
    the above code doesnt make sense , you are reading strings from the standard input seven times and storing them in the same buffer , only the last one stays stored . It is usually better to use fgets than gets .

    Originally posted by sketchit
    Where am I going wrong?
    I am reading a file using gets() with a for loop and printing the string using strtok with a while() loop.
    Can anyone tell me where I am going wrong?

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
    	
    	char customerString[1047];
    	char *tokenPtr;
    	int k;
    	char ch;
    
    	printf("Login Id  User Id  Group Id  User Name  User Home               Directory   Starting Shell\n",
    	 "", "", "", "");
    	for( k = 0; k < 7; k++)
    	{
    		gets(customerString);
    	    
    	}
    	
    	tokenPtr = strtok(customerString, ":");
    
    	while ( tokenPtr != NULL)
    	{
    		printf("%s \t", tokenPtr);
    		tokenPtr = strtok(NULL, ":");
    	}
    		
    	ch = getchar();
    	
    	return 0;
    }//End main
    Example input:
    [code]
    ceva8710:x:1007:100:ahmadmateen:/home/students/ceva8710:/bin/sh
    [\code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ problem
    By sami_chy in forum C++ Programming
    Replies: 2
    Last Post: 07-23-2005, 11:59 PM
  2. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 03:36 PM
  3. looping problem
    By chris285 in forum C++ Programming
    Replies: 4
    Last Post: 04-22-2005, 11:03 AM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM