How to have a cycle interval, up to n and up to 0

Asked

Viewed 99 times

1

In a validation function that passes the tests all will return 1, and if you enter one of the conditions will give return to 0.

I want to cycle until the line breaks, so I’m using the interval:

for(int i=0;dados[4][i]!='\n';i++){
    if(!isdigit(dados[4][i])){
         return 0;
    }
 }

The file has already been split and stored in a matrix dados[4][20], being the lines of the genre

 4 ; Visitante ; 3 ; 0 ; 3 \n
 4 ; Visitante ; 3 ; 0 ; 3 \n
 4 ; Visitante ; 3 ; 0 ; 3 \0

However, at the last line of the file, there is no more line break but a \0, soon will not allow us to validate the last line

  • I suggest you add more content so we can help. This is a run file? Parsed lines already? is a parse of line fields? is n a line break? is it an empty line? I suggest you edit and rephrase the question.

  • I’ll rephrase then

  • But how was the array created? Because the description gives the idea that it has 4 rows and 20 columns, but then the for It seems to only advance in the columns instead of through the rows. If the array was created with a certain size (20) why not iterate up to 20 or up to the amount that was read from the file ? That would be easier and more intuitive. Regardless of all this "given what I’ve been able to interpret from the question" it seems to me that I just change the condition to dados[4][i]!='\n' && dados[4][i]!='\0' would work.

  • @Brunobacelar Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already done so. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site.

2 answers

2

  • This does not work because if it is n it is no longer 0....

  • so it won’t work!

  • @Yes Michelsimões, I forgot to change the sign, thank you. Who negative can already take the negative.

0

I was able to solve the problem by adding a condition inside the for:

 for(int i=0;dados[4][i]!='\n';i++){
    if(dados[4][i]=='\0'){
        break;
    }
    if(!isdigit(dados[4][i])){
         return 0;
    }
 }

Browser other questions tagged

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