Thread: Hello! Can someone help me? My code doesn't work correctly!

  1. #1
    Registered User
    Join Date
    Jun 2015
    Posts
    12

    Hello! Can someone help me? My code doesn't work correctly!

    While I execute it 'printf("\nNota: %d", stud.nota[n]);' gives me the same result when I put differnt datas.
    Code:
    void afisho_lende(){
    	file=fopen("lende.bin","rb");
    	bin=fopen("student.bin","rb");
    	
    	char p_emri[20];
    	char pergjigje;
    	
    	if(!file&&!bin)
    	printf("\nU hasen probleme ne hapje");
    
    
        else{
        	printf("\nJepni emrin e lendes: ");
    		scanf("%s",p_emri);
    		
    		int n;
    		int nr_student=nrrekordesh_student(bin);
    		int nr_lende=nrrekordesh_lende(file);
    		
    		for(int i=0;i<nr_lende;i++){
    			fseek(file,sizeof(struct lendet)*i,SEEK_SET);
    			fread(&lenda,sizeof(struct lendet),1,file);
    			if(strcmp(p_emri,lenda.emri)==0){
    			
    			   printf("\n\n");
                   printf("Emri: %s\n",lenda.emri);
                
              	
                for(int j=0;j<nr_student;j++){
                	n=j;
                	fseek(bin,sizeof(struct student)*j,SEEK_SET);
    			    fread(&stud,sizeof(struct student),1,bin);
                	printf("\nEmri: %s",stud.emri);
                	printf("\nMbiemri: %s",stud.mbiemri);
                	printf("\nGrupi: %d", stud.grupi);
                	printf("\nNota: %d", stud.nota[n]);
                	
    				   
    			fclose(bin);
    			fclose(file);
    		}
    		getchar();
    			
    			
    			
    			printf("\nShtypni p per te kerkuar nje lende tjeter");
    			scanf("%c",&pergjigje);
    			
    			if(pergjigje=='p'||pergjigje=='P')
                afisho_lende();
    		    else
    		    main();
       }}
       }}

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    For starters, you need to indent your code properly, e.g.,
    Code:
    void afisho_lende() {
        file = fopen("lende.bin", "rb");
        bin = fopen("student.bin", "rb");
    
        char p_emri[20];
        char pergjigje;
    
        if (!file && !bin)
            printf("\nU hasen probleme ne hapje");
        else {
            printf("\nJepni emrin e lendes: ");
            scanf("%s", p_emri);
    
            int n;
            int nr_student = nrrekordesh_student(bin);
            int nr_lende = nrrekordesh_lende(file);
    
            for (int i = 0; i < nr_lende; i++) {
                fseek(file, sizeof(struct lendet) * i, SEEK_SET);
                fread(&lenda, sizeof(struct lendet), 1, file);
                if (strcmp(p_emri, lenda.emri) == 0) {
                    printf("\n\n");
                    printf("Emri: %s\n", lenda.emri);
    
                    for (int j = 0; j < nr_student; j++) {
                        n = j;
                        fseek(bin, sizeof(struct student) * j, SEEK_SET);
                        fread(&stud, sizeof(struct student), 1, bin);
                        printf("\nEmri: %s", stud.emri);
                        printf("\nMbiemri: %s", stud.mbiemri);
                        printf("\nGrupi: %d", stud.grupi);
                        printf("\nNota: %d", stud.nota[n]);
    
                        fclose(bin);
                        fclose(file);
                    }
                    getchar();
    
                    printf("\nShtypni p per te kerkuar nje lende tjeter");
                    scanf("%c", &pergjigje);
    
                    if (pergjigje == 'p' || pergjigje == 'P')
                        afisho_lende();
                    else
                        main();
                }
            }
        }
    }
    Global variables make it difficult to reason about your program; you should avoid global variables. For example, file and bin can easily be declared locally. stud should probably be a parameter.

    Why are you still using recursion here? You have the recursive calls right smack within a loop. This is almost certainly wrong.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Also, why are you closing your files within the loop?

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    12
    Thank You!
    I have used recrusive function because it has not give me any errors.
    The problem is at stud.nota[n].
    While I put some different 'lenda' it gives me the same numbers exactly the numbers that I put for the first time.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    And why the call to main()?

    Jim

  6. #6
    Registered User
    Join Date
    Jun 2015
    Posts
    12
    How should i close files?

  7. #7
    Registered User
    Join Date
    Jun 2015
    Posts
    12
    Jim I call main() to execute the other part of program

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Eugen Hoxha
    I have used recrusive function because it has not give me any errors.
    It is not giving you any compile errors, but you have a big logic error right there. Recursion is not the right tool for the job here. Use iteration. Have the function return the caller, and let the caller control whether or not the function is called again.

    Quote Originally Posted by Eugen Hoxha
    The problem is at stud.nota[n].
    While I put some different 'lenda' it gives me the same numbers exactly the numbers that I put for the first time.
    Fix the problems with recursion and with global variables first. Once you make those fixes, fixing this problem will become much easier (and in fact you might find that the problem just disappears).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Jun 2015
    Posts
    12
    Can someone help me what should I do exactly???

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    The function main() should never be called by any function including it's self. You should instead just return to the calling function.

    Jim

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by Eugen Hoxha View Post
    How should i close files?
    You close the files when you're done with them, not when you're in the process of accessing them in a loop.
    You should also check that the files have opened successfully before trying to access them.

    Quote Originally Posted by Eugen Hoxha View Post
    Jim I call main() to execute the other part of program
    Here's me describing to you how to properly do this with loops, rather than calling "main()": http://cboard.cprogramming.com/c-pro...ml#post1233112

    Quote Originally Posted by Eugen Hoxha View Post
    Can someone help me what should I do exactly???
    Follow the advice you've been given. If there's something you don't understand, ask for clarification.

  12. #12
    Registered User
    Join Date
    Jun 2015
    Posts
    12
    Thank you everyone it works without a problem now!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My addition program doesn't work correctly.
    By tmac619619 in forum C Programming
    Replies: 1
    Last Post: 11-04-2012, 02:05 AM
  2. I made a program but it doesn't work correctly
    By jeremy duncan in forum C Programming
    Replies: 30
    Last Post: 11-01-2011, 01:57 PM
  3. Uploading files doesn't work correctly
    By Devils Child in forum C# Programming
    Replies: 8
    Last Post: 05-21-2009, 06:51 AM
  4. Writing BMP file... doesn't work correctly
    By tin in forum Game Programming
    Replies: 3
    Last Post: 12-28-2005, 04:40 AM
  5. linked list doesn't work correctly
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 03-12-2002, 11:32 AM