problems with structure

This is a discussion on problems with structure within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <string.h> #include <math.h> void read(); int Time, RH, retv; float DewPointC; float DryTempC; float val, val1; ...

  1. #61
    Registered User
    Join Date
    Jul 2009
    Posts
    53
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    
    void read();
    
    
    int Time, RH, retv;
    float DewPointC;
    float DryTempC;
    float val, val1;
    float VPD,vapourp, setvapp; 
    char line[256];
    FILE *in;
    int main()
     {	
    	in = fopen("KBIS_2005.txt", "r");
    	read();	
    	
    	return 0;
    	calculate();
    	return 0;
    			
    	
    }
    
    void read()
    {	
    	
    	int i;
    
    	for(i = 0; i<=240; i++) 
    		{
    			fgets(line,256,in);
    			retv=sscanf(line,"%d %f %f %d",&Time,&DewPointC,&DryTempC,&RH);
    			if (!retv) continue;
    			printf("%d items read: %d %f %f %d\n",retv,Time,DewPointC,DryTempC,RH);
    			val = ((7.5 * DewPointC) / (237.7 + DewPointC));
    			vapourp = pow(611, val);
    		
    			                                                                                                                                                                                                                          
    			val1 = ((7.5 * DryTempC)/(237.7 + DryTempC));
    			setvapp = pow(611, val1);
    	
    		
    			VPD = setvapp - vapourp;
    			printf("VPD IS %f",VPD);
    			if(VPD < 0.5)
    			{
    				printf("value is %d",1);
    			}
    			else
    			{
    				printf("val is %d",0);
    			}
    				
    	
    	
    		}	
    	
    }
    
    in this code i tried to apply the formula to the first line too..but it is not taking the data from the first line.it is taking from the 1 line but i need from line 0. help me.

  2. #62
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Here is a problem:

    Code:
    int main()
     {	
    	in = fopen("KBIS_2005.txt", "r");
    	read();	
    	
    	return 0;
    	calculate();
    	return 0;
    			
    	
    }
    An unconditional return statement in main() is the end of the program.

    ps. leave your message outside the code brackets!
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #63
    Registered User
    Join Date
    Jul 2009
    Posts
    53
    Code:
    void read();
    
    
    int Time, RH, retv;
    float DewPointC;
    float DryTempC;
    float val, val1;
    float VPD,vapourp, setvapp; 
    char line[256];
    FILE *in;
    int main()
     {	
    	in = fopen("KBIS_2005.txt", "r");
    	read();	
    	
    	return 0;
    	
    			
    	
    }
    
    void read()
    {	
    	
    	int i;
    	int store, store1;
    	for(i = 0; i<=240; i++) 
    		{
    			fgets(line,256,in);
    			retv=sscanf(line,"%d %f %f %d",&Time,&DewPointC,&DryTempC,&RH);
    			if (!retv) continue;
    			printf("%d items read: %d %f %f %d\n",retv,Time,DewPointC,DryTempC,RH);
    			val = ((7.5 * DewPointC) / (237.7 + DewPointC));
    			vapourp = pow(611, val);
    		
    			                                                                                                                                                                                                                          
    			val1 = ((7.5 * DryTempC)/(237.7 + DryTempC));
    			setvapp = pow(611, val1);
    	
    		
    			VPD = setvapp - vapourp;
    			printf("VPD IS %f",VPD);
    }}
    my actual code is this..still why it is not taking the first line

  4. #64
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    But there's no data on line 0, or so you've said. If the first line of the file contains real data, then it will not be skipped.

  5. #65
    Registered User
    Join Date
    Jul 2009
    Posts
    53
    Code:
    "Time"	"DryTempC"	"DewPointC"	"RH"
    0	0.6	-5	67
     1	0	-3.9	75
    2	0	-3.9	75
    this is the file looks like..there is line 0..so i need to read that too..that one is read but while calculating it is not using the first line..it starts calculating after the 0 line...that is frm 1 line...

  6. #66
    Registered User
    Join Date
    Jul 2009
    Posts
    53
    Code:
    if(VPD < 0.5)
    			{
    				 set = strcpy(store, 1);
    				 printf("value is %s",set);
    				 //printf("value is %d ",1);
    			}
    			else
    			{	 set1 = strcpy(store1,0);
    				 printf("val is %s",set1);
    				 //printf("val is %d ",0);
    			}
    how to use strcpy for integer?is this the way to do?

  7. #67
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by pokhara View Post
    my actual code is this..still why it is not taking the first line
    Do you mean it is skipping a line from the file? You could try another printf debugging line here:
    Code:
    			fgets(line,256,in);
                            printf("%d: %s",i,line);
    			retv=sscanf(line,"%d %f %f %d",&Time,&DewPointC,&DryTempC,&RH);
    			if (!retv) continue;
    printf() is great for debugging, you can cut 'n paste them and change a few things to keep an eye on data. When I have a problem I sometimes end up with them every few lines. Usually what I do is not indent them so they are easy to find and remove when I'm finished*. Also you can temporarily //comment them out.

    There is a GUI version of gdb (the gnu debugger, I think you are using linux?) called "ddd" that makes this easier (in a sense...) but don't worry about that right now, I think printf() will do fine.

    *which makes the code look very sloppy, temporarily
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #68
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by pokhara View Post
    Code:
    "Time"	"DryTempC"	"DewPointC"	"RH"
    0	0.6	-5	67
     1	0	-3.9	75
    2	0	-3.9	75
    this is the file looks like..there is line 0..so i need to read that too..that one is read but while calculating it is not using the first line..it starts calculating after the 0 line...that is frm 1 line...
    The code you posted does not do this. The code you posted gives output that starts with:
    Code:
    4 items read: 0 0.600000 -5.000000 67
    VPD IS -0.7731334

  9. #69
    Registered User
    Join Date
    Jul 2009
    Posts
    53
    i don't want to skip the first line..instead i need to use it too for the calculation. while reading the file the first line is also read but i need to use that line for the calculation too..but why it is not reading the first line.

  10. #70
    Registered User
    Join Date
    Jul 2009
    Posts
    53
    @MK27..i did that step too but still it is taking from line 1 with time 1..its not taking the time 0 line..
    @tabstop...yah it is the giving the same result...so the first line is not read

  11. #71
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by pokhara View Post
    @tabstop...yah it is the giving the same result...so the first line is not read
    Except that .... that is the result from the first line. How can you can claim that the first line is not read when your program is (a) printing it back out and (b) calculating with it?

  12. #72
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Well, it definitely will read every line, so that extra printf should print every single line. By comparing that to whatever happens next in the output, you should be able to figure out where the problem is.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  13. #73
    Registered User
    Join Date
    Jul 2009
    Posts
    53
    @MK27..i didnot get u..so u mean that the formula is also reading the first line ..that is line with time 0..as far as i beleive it is taking from the line with time 1...

  14. #74
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    Quote Originally Posted by tabstop View Post
    It's legal, if bizarre (consecutive strings are merged into one during preprocessing. It reminds me of machine-generated code (like p2c or something), except the variable names make sense. Of course, that's not a style you want to copy as such.

    He's sure.
    Code:
    printf( "Hey now, I happen to think that this particular feature of C preprocessing is very\n"
            "useful and in fact I've used it many time myself when the need arises to embed a\n"
            "long string (such as a banner message) directly into the source code when it is not\n"
            "feasible to store the message in an external file.\n" );
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  15. #75
    Registered User
    Join Date
    Jul 2009
    Posts
    53
    @tabstop...i got that problem solved..it reads the first line too now..thnks
    @MK27...i got that problem solved..it reads the first line too now..thnks

Page 5 of 6 FirstFirst 123456 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing structure arrays to functions?
    By bem82 in forum C Programming
    Replies: 3
    Last Post: 10-30-2006, 05:17 AM
  2. structure ...solution plz????
    By hegdeshashi in forum C Programming
    Replies: 4
    Last Post: 07-24-2006, 09:57 AM
  3. structure problems in windows program.
    By Bajanine in forum Windows Programming
    Replies: 3
    Last Post: 04-19-2004, 06:18 PM
  4. Realloc problems with sturcture array inside structure
    By daveyand in forum C Programming
    Replies: 2
    Last Post: 03-29-2004, 05:48 AM
  5. Structure problems...
    By MillaTime in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2002, 09:27 PM

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