Why does the while not stop?

5
#include <stdlib.h>
#include <stdio.h>

int main()
{
    int i,j;
    char elenco[30];

    while(elenco[i]!='s' || i<=20)
    {   
         printf("indique um menbro do elenco ,se quiser sair escreva apenas a letra S");gets(elenco);
         i++;
    }

    for(j=1;j<=i;j++)
    {
        printf("Elenco.:%-30s\n",elenco[j]);
    }
}

I want the while cycle to continue until the S character is entered or until it is entered 20 times

    
asked by anonymous 14.12.2017 / 11:03

3 answers

1

Make the normal case the traditional way, that is, a for and treat the exception that is to exit typing S right after typing out of the loop. You can even do it in a different way ( do-while ), but so it seems appropriate for a start.

It does not work because it's comparing to something that already fakes. There was even another logic error in the condition.

Furthermore the code does not do what it thinks it does. To have a array of strings in C requires a two-dimensional array for strings and another for the characters, since C has no abstract string .

I made other improvements.

#include <stdio.h>

int main() {
    char elenco[20][30];
    int cont = 0;
    for (int i = 0; i < 20; i++, cont++) {
        printf("\nindique um menbro do elenco ,se quiser sair escreva apenas a letra S");
        scanf("%29s", &elenco[i][0]);
        if  (elenco[i][0] == 'S' && elenco[i][1] == '
#include <stdio.h>

int main() {
    char elenco[20][30];
    int cont = 0;
    for (int i = 0; i < 20; i++, cont++) {
        printf("\nindique um menbro do elenco ,se quiser sair escreva apenas a letra S");
        scanf("%29s", &elenco[i][0]);
        if  (elenco[i][0] == 'S' && elenco[i][1] == '%pre%') break;
    }
    for (int i = 0; i < cont; i++) printf("\nElenco: %s", elenco[i]);
}
') break; } for (int i = 0; i < cont; i++) printf("\nElenco: %s", elenco[i]); }

See running on ideone . And no Coding Ground . Also put it in GitHub for future reference .

    
14.12.2017 / 13:16
0

First, you want to compare if the content received by gets is equal to the string "s" . To compare strings you should use the strcmp function, which returns 0 if both strings are equal.

while (strcmp(elenco, "s") != 0 && i < 20) {
   ...
}

In addition, I recommend using do while instead of while normal.

do {
   ...
} while (strcmp(elenco, "s" && i < 20) != 0);

Because it does not make sense to run the test before receiving input from the user.

Additionally, the use of the gets function is discouraged, consider using fgets or similar.

link

Apparently you tried to create an array of strings. In C, an array of strings is an array of char of 2 dimensions.

So if you want to get 20 names and each name will have a maximum of 50 characters, you should declare:

char elenco[20][50]
    
14.12.2017 / 11:43
-1

Your while loop is infinite because you use the comparison "OR" ||, even if it is greater than 20 it will still be different from s, then you ask, but it goes from 20 and I type os and it also does not exit the program . This is because you compare the new value entered. Example Type s and is in the cast position [0]; soon after I typed the counter "i" increases in case going to position 1 you would be comparing cast [1] and not cast [0] that contains the 's' for these reasons does not leave the loop. After you still have other problems, you can not store multiple names in a vector, this has to use array (array) in C would have to declare cast [30] because if you want to access the second name for example you would access cast [ 1] ... Let's have a detailed answer.

Include the string.h library to compare strings and use the memset, I declare the array cast as [30] [30]. I made a fork of 20 and gave fflush (stdin) to clear the memory buffer then there in the "if" I compare if the guy typed "s" if it was typed if I play the position the loop stopped for the variable " k "if it does not enter the loop it is worth 20 and I use memset to zero the position of the" s "that will not be a cast, then I give a break to close the loop, in the other one I just put it while it is smaller than the variable k printa the cast You can leave a response, or trackback from your own site.

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main()
{

int i = 0, k=20;
char elenco[30][30];
for(i=0;i<=20;i++){
fflush(stdin);
printf("indique um menbro do elenco ,se quiser sair escreva apenas a letra S");
scanf("%s",&elenco[i]);
if(strcmp(elenco[i],"s") == 0){
memset(&elenco[i],0,sizeof(elenco[i]));
k = i;
break;
}
}

for(i=0;i<k;i++)
printf("Elenco.:%-30s\n",elenco[i]);
}
}
    
14.12.2017 / 12:21