What is wrong with my sscanf function?

This is a discussion on What is wrong with my sscanf function? within the C Programming forums, part of the General Programming Boards category; This is a small part of my program. The sscanf is creating run time errors. Could you please tell me ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    16

    What is wrong with my sscanf function?

    This is a small part of my program. The sscanf is creating run time errors. Could you please tell me what i'm doing wrong and how t o correct it?
    thanks


    paren_ptr = strchr (line, '(');
    age_ptr = paren_ptr + 1;
    sscanf (age_ptr, "%d", &person.age);

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1) Post more code.
    2) Post your error.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,659
    Is age_ptr NULL terminated? Does paren_ptr + 1 put you past the end of the string?

    gg

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    16

    %lf?

    Code:
    int main()                          
    
    
    {
    	char line[200];
    	int icount = 0;
    	age_record females[100];
    	age_record  males[100];
    	runner_t person;
    	
    	
    	FILE *infile;
    	FILE *outfile;
    
    //	connecting the file pointers to the actual files.
    
    	infile = fopen("marathon_txt.txt","r");
    	outfile = fopen("copy.txt","w");
    
    
    /*	Read the entire data file
        fgets reads a line up to and including \n, returns NULL if error 
        or it encountered the end-of-file.  */
    	fgets(line,199,infile);
    	fgets(line,199,infile);
    
    	while (fgets(line,199,infile) != NULL)
    	{
    		if (not_blank(line) == 1)
    		{
    			icount++;
    			person = creat_record(line,icount);
    			printf("%d%s\n",icount,line);
    			fprintf(outfile,"%d%\n",icount,line);
    			if (person.gender =='F')
    			{
    				females[person.age].sumt+= person.off_time;
    				females[person.age].sumt2+= person.off_time * person.off_time;
    			}
    			if (person.gender == 'M')
    			{
    			
    				males[person.age].sumt+= person.off_time;
    				males[person.age].sumt+= person.off_time * person.off_time;
    			}
    			///printf("%lf", person.off_time);
    			//getch();
    		}
    	} 
    
    	printf("The program read %d lines \n",icount);
    	printf ("There are %d runners \n", icount);
    
    	fclose(infile);
    	fclose(outfile);
    	
    	return 0;
    
    }
    
     
    
    //Not_Blank function takes out the spaces as the file is being read.
    
    int not_blank(char * line)
    {
    	while (*line != '\0')
    	{
    		if (*line !=' ' && *line != '\t' && *line != '\n') return 1;
    		++line;
    	}
    	return 0;
    }   
    
    
    runner_t creat_record(char line[], int icount)
    {
    
    	runner_t person;
    	double hours; 
    	double mins;
    	double secs;
    	char *paren_ptr = '\0';
    	char paren ='(';
    	
    
    	if(line[6] == 'F')
    	{
    		person.gender = 'F';
    	}
    	else
    	{
    		person.gender = 'M';
    	}
    
       /* This part of the fuction puts the age into the runner_t struct.*/
    
    	paren_ptr = strchr (line, paren);
    	if ( paren_ptr != NULL ) 
    	{
    		sscanf ( paren_ptr+1, "%d", &person.age);
    	} 
    	else 
    	{
    		fprintf( stderr, "Panic!!\n" );
    		exit( 1 );
    	}
    
    
       /*This part of the fuction puts the calls a fuction that converts the time and puts into the 
       struct as minutes*/ 
    
    	sscanf (line + 60,"%d:%d:%d", &hours, &mins, &secs);
    	person.off_time = (60.0 * hours + secs/60.0 + mins);
    	printf("%d", person.off_time);
    
    	return person;
    
    }
    This code is suppposed to scanf data from a file and pick out specific things. However when scan in the time , which is in the form 2:45:23, hours mins and seconds, convert it to minutes and print them out using %d...I get a rounded form which is right, but the same figures don't appear when I debug it.

    Then when I use %lf... It prints out 0.00...rather than the correct converted values. How to I solve this problem? What am I doing wrong?


    [code][/code]tagged by Salem

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    16

    How do I calculate standard deviation.

    Code:
    .
    #include <stdio.h>
    #include <iostream.h>
    #include <fstream.h>
    #include <string.h>
    #include <conio.h>
    
    typedef struct {
    	int age;
    	double off_time;
    	char gender;
    } runner_t;
    
    typedef struct {
    	int N;
    	double sumt;
    	double sumt2;
    	double mean;
    	double SD;
    } age_record_t;
    
    /*typedef struct {
    	int age[60];
    } male_t;*/
    
    /* Declaring Prototypes.*/
    int not_blank (char * line);
    runner_t creat_record(char line[], int icount);
    
    
    int main()                          
    
    
    {
    	char line[200];
    	int icount = 0;
    	age_record_t fandm; 
    	fandm females[100];
    	fandm males[100];
    	runner_t person;
    	
    	
    	FILE *infile;
    	FILE *outfile;
    
    //	connecting the file pointers to the actual files.
    
    	infile = fopen("marathon_txt.txt","r");
    	outfile = fopen("copy.txt","w");
    
    
    /*	Read the entire data file
        fgets reads a line up to and including \n, returns NULL if error 
        or it encountered the end-of-file.  */
    	fgets(line,199,infile);
    	fgets(line,199,infile);
    
    	while (fgets(line,199,infile) != NULL)
    	{
    		if (not_blank(line) == 1)
    		{
    			
    			icount++;
    			person = creat_record(line,icount);
    			printf("%d%s\n",icount,line);
    			//getch();
    			fprintf(outfile,"%d%s\n",icount,line);
    			if (person.gender =='F')
    			{
    				females[person.age].sumt+= person.off_time;
    				females[person.age].sumt2+= person.off_time * person.off_time;
    				females[person.age].N++
    		
    			}
    			if (person.gender == 'M')
    			{
    			
    				males[person.age].sumt+= person.off_time;
    				males[person.age].sumt2+= person.off_time * person.off_time;
    				males[person.age].N++;
    			}
    			
    		}
    	} 
    
    	printf("The program read %d lines \n",icount);
    	printf ("There are %d runners \n", icount);
    
    	fclose(infile);
    	fclose(outfile);
    	
    	return 0;
    
    }
    
     
    
    //Not_Blank function takes out the spaces as the file is being read.
    
    int not_blank(char * line)
    {
    	while (*line != '\0')
    	{
    		if (*line !=' ' && *line != '\t' && *line != '\n') return 1;
    		++line;
    	}
    	return 0;
    }   
    
    
    runner_t creat_record(char line[], int icount)
    {
    
    	runner_t person;
    	double hours = 0.0; 
    	double mins = 0.0;
    	double secs = 0.0;
    	char *paren_ptr = '\0';
    	char paren ='(';
    	int fcount = 0;
    	int mcount = 0;
    	
    
    	if(line[6] == 'F')
    	{
    		person.gender = 'F';
    		fcount ++;
    	}
    	else
    	{
    		person.gender = 'M';
    		mcount ++;
    	}
    
       /* This part of the fuction puts the age into the runner_t struct.*/
    
    	
    	paren_ptr = strchr (line, paren);
    	if ( paren_ptr != NULL ) 
    	{
    		sscanf ( paren_ptr+1, "%d", &person.age);
    	} 
    	else 
    	{
    		fprintf( stderr, "Panic!!\n" );
    		exit( 1 );
    	}
    
    
       /*This part of the fuction puts the calls a fuction that converts the time and puts into the 
       struct as minutes*/ 
    
    	sscanf (line + 60,"%lf:%lf:%lf", &hours, &mins, &secs);
    	person.off_time = (60.0 * hours + secs/60.0 + mins);
    	//printf("%.2lf", person.off_time);
    
    	return person;
    
    }
    .

    In this program , the creat record function, returns the age time and gender of every runner. The main fuction reads lines of text from a file, as the data is returned from the data-extractin function, the results are supposed to be added to an array of different structs , arrayed by age. This array shd be of type age-record tight?
    Am I declaring the arrays right? I don't think I am since I get an error message:
    missising ; before identifier females ...and males.

    Also once I get these numbers, how do I compute the standard deviation of their times... for all the ages? ..Go through the array twice... making sure that there is valid data(N>=2)?

    In the end I'll print the age, gender , mean and standard deviation to the screen.
    Last edited by doampofo; 03-09-2003 at 09:51 PM.

  6. #6
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Please read this and edit your post.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. On the wrong track for a Function Prototype
    By Mohammed in forum C++ Programming
    Replies: 2
    Last Post: 08-01-2003, 01:16 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21