Thread: I cannot find the error...

  1. #1
    Registered User catasturslykid's Avatar
    Join Date
    Jul 2013
    Location
    Barcelona, Spain
    Posts
    72

    I cannot find the error...

    Hi guys!

    I've got the following problem with my program:

    While opening a file to read, something happens and nothing appears at the screen.

    I ask the user the filename and the program opens it.

    If the filename is wrong, the program says: "Error!" and returns to the main menu.

    BUT, if the filename is correct, nothing happens.

    Here is the code:

    Code:
    #include "total.h" //All the functions and xxx.h are included
    
    void carregaPersonatge(FILE *h, Classe *classes, int nClas, int nEnemics, Enemic *enemics){
    
    Personatge personatge;
    char strNom[50], strNomc[50],strOmet[100];
    int nVida, nAtac, nExp, nNivell, nQuina;
    
    printf("FILE name?");
    scanf("%s",strNom);
    h = fopen(strNom,"r");
    
    printf("HELLO!"); //THAT HELLO DOES NOT APPEAR.
    
    if(h == NULL){
    printf("Error!\n");
    menuPrincipal(nClas, nEnemics, classes, enemics);
    }
    else{
    
    printf("NICE"); //DOES NOT APPEAR.
    
    //NOW I READ ALL THE INFORMATION FROM THE FILE
    
    fscanf(h,"%s",strOmet);
    fscanf(h,"%s",strNomc);
    fscanf(h,"%d",&nNivell);
    fscanf(h,"%d",&nExp);
    personatge.nExperiencia = nExp;
    personatge.nNivell = nNivell;
    strcpy(personatge.strNom, strNom);
    nQuina = dadesClasse(strNomc,nClas,classes);
    
    personatge.vida = classe[nQuina].nVida + nNivell*50;
    personatge.nAtac = classe[nQuina].nAtac + nNivell*10;
    personatge.nDefensa = classe[nQuina].nDefensa + nNivell*5;
    strcpy(personatge.classe.strNom, classes[nQuina].strNom);
    menuPersonatge(personatge, enemics, h ,nEnemics,nClas,classes);
    
    }
    fclose(h);
    }
    And that's what appears (NOTHING!):

    I cannot find the error...-sin-t-tulo-png

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Line 13, add a newline \n to the end of the string you're printing. It appears to be buffering it, which you don't want.

    Ditto line 21.

    Then recompile it and check your compiler's warnings. Run it and see what happens, just to help narrow it down.

    Why do I want you to debug it a bit? Because debugging skills must be practiced, and I don't see that you've done much, yet.

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You need to post your entire program since the error is presumably somewhere else.

    And if you're using printf as a debug tool you should probably use fprintf(stderr, ...) and make sure you have a newline at the end of the string.
    Otherwise the buffered nature of stdout may cause the output not to appear even though the line was executed.

    Of course, a debugger is better.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User catasturslykid's Avatar
    Join Date
    Jul 2013
    Location
    Barcelona, Spain
    Posts
    72
    Hi guys. I added the \n at the end of all the printf and it worked, but not at all.

    Let me explain:

    Everything works until I arrive to the function dadesClasse.

    That function returns and INT that shows the position of the array Classe that the program needs.

    The code is:

    Code:
    int dadesClasse(char strNomc[50], int nClas, Classe *classes){
    
    int i = 0, nTrobat = 0, nQuina; 
    
        while(i < nClas && !nTrobat){
            if(strcmp(classes[i].strNom, strNomc) == 0){
                nTrobat = 1;
                nQuina = i;
            }
        }
        return nQuina;
    }
    And, as I said in my last post, nothing happens.

    I add

    Code:
    printf("%d\n", &nQuina);
    under the function

    Code:
    nQuina = dadesClasse(...);
    to check if nQuina is correct, but nothing happens.

    Where is the error?

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You also need to provide the main() function that is calling carregaPersonatge(). And the contents of total.h.

    What about the notion of "post your entire program" is eluding you?

    The reason others have requested your entire program is that, most of the time, code that people choose not to post is a contributor to their problem. Forum members are not mindreaders. Since your code is reading a file, it is a good idea to provide a sample input file. Mismatches between the contents of a file, and the code which reads it, are another common cause of problems.

    If your "entire program" is too big to post (i.e. it will be too big to expect people to sift through, unless you pay them hard cash) then provide a small and complete sample of code that accurately exhibits your problem.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Jul 2013
    Posts
    41
    Quote Originally Posted by grumpy View Post
    What about the notion of "post your entire program" is eluding you?
    .
    how did you get the nickname 'grumpy' ?

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    int i = 0, nTrobat = 0, nQuina;
    Change above to
    Code:
    int i = 0, nTrobat = 0, nQuina = -1;
    Then check for -1 if it is -1 DO NOT use nQuina because the value is bad.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by stach View Post
    how did you get the nickname 'grumpy' ?
    By having a low tolerance for rampant stupidity, those who ignore reasonable requests for clarifying information, those who think they are entitled to have others do their homework for them, and those who ask silly questions about my handle. Ignorance I accept (as everyone is ignorant of something, and they can address that through honest effort).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  9. #9
    Registered User catasturslykid's Avatar
    Join Date
    Jul 2013
    Location
    Barcelona, Spain
    Posts
    72
    Quote Originally Posted by grumpy View Post
    You also need to provide the main() function that is calling carregaPersonatge(). And the contents of total.h.

    What about the notion of "post your entire program" is eluding you?

    The reason others have requested your entire program is that, most of the time, code that people choose not to post is a contributor to their problem. Forum members are not mindreaders. Since your code is reading a file, it is a good idea to provide a sample input file. Mismatches between the contents of a file, and the code which reads it, are another common cause of problems.

    If your "entire program" is too big to post (i.e. it will be too big to expect people to sift through, unless you pay them hard cash) then provide a small and complete sample of code that accurately exhibits your problem.
    Excuse me, sir.

    I do not post the entire program because is quite big. And the function carregaPersonatge is an "in-function". Another problem is that I'm using "putty" to conect to my university computer and I can't copy-paste.

    Here's the "total.h":

    Code:
    #ifndef TOTAL_H
    #define TOTAL_H
    
    #include "tipus.h"
    #include "cua.h"
    
    //I'm going to write only the important functions.
    
    void main(int argc, char* argv[]);
    void menuPrincipal(int nClas, int nEnemics, Classe *classes, Enemic *enemics);
    .
    .
    .
    void carregaPersonatge(FILE *h, Classe *classes, int nClas, int nEnemics, Enemic *enemics);
    int dadesClasse(char strNomc[50], int nClas, Classe *classes);
    
    #endif

    MAIN

    Code:
    #include "total.h"
    
    void main(int argc, char* argv[]){
    
    FILE *f,*g;
    int nCorrecte = 0, nOpcio, nEnemics, nClas;
    Classe *classes;
    Enemic *enemics;
    
    srand(time(NULL));
    
    f = fopen(argv[1],"r");
    if (f == NULL){
        printf("Error!(1)\n");
    }
    else{
        g = fopen(argv[2],"r");
        if( g == NULL){
             printf("Error!(2)\n);
        }
        else{
           carregaClasses(f,&classes,&nClas);
           carregaEnemics(g,&enemics,&nEnemics);
           menuPrincipal(nClas, nEnemics, classes, enemics);
        }
    }
    fclose(f);
    fclose(g);
    }
    menuPrincipal
    Code:
    void menuPrincipal(int nClas, int nEnemics, Classe *classes, Enemic *enemics){
    
    FILE *h;
    int nCorrecte = 0, nOpcio;
    
    while(nCorrecte == 0){
    
    printf("  Menu Principal\");
    printf("1- New character\n");
    printf("2- LOAD CHARACTER\n");
    printf("3- Exit\n");
    printf("Option?");
    scanf("%d",&nOpcio);
    if(nOpcio == 1 || nOpcio == 2 || nOpcio == 3){
        nCorrecte = 1;
    }
    else{
        printf("Error!\n");
    }
    
    if(nCorrecte == 1){
    switch(nOpcio){
        case 1: creaPersonatge(classes, nClas, &h, nEnemics, enemics);
                   break;
        case 2: carregaPersonatge (&h,classes,nClas,nEnemics,enemics);
                   break;
        default: exit(1);
                   break;
       }
    }
    }
    }

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Another problem is that I'm using "putty" to conect to my university computer and I can't copy-paste.
    Fight your ignorance: PuTTY FAQ

  11. #11
    Registered User catasturslykid's Avatar
    Join Date
    Jul 2013
    Location
    Barcelona, Spain
    Posts
    72
    Thanks!

    Our teachers said that we cannot copy-paste from PuTTY to Windows... Thanks again!


    Let's paste all the code:

    Code:
       #include "total.h"
      
      
      
       void main(int argc, char* argv[]){ //S'executa possant: practica3 fitxer_classes fitxer_enemics
      
       FILE *f, *g;
       int nCorrecte = 0, nOpcio, nEnemics, nClas;
       Classe *classes;
      Enemic *enemics;
     
     
          srand(time(NULL));
     
          f = fopen (argv[1],"r");
          if (f == NULL){
              printf("Error!(1)\n");
          }
          else{
     
             g = fopen (argv[2],"r");
              if( g == NULL){
                  printf("Error!(2)\n");
              }
              else{
                  carregaClasses(f,&classes,&nClas);
                  carregaEnemics(g,&enemics,&nEnemics);
                  printf("%s", enemics[5].strNom);
                  menuPrincipal(nClas, nEnemics, classes, enemics);
     
              }
          }
          fclose(f);
          fclose(g);
      }
    Code:
     #include "total.h"
      
      
       void menuPrincipal(int nClas, int nEnemics, Classe *classes, Enemic *enemics){
       FILE *h;
       int nCorrecte = 0, nOpcio;
      
      
      
          while (nCorrecte == 0){
              printf("   Menu Principal\n");
              printf("1- Crear Personatge Nou\n");
              printf("2- Carregar Personatge\n");
              printf("3- Sortir\n\n");
              printf("Opcio? ");
              scanf("%d",&nOpcio);
              if( nOpcio == 1 || nOpcio == 2 || nOpcio ==3){
                  nCorrecte = 1;
              }
              else{
                  printf("Error! Escull una opcio correcta!\n");
              }
              if( nCorrecte == 1){
                  switch(nOpcio){
                      case 1: creaPersonatge(classes, nClas, &h, nEnemics, enemics);
                              break;
                      case 2: carregaPersonatge(&h,classes,nClas,nEnemics,enemics);
                              break;
                      default: exit(1);
                              break;
                      }
              }
          }
      }
    Code:
      #include "total.h"
      
      
      
       void carregaPersonatge(FILE *h, Classe *classes, int nClas,int nEnemics, Enemic *enemics){
      
       Personatge personatge;
       char strNom[50], strNomc[50], strOmet[100];
       int nVida, nAtac, nExp, nNivell, nQuina = 0;
     
     
          printf("Nom del personatge?:  ");
          scanf("%s",strNom);
          h = fopen(strNom,"r"); 
          printf("HOLA\n");
          if( h == NULL){
              printf("Error!\n");
              menuPrincipal(nClas, nEnemics, classes, enemics);//Return to the main menu.
          }
          else{
     
              printf("NICE\n");
     
              fscanf(h,"%s",strOmet); 
              printf("%s\n",strOmet); //Only to check
              fscanf(h,"%s", strNomc); 
              printf("%s\n",strNomc); //Only to check
              fscanf(h,"%d",&nNivell); 
              fscanf(h,"%d",&nExp); 
              personatge.nExperiencia = nExp;
              personatge.nNivell = nNivell;
              strcpy(personatge.strNom, strNom);
              printf("%d\n",personatge.nNivell); //Only to check
              nQuina = dadesClasse(strNomc,nClas,classes); 
              printf("%d\n",&nQuina); //Only to check -> Nothing happens.
              personatge.nVida = classes[nQuina].nVida + nNivell*50;
              personatge.nAtac = classes[nQuina].nAtac + nNivell*10;
              personatge.nDefensa = classes[nQuina].nDefensa + nNivell*5;
              strcpy(personatge.classe.strNom, classes[nQuina].strNom);
              printf("%s\n",personatge.classe.strNom); //Only to check
              menuPersonatge(personatge, enemics, h, nEnemics, nClas, classes);
    
    
     
          }
          fclose(h);
     }
    And as I said, after print personatge.nNivell nothing appears on the screen.
    Last edited by catasturslykid; 08-12-2013 at 10:42 AM.

  12. #12
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Once again Initialize all local variables!!

    Code:
    FILE *f, *g;
     int nCorrecte = 0, nOpcio, nEnemics, nClas;
     Classe *classes;
    Enemic *enemics;
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  13. #13
    Registered User catasturslykid's Avatar
    Join Date
    Jul 2013
    Location
    Barcelona, Spain
    Posts
    72
    Quote Originally Posted by stahta01 View Post
    Once again Initialize all local variables!!

    Code:
    FILE *f, *g;
     int nCorrecte = 0, nOpcio, nEnemics, nClas;
     Classe *classes;
    Enemic *enemics;
    Done! But still not working.

    One more question?

    Why is important to initialize all the local variables?

  14. #14
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by catasturslykid View Post
    Done! But still not working.

    One more question?

    Why is important to initialize all the local variables?
    Because they can have random values if not initialized.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  15. #15
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288
    Quote Originally Posted by stahta01 View Post
    Because they can have random values if not initialized.

    Tim S.
    To add to that, usually when a computer gives you memory to store values in, it doesn't zero out the memory. That means you get whatever values left over from any number of sources on your computer.
    "Some people think they can outsmart me, maybe. Maybe. I've yet to meet one that can outsmart bullet" - Meet the Heavy, Team Fortress 2

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you find the error?
    By neeha_khan in forum C++ Programming
    Replies: 20
    Last Post: 04-15-2013, 07:02 AM
  2. Cant Find Error
    By Toonzaka in forum C Programming
    Replies: 3
    Last Post: 08-10-2008, 02:00 PM
  3. one error....can't find it
    By ammanbesaw in forum C++ Programming
    Replies: 4
    Last Post: 12-13-2007, 10:36 AM
  4. Cannot find error
    By rwmarsh in forum Game Programming
    Replies: 6
    Last Post: 09-20-2006, 06:48 AM
  5. Cant find the error
    By Coder87C in forum C Programming
    Replies: 8
    Last Post: 06-19-2005, 01:57 AM