Thread: Need Help

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    18

    Need Help

    Hi!

    I'm writing a program but i'm having some trouble in a function.
    My problem is that when the function goes into the bolded cycle below a second time it doesn't enter the cycle and doesn't change the variable premio.

    Here the whole function:

    Code:
    void calcular_premios(int cod_seg)
    {
    	system("cls");
    	ifstream fa;
    	ifstream fb;
    	sContrato c;
    	char linha[100], *p;
    	int cod_cob;
    	float percentagem, premio=0;
    	long pos;
    
    	pos = procura_segurado(cod_seg);
    
    	
    	fa.open("contratos.bin",ios::in|ios::binary);
    	if(!fa)
    	     return;
    	
    	fb.open("cobertura_premio.txt");
    	if(!fb)
    	    return;
    	
    	fa.seekg(pos);
    
    	while(fa.read((char *)&c, sizeof c))
    	{
    		if(c.cod_segurado == cod_seg)
    		{	
    			fb.seekg(0, ios::beg);
    			while(fb.getline(linha,100))
    			{
    				p = strtok(linha,":"); 
    				cod_cob = atoi(p);
    				p = strtok(NULL,"\n");
    				percentagem = (float)atof(p);
    				if (c.cod_cobertura == cod_cob)
    					premio+=(c.capital_apolice*percentagem);
    			}
    		}
    	}
    	fa.close();
    	fb.close();
    	
    }
    Thanks

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You need to clear the stream of the error state that is set when the read fails (at the end of the file) the first time through. Use fb.clear() before you start reading.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    18
    Yes, that's right.

    Would never got it on my own!

    Thanks a lot!!!


  4. #4
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    Hhhmm..

    sContrato
    linha
    cobertura_premio
    calcular_premios
    c.cod_segurado
    Are you Spanish?
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    18
    Quote Originally Posted by Ideswa
    Hhhmm..



    Are you Spanish?
    Close.

    I'm Portuguese.


  6. #6
    #define WORLD "sad place" LinuxCoder's Avatar
    Join Date
    Mar 2006
    Location
    Portugal
    Posts
    89
    Hi there,

    It's nice seeing another portuguese fellow around, being a portuguese fellow myself

    See you around. Cheers.

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    18
    Hi again!

    Suppose i have a function to sort a file.
    I copy half the file to an array and the other half to another array.
    Then i sort the arrays (with bubble sort) and copy each of them to a new file.
    I then merge the two new files to sort them (merge function) and copy them on the original file, thus sorting the original file (its a program requirement).
    My problem is that if a make a new sort the program (using the same files) it creates lots of entries on the original file instead of writing teh new sorted format.

    Any ideas on how to pass this?

    Thanks

  8. #8
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    My problem is that if a make a new sort the program (using the same files) it creates lots of entries on the original file instead of writing teh new sorted format.
    Don't understand. You mean the program takes in a file

    Code:
    0192837465
    And instead of making the file

    Code:
    0123456789
    It makes this file?

    Code:
    0192837465 0123456789
    Also, why bubble sort? Is this some coursework? Because bubblesort is damn sloooooowwww...

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by LinuxCoder
    Hi there,

    It's nice seeing another portuguese fellow around, being a portuguese fellow myself

    See you around. Cheers.
    Well in that case you could have been more original in the title of your thread. "Ajuda!"
    perhaps?

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    18
    Quote Originally Posted by jafet
    Don't understand. You mean the program takes in a file

    Hi!

    Well it's this way:

    the program takes a file:

    Code:
    0192837465
    breaks it in half, copying it to an array each:

    Code:
    01928
    Code:
    37465
    sorts each array:

    Code:
    01289
    Code:
    34567
    copies each array to a file and merges the two files in the original, sorted:

    Code:
    0123456789
    this works fine one time, when i do it again it goes something like this:

    Code:
    01234567012389456789
    Quote Originally Posted by jafet
    Also, why bubble sort? Is this some coursework? Because bubblesort is damn sloooooowwww...
    Yes its a coursework...


  11. #11
    #define WORLD "sad place" LinuxCoder's Avatar
    Join Date
    Mar 2006
    Location
    Portugal
    Posts
    89
    Quote Originally Posted by iMalc
    Well in that case you could have been more original in the title of your thread. "Ajuda!"
    perhaps?
    hmmm some more portuguese ppl?!? Anyway you should be addressing Goldrak since it was him who started the thread

  12. #12
    Registered User
    Join Date
    Apr 2006
    Posts
    18
    I identified where the problem seems to be:

    Code:
    	fi.clear();
    	
    	fi.seekp(0);
    	fa.seekg(0);
    	fb.seekg(0);
    	
    	fa.read((char *)&c, sizeof c);
    	fb.read((char *)&d, sizeof d);
    	
    	while (!fa.eof() && !fb.eof()) 
    	{
    		if (c.cod_cobertura > d.cod_cobertura) 
    		{
    			fi.write((char *)&c,sizeof c);
    			fa.read((char *)&c, sizeof c); 
                    }
    		else 
    		{
    			fi.write((char *)&d,sizeof d);
    			fb.read((char *)&d, sizeof d); 
    		}
    	}
    	
    	while (!fa.eof() ) 
    	{
    		fi.write((char *)&c,sizeof c);
    		fa.read((char *)&c, sizeof c); 
    	}
    	
    	while (!fb.eof() ) 
    	{
    		fi.write((char *)&d,sizeof d);
    		fb.read((char *)&d , sizeof d); 
    	}
    
    	fa.close();
    	fb.close();
    	fi.close();
    When i enter this part of the program ( the 3 while's) a second time, it doubles the number of existing entries on the fi stream.
    It appends the new information instead of writing from the beggining...

    What i'm i doing wrong??

    Thanks

  13. #13
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    I have no idea of what the fa/b/i FILEs are supposed to do; however I'm guessing that fi writes the values to the file and fa/b are for the temp files.

    Have you made sure the file is empty before you write with fi? Like opening it in ios::truncate?
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  14. #14
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    --doublepost--
    Last edited by jafet; 04-08-2006 at 08:33 AM.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  15. #15
    Registered User
    Join Date
    Apr 2006
    Posts
    18
    Quote Originally Posted by jafet
    I have no idea of what the fa/b/i FILEs are supposed to do; however I'm guessing that fi writes the values to the file and fa/b are for the temp files.
    yes, you're right!!

    Quote Originally Posted by jafet
    Have you made sure the file is empty before you write with fi? Like opening it in ios::truncate?
    This was the problem, i was appendind the fa and fb files instead of truncate them.

    Thanks a lot!!


Popular pages Recent additions subscribe to a feed