How to return a data message that does not meet a condition in a repeat loop?

Asked

Viewed 70 times

-1

Good night, you guys! I am studying C language by myself and, to train, I am trying to encode a program that reads inputs of metallurgical parameters (alloy, temper and thickness) so that a radius value for bending assay is returned to the user. Well, I was able to encode a piece of code with a loop to evaluate the parameters passed by the user, determining whether they are valid for the fold test or not. So far, I’ve been able to filter the alloy, and I’ve been able to determine whether or not it enters the group of alloys that require bending assay, but I’m unable to filter the temper. The code goes below:

for (cont=0; cont<=3;cont++){ //contador para comparação com as ligas cadastradas
    if (liga1==liga[cont]){
        for (cont=0; cont<=6;cont++){ //contador para comparação com as têmperas cadastradas
            if (strcmp(temp1,temp[cont])==0){
                printf ("\nDobramento exigido para a liga %d e têmpera %s, na espessura %.3fmm na Norma ASTM!\n", liga1, temp1, esp1);
                if (esp1>=esp[0] && esp1<esp[1]){
                    printf ("\n\nO raio de dobramento é 'x'!\n");
                } else if (esp1>=esp[1] && esp1<esp[2]){
                    printf ("\n\nO raio de dobramento é 'y'!\n");
                } else if (esp1>=esp[2]){
                    printf ("\n\nO raio de dobramento é 'z'!\n");
                }
            } else if (strcmp(temp1, temp[cont])!=0){ //AQUI ESTÁ A MINHA DIFICULDADE!!!
                if (cont==6)
                printf ("\nTêmpera sem dobramento!\n");
            }
        }
    } else if (liga1!=liga[cont]){ /*Se a liga não for igual a nenhuma liga cadastrada, retorna
    msg, essa condição é verifica antes da têmpera, se retornar verdadeiro, o programa nem avalia
    a têmpera e a espessura*/
        if (cont == 3){
            printf ("Liga não contemplada com ensaio de dobramento na Norma ASTM!\n\a");
        }
    }
}

I put the comment "HERE IS MY DIFFICULTY" in the line of the code in which I am having problems. I need this line of code to return "Tempering without bending" to the user only when the tempering is not equal to any previously registered tempering in the temp variable (string vector). It turns out that in the execution of the code, it even returns the message correctly in case of tempering different from those that were registered, but is also showing when the user type a valid temper (appearing the message "Tempering without bending" after the message of "Folding required, etc...")! Can you help me, please?

I do not know if I am using the correct method for such an end (this kind of loop of repetition), so I am open to suggestions and criticism, after all they are welcome in learning!

  • You are using the same variable for alloy index and tempering index, which is within the loop of the first. Use different variables.

2 answers

0

Guys, I was able to solve the problem by changing the validation structure of the data provided by the user. I made a menu using the switch function, where it enters with the standard option to be used to search for the required radius value for folding in the material analysis (this after it enters the alloy data, tempering and thickness). Once the standard is chosen, I evaluate the group to which the material alloy belongs (group 3XXX or 5XXX) and redirect the execution of the program to a corresponding function (considering the standard requested and the material alloy group). For example, the function for the 3XXX alloy in the ASTM standard follows below:

float astm_3xxx(float r, char lg_3xxx[6], char t_3xxx[4], float e_3xxx){
int cont, cont1; //contadores para comparação entre ligas
int cnt, cnt1; //contadores para comparação entre têmperas

char lg1[3][6]={"3004", "3104", "3105"}; /*primeiro índice indica a qtde de ligas e o segundo
a quantidade de caracteres que poderão ser armazenados em cada liga/conteúdo*/
char tp1[5][4]={"H22", "H24", "H26", "H32", "H34"};

for (cont=0;cont<=3;cont++){ //percorrendo os índices de liga
    for (cont1=0;cont1<=4;cont1++){//percorrendo os caracteres de cada índice
        if (lg_3xxx[cont1]==lg1[cont][cont1]){
            cont1++;
            if (lg_3xxx[cont1]==lg1[cont][cont1]){
                cont1++;
                if (lg_3xxx[cont1]==lg1[cont][cont1]){
                    cont1++;
                    if (lg_3xxx[cont1]==lg1[cont][cont1]){
                        for (cnt=0;cnt<=5;cnt++){//percorrendo os índices de têmpera
                            for (cnt1=0;cnt1<=4;cnt1++){//percorrendo os caracteres de cada índice
                                if (t_3xxx[cnt1]==tp1[cnt][cnt1]){
                                    cnt1++;
                                    if (t_3xxx[cnt1]==tp1[cnt][cnt1]){
                                        cnt1++;
                                        if (t_3xxx[cnt1]==tp1[cnt][cnt1]){
                                          cnt1++;
                                          if (t_3xxx[cnt1]==tp1[cnt][cnt1]){
                                                  //TRATAMENTO DAS ESPESSURAS
                                                    if (e_3xxx <= 0.400){
                                                        r = 0;
                                                        printf ("\nEspessura abaixo do range que exige dobramento!\n");
                                                    } else if (e_3xxx > 0.400 && e_3xxx <=1.200){
                                                        r = 2.5;
                                                        return (r);
                                                    } else if (e_3xxx > 1.200 && e_3xxx <= 3.200){
                                                        r = 4.5;
                                                        return (r);
                                                    } else if (e_3xxx > 3.200){
                                                        r = 6.0;
                                                        return (r);
                                                    } 
                                            }  
                                        }
                                    }

                                }
                            }    
                        }    
                    }   
                }
            }
        }
    }
}

}

If the user has informed a 3004 league, for example, it will be redirected to this function, where I have stored some alloys in a string array. I’ll do an informed alloy check to see if it’s one of the registered alloys that requires bending in the norm, running character by character, index by index. If true, I compare the tempera with the registered tempera. If true, I do the treatment for thicknesses and return the radius value to the main() function of the program. If the alloy or tempering are not equal to the registered ones, the function will return r = 0 to the main() function, then I will inform the user that the folding is not required for that material.

I am studying on my own, after long years without studying (I never got to work with programming, stopping because I started working far from the IT area, even before I entered this market). So I guess there are better ways to make the code cleaner, I guess. If anyone has any suggestions or comments, please do not be shy. I am here to learn!

0

The best way would be to create a search function that returns true or false:

boolean busca(TIPO_AQUI temp1,TIPO_AQUI temp)
{
    for (cont=0; cont<=6;cont++)
        if (strcmp(temp1,temp[cont])==0)
            return true;    
    return false;
}

So you would use it as follows:

else if (!busca(temp1,temp))
    printf ("\nTêmpera sem dobramento!\n");

Some details of your code should be changed:

  1. The same variable cont=0 is being used in two loops, theoretically as one loop is inside the other, this can affect your code.

Then I suggest renaming the variables.

  • Filipe, I kept trying to encode on the basis of the same race and, although I believe that I can leave the code a little cleaner over time, I managed to solve the issue in another way. I made another structure to validate the data, which worked super well (at least, the general functioning). There are still some outstanding issues that I will try to resolve, if I fail, I will ask for help from the users of the site. But thank you, anyway...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.