Thread: "Overprinted"?

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

    "Overprinted"?

    Hi guys, it's me again!

    And of course, I've got a problem.

    I promised I will do the next program starting with a small program, but now I need to solve this:

    When I print the following text, the string does not appear.

    Code:
     void atacant(Personatge *personatge, Enemic *aux, int *nMort, int nDaus, int *nFinal){ //nFinal indica si el enemic ha mort.
    
    
         int nMal = 0;
         nDaus = rand() %20; //Tira daus
    
    
         if (nDaus == 1){ //Falla
             printf("%s ataca i... (%d) falla!\n",personatge->strNom, nDaus);
             fflush(stdout);
             printf("Vida de %s es: %d\n",aux->strNom, aux->nVida);
             fflush(stdout);
         }
         if(nDaus >= 2 && nDaus <= 19){ //Normal
             printf("%s ataca i... (%d) Atac normal.\n",personatge->strNom, nDaus);
             nMal = personatge->nAtac - aux->nDefensa;
             fflush(stdout);
             printf("Fa %d de mal a %s\n", nMal, aux->strNom);
             aux->nVida = aux->nVida - nMal;
             if(aux->nVida <= 0){
                 printf("%s ha mort!\n", aux->strNom);
                 *nFinal = 1;
             }
             else{
                 printf("Vida de %s es: %d\n",aux->strNom, aux->nVida);
             }
         }
         if(nDaus == 20){ //Critic
             printf("%s ataca i... (%d) Atac critic!\n", personatge->strNom, nDaus);
             nMal = personatge->nAtac *2 - aux->nDefensa;
             fflush(stdout);
             printf("Fa %d de mal a %s\n", nMal, aux->strNom);
             aux->nVida = aux->nVida - nMal;
             if(aux->nVida <= 0){
                 fflush(stdout);
                 printf("%s ha mort!\n");
                 *nFinal = 1;
             }
             else{
                 printf("Vida de %s es: %d\n", aux->strNom, aux->nVida);
             }
         }
    
    
     }
    That's what appear:

    &quot;Overprinted&quot;?-sin-t-tulo-jpg



  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    What is the string that is NOT being printed, and what line number is the code that should print it?

    Please be specific!

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Explain in your own words what you think this line does.

    Code:
    nDaus = rand() %20; //Tira daus
    Your code implies that you do NOT know what it really does.

    What is the possible range of values you expect to be placed in nDaus?

    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

  4. #4
    Registered User catasturslykid's Avatar
    Join Date
    Jul 2013
    Location
    Barcelona, Spain
    Posts
    72
    Quote Originally Posted by stahta01 View Post
    Explain in your own words what you think this line does.

    Code:
    nDaus = rand() %20; //Tira daus
    Your code implies that you do NOT know what it really does.

    What is the possible range of values you expect to be placed in nDaus?

    Tim S.



    That line gives a range of values between 1 and 20 to the variable nDaus. It's like a dice for the game, where 1 means a failed attack, 2-19 normal attack and 20 critical attack.





    Quote Originally Posted by Adak View Post
    What is the string that is NOT being printed, and what line number is the code that should print it?

    Please be specific!
    My fault, sorry.

    Code:
    void atacant(Personatge *personatge, Enemic *aux, int *nMort, int nDaus, int *nFinal){ //nFinal indica si el enemic ha mort.
    
    
         int nMal = 0;
         nDaus = rand() %20; //nDaus recives a value between 1-20
    
    
         if (nDaus == 1){ //If nDaus = 1 --> Attack failed.
             printf("%s ataca i... (%d) falla!\n",personatge->strNom, nDaus); //That string does not appear.
             fflush(stdout);
             printf("Vida de %s es: %d\n",aux->strNom, aux->nVida);
             fflush(stdout);
         }
         if(nDaus >= 2 && nDaus <= 19){ //Normal attack
             printf("%s ataca i... (%d) Atac normal.\n",personatge->strNom, nDaus); //That string does not appear.
             nMal = personatge->nAtac - aux->nDefensa;
             fflush(stdout);
             printf("Fa %d de mal a %s\n", nMal, aux->strNom);
             aux->nVida = aux->nVida - nMal;
             if(aux->nVida <= 0){
                 printf("%s ha mort!\n", aux->strNom);
                 *nFinal = 1;
             }
             else{
                 printf("Vida de %s es: %d\n",aux->strNom, aux->nVida);
             }
         }
         if(nDaus == 20){ //Critical attack
             printf("%s ataca i... (%d) Atac critic!\n", personatge->strNom, nDaus); //That string does not appear.
             nMal = personatge->nAtac *2 - aux->nDefensa;
             fflush(stdout);
            printf("Fa %d de mal a %s\n", nMal, aux->strNom);
             aux->nVida = aux->nVida - nMal;
             if(aux->nVida <= 0){
                 fflush(stdout);
                 printf("%s ha mort!\n");
                 *nFinal = 1;
             }
             else{
                 printf("Vida de %s es: %d\n", aux->strNom, aux->nVida);
             }
         }
    
    
     }

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by catasturslykid View Post
    That line gives a range of values between 1 and 20 to the variable nDaus.
    False, that line of code gives a range of values from 0 to 19.

    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

  6. #6
    Registered User catasturslykid's Avatar
    Join Date
    Jul 2013
    Location
    Barcelona, Spain
    Posts
    72
    Quote Originally Posted by stahta01 View Post
    False, that line of code gives a range of values from 0 to 19.

    Tim S.
    True, thanks.

    But this is not the problem, the problem is that some strings don't appear.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Your main problem - at least in the forum - is that you haven't provided enough information.

    There is nothing in that sample output you have shown which is inconsistent with the code. Your problem is that the code behaviour depends on data supplied to the function (depending on what data is supplied, some lines of output may not always be produced). You have provided no information about what data is being passed to the function.

    The fact that nDaus will never have a value of 20, but a block of your code will only produce output if nDaus has a value of 20, means that block will never be executed. [That was the point being made by stahta01].

    Without knowing what data you are supplying your function, it is impossible to predict what output your code will produce, let alone check whether it is consistent with your expectations. It is, for example, possible for a string to have length zero - printing such a string produces nothing.
    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.

  8. #8
    Registered User catasturslykid's Avatar
    Join Date
    Jul 2013
    Location
    Barcelona, Spain
    Posts
    72
    I didn't post all the code because it's quite big and I don't want to occupy your time.

    That's the code you will propably need:

    Code:
      void aventura(Personatge *personatge, Enemic *enemics, FILE *h, int nEnemics){
      
       Cua cua;
       int nMort = 0, nExpguany = 0, nRivals = 0;
      
      
         printf("%d\n",nEnemics); 
    
    
     
         cua = CUA_crea();
         mostraEnemics(enemics,nEnemics,&cua,&nRivals); /
         lluita(personatge,&cua,&nMort,&nExpguany,enemics);//That's the function where I have the problem.
         printf("LUCHA FINALIZADA\n");
         levelup(personatge,nMort,nExpguany,h); 
         printf("LEVELUP FINALIZADO\n");
         CUA_destrueix(&cua);
    
    
     }

    Function LLUITA

    Code:
     void lluita(Personatge *personatge, Cua *cua, int *nMort, int *nExpguany,Enemic *enemics){
     
          Enemic aux;
          int nDaus = 0, nFinal = 0,  nVidamax = 0; 
     
     
          if(cua->cap == cua->ult){
              printf("Error, cua buida!\n");
          }
          else{
     
              nVidamax = personatge->nVida;
              while((*cua).cap != (*cua).ult && *nMort != 1){
                  aux = CUA_primer(*cua);
                  CUA_desencua(&(*cua));
                  personatge->nVida = nVidamax;
                  nFinal = 0;
                  printf("Combat entre %s i %s\n", personatge->strNom, aux.strNom);
                  printf("Prem una tecla per continuar...\n");
                  getchar();
                  fflush(stdout);
     
                  while(nFinal == 0){
     
                      atacant(personatge, &aux, &nMort, nDaus, &nFinal); //HERE IS THE FUNCTION WHERE THE PROBLEM APPEARS
                      if(nFinal == 0){
                          enemic(personatge, &aux, &nMort, nDaus, &nFinal);
                      }
     
                      if(nFinal == 1 ){
     
                      printf("ENTRA EN IF nFinal = 1\n");
                          if( *nMort == 0){ 
                             printf("BIEN, SE ACTUALIZA LA EXPERIENCIA\n");
                             *nExpguany = *nExpguany +  enemics->nExperiencia;
                             printf("%d\n",*nExpguany);
                             printf("Final del combat!\n");
                         }
                         else{
                             printf("NO EXP!!\n");
                             printf("Final del combat!\n");
                        }
                     printf("Prem una tecla per continuar...\n");
                     getchar();
                     fflush(stdout);
    
    
                     }
                 }
             }
         }
     }

    ATACANT

    Code:
     void atacant(Personatge *personatge, Enemic *aux, int **nMort, int nDaus, int *nFinal){ //nFinal means that the enemy is dead.
    
    
         int nMal = 0;
         nDaus = rand() %21; //Tira daus
    
    
         if (nDaus == 1){ //Fail
             printf("%s ataca i... (%d) falla!\n",personatge->strNom, nDaus);
             fflush(stdout);
             printf("Vida de %s\n",aux->strNom);
             printf("%d\n",aux->nVida);
             fflush(stdout);
         }
         if(nDaus >= 2 && nDaus <= 19){ //Normal
             printf("%s ataca i... (%d) Atac normal.\n",personatge->strNom, nDaus);
             nMal = personatge->nAtac - aux->nDefensa;
             if(nMal < 0){
                 nMal = 0;
             }
             fflush(stdout);
             printf("Fa %d de mal a %s\n", nMal, aux->strNom);
             aux->nVida = aux->nVida - nMal;
             if(aux->nVida <= 0){
                 printf("%s ha mort!\n", aux->strNom); //HERE THE %S IS PRINTED BUT UNDER THE REST OF THE TEXT: "ha mort!\n"
                 *nFinal = 1;
             }
             else{
                 printf("Vida de %s\n",aux->strNom);
                 printf("%d\n",aux->nVida);
             }
         }
         if(nDaus == 20){ //Critical ->  HERE APPEARS A CORE DUMPED!
             printf("%s ataca i... (%d) Atac critic!\n", personatge->strNom, nDaus);
             nMal = (personatge->nAtac *2) - aux->nDefensa;
             if(nMal < 0){
                 nMal = 0;
             }
             fflush(stdout);
             printf("Fa %d de mal a %s\n", nMal, aux->strNom);
             aux->nVida = aux->nVida - nMal;
            if(aux->nVida <= 0){
             printf("%s ha mort!\n");
                 *nFinal = 1;
             }
             else{
                 printf("Vida de %s es: %d\n", aux->strNom, aux->nVida);
             }
         }
    
    
     }

    The critical function is the same that the normal function, but a CORE DUMPED appears.... (segmentation fault).
    Last edited by catasturslykid; 08-14-2013 at 09:54 AM.

  9. #9
    Registered User catasturslykid's Avatar
    Join Date
    Jul 2013
    Location
    Barcelona, Spain
    Posts
    72
    Can someone help me, please?

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you need to post a minimal compilable/runnable sample that represents a problem
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Add a line 4 to atacant(). Print out all the values of the parameters that are being brought into atacant, and then add a scanf("%d", &garbage) with a "garbage" variable you will add to atacant. I want you to verify that atacant() is getting the right values. The values that you are expecting, and then a pause so you can see them.

    Then line 5, change % 21 to rand() % 20 + 1. I believe the range you want is 1 to 20, not 0 to 19, correct?

    Every time you run the program, unless you are seeding the random generator first, you will be given the SAME random values by rand(). This is for testing purposes. Print out on line 6, WHAT that value of nDaus is, by printing it out, just like you did for line #4. Tell us what that value is for nDaus.


    Lastly, are you getting any warnings when you compile the program? If so, what are they?

  12. #12
    Registered User catasturslykid's Avatar
    Join Date
    Jul 2013
    Location
    Barcelona, Spain
    Posts
    72
    Hi Adak, thank you for your help. Here is all the information that you want:

    WARNINGS: (Are not in english, so I have to translate)

    main's type return is not int

    I think that's because I'm using VOID MAIN instead of INT MAIN.

    There's no more warnings.

    Now, I've done that to see the variables that are being brought into atacant:

    Code:
     
     printf(" %s %d %d %d %d\n", personatge->strNom, personatge->nAtac, personatge->nVida, personatge->nExperiencia, personatge->nNivell);
     printf(" %s %d %d %d %d\n", aux->strNom, aux->nAtac, aux->nDefensa, aux->nExperiencia);
     printf("%d\n",*(*nMort));
     printf("%d\n",*nFinal);
    And that's waht I see:

    &quot;Overprinted&quot;?-sin-t-tulo-jpg

    All the numbers are correct, BUT the second string has the same problem that I said in the other post.

    nDaus has a diferent value every time, that's what I need. I put this on the main.c:

    Code:
    srand(time(NULL));

    What do you think?

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > What do you think?
    I think that none of the code you've posted (or the input you've suggested) has either of the strings "Proba" or "vall" present.
    So it's impossible to say where they're coming from.

    > printf("%s ha mort!\n", aux->strNom); //HERE THE %S IS PRINTED BUT UNDER THE REST OF THE TEXT: "ha mort!\n"
    If strNom contains a newline, then this is what you will see?
    Did you by chance read the line with fgets(), which will store the \n.

    > printf("%s ha mort!\n");
    > printf(" %s %d %d %d %d\n", aux->strNom, aux->nAtac, aux->nDefensa, aux->nExperiencia);
    As a matter of urgency, you must start compiling with the -Wall option, and fixing ALL the warnings before trying to run code.
    If you don't supply the correct number (and type) of parameters to printf/scanf, then all sorts of crap can happen.

    If you don't understand a particular warning, then ask (don't ignore it).

    Too few parameters in the presence of a %s is one sure way of getting large amounts of garbage printed (assuming it doesn't crash in the process).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by catasturslykid View Post
    Hi Adak, thank you for your help. Here is all the information that you want:

    WARNINGS: (Are not in english, so I have to translate)

    main's type return is not int

    I think that's because I'm using VOID MAIN instead of INT MAIN.
    Correct! main() should return an int to the operating system.

    There's no more warnings.

    Now, I've done that to see the variables that are being brought into atacant:

    Code:
     
    printf(" %s %d %d %d %d\n", personatge->strNom, personatge->nAtac, personatge->nVida, personatge->nExperiencia, personatge->nNivell);
    
    
     printf(" %s %d %d %d %d\n", aux->strNom, aux->nAtac, aux->nDefensa, aux->nExperiencia);
     printf("%d\n",*(*nMort));
     printf("%d\n",*nFinal);
    And that's what I see:
    All the numbers are correct, BUT the second string has the same problem that I said in the other post.

    nDaus has a different value every time, that's what I need. I put this on the main.c:

    Code:
    srand(time(NULL));
    What do you think?
    OK, leave srand() there.

    You have to follow this variable (aux->strNom) back to it's origin, and when you find it, post the code. It has an newline at the end of it, that needs to be removed, right at the point it's being given a value.

    As Salem suggested, it's probably because it was given it's value by fgets(), since fgets() puts a newline onto the end of a string.
    Do you know how to delete a last newline char from a string yet?
    Last edited by Adak; 08-16-2013 at 12:47 PM.

  15. #15
    Registered User catasturslykid's Avatar
    Join Date
    Jul 2013
    Location
    Barcelona, Spain
    Posts
    72
    Quote Originally Posted by Salem View Post
    > What do you think?
    I think that none of the code you've posted (or the input you've suggested) has either of the strings "Proba" or "vall" present.
    So it's impossible to say where they're coming from.

    > printf("%s ha mort!\n", aux->strNom); //HERE THE %S IS PRINTED BUT UNDER THE REST OF THE TEXT: "ha mort!\n"
    If strNom contains a newline, then this is what you will see?
    Did you by chance read the line with fgets(), which will store the \n.

    > printf("%s ha mort!\n");
    > printf(" %s %d %d %d %d\n", aux->strNom, aux->nAtac, aux->nDefensa, aux->nExperiencia);
    As a matter of urgency, you must start compiling with the -Wall option, and fixing ALL the warnings before trying to run code.
    If you don't supply the correct number (and type) of parameters to printf/scanf, then all sorts of crap can happen.

    If you don't understand a particular warning, then ask (don't ignore it).

    Too few parameters in the presence of a %s is one sure way of getting large amounts of garbage printed (assuming it doesn't crash in the process).
    Sorry Salem, but I don't know how to use the -Wall option

    Quote Originally Posted by Adak View Post
    Correct! main() should return an int to the operating system.



    OK, leave srand() there.

    You have to follow this variable (aux->strNom) back to it's origin, and when you find it, post the code. It has an newline at the end of it, that needs to be removed, right at the point it's being given a value.

    As Salem suggested, it's probably because it was given it's value by fgets(), since fgets() puts a newline onto the end of a string.
    Do you know how to delete a last newline char from a string yet?

    Well, here is the code where the variable aux->strNom is:

    Code:
     void lluita(Personatge *personatge, Cua *cua, int *nMort, int *nExpguany,Enemic *enemics){
     
          Enemic aux; //HERE IT'S THE VARIABLE aux.
          int nDaus = 0, nFinal = 0,  nVidamax = 0; 
     
     
          if(cua->cap == cua->ult){
              printf("Error, cua buida!\n");
          }
          else{
     
              nVidamax = personatge->nVida;
              while((*cua).cap != (*cua).ult && *nMort != 1){
                  aux = CUA_primer(*cua); //The variable aux recieves information from CUA, that in english is a TAIL.
                  CUA_desencua(&(*cua));
                  personatge->nVida = nVidamax;
                  nFinal = 0;
                  printf("Combat entre %s i %s\n", personatge->strNom, aux.strNom);
                  printf("Prem una tecla per continuar...\n");
                 getchar();
                fflush(stdout);
     
                  while(nFinal == 0){
     
                      atacant(personatge, &aux, &nMort, nDaus, &nFinal);
                      if(nFinal == 0){
                          enemic(personatge, &aux, &nMort, nDaus, &nFinal);
                      }
    
    
                      if(nFinal == 1 ){
     
                      printf("ENTRA EN IF nFinal = 1\n");
                          if( *nMort == 0){ 
                             printf("BIEN, SE ACTUALIZA LA EXPERIENCIA\n");
                             *nExpguany = *nExpguany +  enemics->nExperiencia;
                             printf("%d\n",*nExpguany);
                             printf("Final del combat!\n");
                         }
                         else{
                             printf("NO EXP!!\n");
                             printf("Final del combat!\n");
                        }
    And of course, I'm going to put the code where the info comes from, but before that, let me explain what's in the tail.

    There are the different enemies available and their own characteristics, like attack, defense and experience.

    How I get that? I get that info from a FILE .txt

    Here is the code:

    Code:
     void carregaEnemics(FILE *f, Enemic **enemics, int *nEnemic){
     
      char strNombre[50];
      char strNom[50];
      char cSalt;
      int nAtac = 0, nDefensa = 0, nVida = 0, nExperiencia = 0, i;
     
          fscanf(f,"%d",nEnemic); //Reads how many enemies are in the file.
          fgets(strNombre,50,f);
          *enemics = (Enemic*)malloc(sizeof(Enemic) * *nEnemic);
          if(enemics == NULL){
              printf("Error!\n");
          }
          else{
              for( i = 0; i < *nEnemic; i++){
     
                  fgets(strNombre,50,f); //Reading the name.
                  strNombre[strlen(strNombre)-1]='\0'; //Remove the \n
     
                  fscanf(f,"%d",&nAtac);
                  fscanf(f,"%d",&nDefensa);
                  fscanf(f,"%d",&nVida);
                  fscanf(f,"%d",&nExperiencia);
     
     
                  strcpy((*enemics)[i].strNom, strNombre);
                  (*enemics)[i].nAtac = nAtac;
                  (*enemics)[i].nDefensa = nDefensa;
                  (*enemics)[i].nVida = nVida;
                  (*enemics)[i].nExperiencia = nExperiencia;
                  fgets(strNombre,50,f);
              }
          }
     
     
      }
    The file is like:

    11
    Cavaller
    20
    10
    100
    100
    Tirant lo Blanc
    120
    75
    600
    1100
    Sarrai a cavall
    70
    35
    350
    500
    Creuat
    30
    15
    150
    200
    Sarrai amb espasa i escut
    40
    20
    200
    300
    Creuat delit
    100
    65
    500
    900
    Pirata marroqui
    50
    25
    250
    400
    Inquisidor
    90
    60
    450
    800
    Joanot Martorell
    240
    150
    1200
    3000
    Carmesina
    130
    80
    650
    1200
    Guia estil
    150
    100
    50
    4500
    -Number of enemies
    -Name
    -Attack
    -Defense
    -Life
    -Experience





    Hope this will help you, guys!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 08-19-2012, 06:15 AM
  2. Replies: 46
    Last Post: 08-24-2007, 04:52 PM
  3. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  4. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM