Home » Developer & Programmer » Precompilers, OCI & OCCI » C program error
C program error [message #94575] Wed, 16 February 2005 22:28
Raj
Messages: 411
Registered: November 1998
Senior Member
HI all,
I am finding difficulty in a c program. Actually its a program which will calculate the percentage of an input parameter and add to the time converted to seconds.

eg > percent 00:08:00 30

here time is of HH:MI:SS format, so 8 * 60 = 480 secs.
30% of 480 is 144. not the result of the program is 624 (480 + 144).
now actually when the min is between 6 min and 00:55:32 min its giving wrong output. for other values the answer is correct.
if i add a printf anywhere in the program the output is correct for any combinations of parameter, bit confusing, hope i am clear in explaning. Thanks in advance.
here is the sample program
/*
****************************************************************************
** NOTE THIS IS THE CVS HEADER --> DO NOT CHANGE THIS SECTION
**
** $Revision: 1.1.1.1 $ $Author: jhoney $ $Date: 2002/11/05 23:09:22 $
**
** File History:
** $Log: percent.c,v $
** Revision 1.1.1.1 2002/11/05 23:09:22 jhoney
** Control M Project Source Code
**
** Revision 1.1.1.1 2002/10/31 05:01:59 jhoney
** ONP Monitor Project Source Code
**
****************************************************************************
** Program: percent.c
** Purpose: Takes three values and returns the percentage rounded up
** into the file supplied by the user
** Author: James Honey
** Date: 30/05/2002
**
** Input 1: Time Difference in Time Format {00:00:00} or Int Value
** Input 2: Percent
** Input 3: Output file
**
** Syntax: percent [[values {time/integar}]] percentage file
** Example 1: percent 00:00:20 30 percent.tmp
** will return time in seconds plus 30% which is 26 into the file
**
** Example 2: percent 100 30 percent.tmp
** will return value plus 30% which is 130 into the file
**
** To compile this program and produce and executable called percent use
** this command: gcc -o percent percent.c
****************************************************************************
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>

/* Function Prototype */
int rounding(double dbl_value);

main(int argc, char *argv[[]])
{
FILE *fp;
int time = 0;
int nbr_params = argc;
int i = 0;
int x = 0, y = 0, z = 0;
int param1_length = 0, param2_length = 0;
int hour_digit=0 , minute_digit=0 , second_digit=0 ;
int sub_total=0, time_sec_total=0;
int int_value=0;
char *prog_name;
char *argv_param1, *argv_param2;
char *output_file = NULL;
char *tmp_array;
char hour[[5]], minute[[5]], second[[5]];
double percent, dbl_value;
double param1, param2;

prog_name = argv[[0]];

/*printf( "value of argv[[0]] is %sn", argv[[0]]);
printf( "value of argv[[1]] is %sn", argv[[1]]);
printf( "value of argv[[2]] is %sn", argv[[2]]);
printf( "value of argv[[3]] is %sn", argv[[3]]);
*/
if (nbr_params == 4)
{
argv_param1 = argv[[1]];
argv_param2 = argv[[2]];
output_file = argv[[3]];
param1_length = strlen(argv_param1);

/* Check the first Parameter to determine if it is time or int */

/*printf( "value of argv_param1[[0]] is %cn", argv_param1[[0]]);
printf( "value of argv_param1[[1]] is %cn", argv_param1[[1]]);
printf( "value of argv_param1[[2]] is %cn", argv_param1[[2]]);
printf( "value of argv_param1[[3]] is %cn", argv_param1[[3]]);
printf( "value of argv_param1[[4]] is %cn", argv_param1[[4]]);
printf( "value of argv_param1[[5]] is %cn", argv_param1[[5]]);
printf( "value of argv_param1[[6]] is %cn", argv_param1[[6]]);
*/
if((argv_param1[[2]] == ':') || (argv_param1[[5]] == ':'))
{
time=1;
if (param1_length != 8 )
{
fprintf(stderr, "%s: argument(1) is not a valid length [[00:00:00]]n", prog_name);
exit (2);
}
}
else
{
param1_length = strlen(argv_param1);
for (i=0; i < param1_length; i++)
{
if (!isdigit(argv_param1[[i]]))
{
fprintf(stderr, "%s: argument(1) must be a Integar Valuen", prog_name);
exit (2);
}
}
param1 = atof(argv_param1);
}

/* Check the Second Parameter */
param2_length = strlen(argv_param2);
for (i=0; i < param2_length; i++)
{
if (!isdigit(argv_param2[[i]]))
{
fprintf(stderr, "%s: argument(2) must be a Integar Valuen", prog_name);
exit (2);
}
}
param2 = atof(argv_param2);
percent = param2 / 100;

if (time == 1)
{
/* Convert Time to hours minutes and seconds */
for (i=0; i < param1_length; i++)
{
if(isdigit(argv_param1[[i]]))
{
if(i <= 1)
{
hour[[x]]=argv_param1[[i]];
x++;
}

if(i == 3 || i == 4)
{
minute[[y]]=argv_param1[[i]];
y++;
}

if(i == 6 || i == 7)
{
second[[z]]=argv_param1[[i]];
z++;
}
}
}

hour_digit = atol(hour);
minute_digit = atol(minute);
second_digit = atol(second);
sub_total = (hour_digit * 3600) + (minute_digit * 60);
time_sec_total = sub_total + second_digit;
dbl_value = time_sec_total * percent;
int_value = rounding(dbl_value);
int_value = int_value + time_sec_total;
} /* if time */
else
{
dbl_value = param1 * percent;
int_value = rounding(dbl_value);
int_value = int_value + param1;
}

/* Open file and put string value in the file */
if ((fp = fopen(output_file, "w")) == NULL)
{
fprintf(stderr, "Cannot open file %sn", output_file);
exit (2);
}
else
{
tmp_array = realloc(tmp_array, sizeof(int_value));
sprintf(tmp_array, "%dn", int_value);
fputs(tmp_array, fp);
fclose(fp);
free(tmp_array);
}
} /* If Nbr of Params */
else
{
fprintf(stderr, "Usage: %s timediff percent filen", prog_name);
fprintf(stderr, " %s 00:00:20[[Timediff]] 10[[Percent]] file.out[[File]]n", prog_name);
exit(2);
}
exit (0);
}
/***************************************************************************
** This function rounds up to next int
***************************************************************************/
int rounding(double dbl_value)
{
char *tmp_array;
char *digit;
char value[[256]];
int length = 0;
int i = 0;
int round_value = 0;
int int_rtn_value = 0;

tmp_array = malloc(sizeof(dbl_value));
sprintf(tmp_array, "%.2fln", dbl_value);
length = strlen(tmp_array);
tmp_array[[length]] = '';
for (i=0; i < length; i++)
{
if(tmp_array[[i]] == '.' )
{
digit = malloc(2);
digit[[0]] = tmp_array[[i+1]];
digit[[1]] = '';
round_value = atol(digit);
break;
}
else
value[[i]] = tmp_array[[i]];
}

/* Round the value up to next whole number */
if (round_value >= 5 )
{
int_rtn_value = atol(value);
int_rtn_value++;
}
else
{
int_rtn_value = atol(value);
}
return int_rtn_value;
}
Previous Topic: Plz suggest me about the steps of creating Pro*C to VC+
Next Topic: Where can I download pro*C 8.0.5 for windows?
Goto Forum:
  


Current Time: Fri Apr 19 20:39:13 CDT 2024