type casting

R

ram.ragu

here i can't typecast the value of character into double .. please
discuss the reason and solution..
/*finding the difference between job render times*/
#include <stdio.h>
#include<ctype.h>
#include<time.h>
#define LINE_LENGTH 1000
double fuction(double hour,double minute,double second);
void printline();
main()
{
char buf[LINE_LENGTH];char dummy[50],dates[11],times[9],fname[40];
int day,month,year,jobid;
int hour,minute,second;/*scan value in int then typecast into double*/
double r[100],count=0;int i=0,j[100],id=0;
FILE *ptr;
clrscr();
printf("enter the logfile name:");
gets(fname);
ptr = fopen(fname,"r");
/*freopen("myfile.txt","w",stdout);IF JOBID MORE THAN 10 USE THIS TO
GET O/P IN TEXTFILE*/
if (ptr==NULL)
{
printf("can't open file name %s",fname);
exit();
}
else
{
printline();
while (feof(ptr) == 0)
{
fgets(buf,LINE_LENGTH,ptr); /* Read next record*/
if(isdigit(buf[6]))
{
sscanf(buf,"%d %s %s %s %s %s %s",
&jobid,dummy,dummy,dummy,dummy,dates,times);
printf("%d %s %s",jobid,dates,times);
j[id]=jobid;id++;
sscanf(times,"%d:%d:%d",&hour,&minute,&second);
printf("\t
totalsec:%5.0lf\n",r[count]=function((double)hour,(double)minute,(double)se­cond));

count++;
}
else{
continue;}
}



}


printf("----------------------------------\n");
printf("TIME DIFFERENCE BETWEEN TWO JOBS\n");
printf("----------------------------------");
for(i=0,id=0;i<count-1;i++,id++){
printf("\ndiff in time between job id %d and %d is %5.0lf
sec",j[id+1],j[id],(r[i+1]-r));}
fclose(ptr);
fclose(stdout);
}

double function(double hour,double minute,double second)
{
double s1,s2,t;
s1=(double)hour*3600;
s2=(double)minute*60;
t=(double)second+s1+s2;
return (t);


}


void printline()
{
printf("-------------------------\n");
printf("JOBID\tDATE\tTIME\tTOTAL\n");
printf("-------------------------\n");

}
 
R

Richard Heathfield

(e-mail address removed) said:
here i can't typecast the value of character into double .. please
discuss the reason and solution..
/*finding the difference between job render times*/
#include <stdio.h>
#include<ctype.h>
#include<time.h>
#define LINE_LENGTH 1000
double fuction(double hour,double minute,double second);
void printline();
main()

Bettr: int main(void)
{
char buf[LINE_LENGTH];char dummy[50],dates[11],times[9],fname[40];
int day,month,year,jobid;
int hour,minute,second;/*scan value in int then typecast into double*/
double r[100],count=0;int i=0,j[100],id=0;
FILE *ptr;
clrscr();

You have no prototype in scope for this non-standard function that you
probably don't need.
printf("enter the logfile name:");
gets(fname);

If you haven't yet learned not to use gets(), casting is way beyond your
current skill level. Basics first, advanced stuff later.
 
J

jj

here i can't typecast the value of character into double .. please
discuss the reason and solution..
/*finding the difference between job render times*/
#include <stdio.h>
#include<ctype.h>
#include<time.h>
#define LINE_LENGTH 1000
double fuction(double hour,double minute,double second);
void printline();
main()
{
char buf[LINE_LENGTH];char dummy[50],dates[11],times[9],fname[40];
int day,month,year,jobid;
int hour,minute,second;/*scan value in int then typecast into double*/
double r[100],count=0;int i=0,j[100],id=0;
FILE *ptr;
clrscr();
printf("enter the logfile name:");
gets(fname);
ptr = fopen(fname,"r");
/*freopen("myfile.txt","w",stdout);IF JOBID MORE THAN 10 USE THIS TO
GET O/P IN TEXTFILE*/
if (ptr==NULL)
{
printf("can't open file name %s",fname);
exit();
}
else
{
printline();
while (feof(ptr) == 0)
{
fgets(buf,LINE_LENGTH,ptr); /* Read next record*/
if(isdigit(buf[6]))
{
sscanf(buf,"%d %s %s %s %s %s %s",
&jobid,dummy,dummy,dummy,dummy,dates,times);
printf("%d %s %s",jobid,dates,times);
j[id]=jobid;id++;
sscanf(times,"%d:%d:%d",&hour,&minute,&second);
printf("\t
totalsec:%5.0lf\n",r[count]=function((double)hour,(double)minute,(double)se­cond));

count++;
}
else{
continue;}
}



}


printf("----------------------------------\n");
printf("TIME DIFFERENCE BETWEEN TWO JOBS\n");
printf("----------------------------------");
for(i=0,id=0;i<count-1;i++,id++){
printf("\ndiff in time between job id %d and %d is %5.0lf
sec",j[id+1],j[id],(r[i+1]-r));}
fclose(ptr);
fclose(stdout);
}

double function(double hour,double minute,double second)
{
double s1,s2,t;
s1=(double)hour*3600;
s2=(double)minute*60;
t=(double)second+s1+s2;
return (t);


}


void printline()
{
printf("-------------------------\n");
printf("JOBID\tDATE\tTIME\tTOTAL\n");
printf("-------------------------\n");

}



Array descripotr is of type double. It should be constant ingtegral
value.
 
A

apoelstra

here i can't typecast the value of character into double .. please
discuss the reason and solution..
/*finding the difference between job render times*/
#include <stdio.h>
#include<ctype.h>
#include<time.h>
#define LINE_LENGTH 1000
double fuction(double hour,double minute,double second);
void printline();
main()
int main (void);
Figure out how to start a C program, and you'll get a lot better help.
{
char buf[LINE_LENGTH];char dummy[50],dates[11],times[9],fname[40];
int day,month,year,jobid;
int hour,minute,second;/*scan value in int then typecast into double*/
double r[100],count=0;int i=0,j[100],id=0;
FILE *ptr;
clrscr();
1) There's no prototype in scope.
2) This isn't standard, so we have no idea what it means.
3) If it means to clear the screen, you need to take another
program design course.
printf("enter the logfile name:");
gets(fname);
Never use gets()! http://www.c-faq.com
Fix all these problems and your formatting, and maybe we'll
help you.
 
R

Richard Heathfield

(e-mail address removed) said:
int main (void);
Figure out how to start a C program, and you'll get a lot better help.

You might want to lose that semicolon, Andrew.

<snip>
 
T

Thomas Lumley

here i can't typecast the value of character into double .. please
discuss the reason and solution..

You might want to say why you think your problem is "can't typecast the
value of character into double". None of the casts you include appear

necessary.
/*finding the difference between job render times*/
#include <stdio.h>
#include<ctype.h>
#include<time.h>
#define LINE_LENGTH 1000
double fuction(double hour,double minute,double second);
^^^^^^
Did you mean "function"?
double function(double hour,double minute,double second)
{
double s1,s2,t;
s1=(double)hour*3600;
s2=(double)minute*60;
t=(double)second+s1+s2;
return (t);


}

After getting rid of the mysterious clrscr() I get the following on
trying to compile

test.c: In function 'main':
test.c:21: warning: incompatible implicit declaration of built-in
function 'exit'
test.c:21: error: too few arguments to function 'exit'
This is because you didn't include stdlib.h to define exit(), and
because you didn't
give it the argument it needs

test.c:35: error: array subscript is not an integer
Now, a cast might actually help here, in a perverse sort of way, but
defining count as an int would make more sense

test.c: At top level:
test.c:55: error: conflicting types for 'function'
test.c:35: error: previous implicit declaration of 'function' was
here
and this is because of spelling.

Sometimes a compiler will give diagnostics about missing casts when you
are
actually missing a function definition (malloc() is the canonical
example), but I
don't know if that's your problem here.

And as everyone else said: don't use gets(). Ever.

-thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,163
Latest member
Sasha15427
Top