Why doesn’t the while stop?

Asked

Viewed 342 times

6

#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 cycle while continue until the character is entered S or until introduced 20 times

  • 1

    Apparently because you didn’t initialize the value of i

  • i=0 resolve then?

  • Probably solve.

  • still equal any other idea? xD

  • Always start your local variables.

3 answers

2


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

It does not work because it is comparing to something that already gives false face. There was even another logic error in the condition.

Also the code doesn’t do what you think it does. To have a array of strings in C a array two-dimensional, one for the strings and another for the characters, since C does not have string abstract.

I’ve 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] == '\0') break;
    }
    for (int i = 0; i < cont; i++) printf("\nElenco: %s", elenco[i]);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

1

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

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

Also, I recommend using a do while in place of while normal.

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

Because it makes no sense to run the test before receiving user input.

In addition, the use of the function gets is not recommended, consider using the fgets or similar.

https://www.tutorialspoint.com/c_standard_library/c_function_fgets.htm


Apparently you tried to create an array of strings. In C, a string array is an array of char two-dimensional.

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

char elenco[20][50]
  • while I had but was not giving so I switched to while to see if it made a difference

  • Gabriel have some network or something I can use to discuss some questions with you pff?

  • @Kira I added an extra piece of information to my answer because I noticed another problem in your code.

  • @Kira At the moment I can not talk. You can open other questions for your questions, so anyone can help you.

  • ok thanks anyway ^^

-1

Your while loop becomes infinite because you use the comparison "OR" ||, even if it is larger than 20 it will still be different from s, then you ask, but it passes 20 and I type the s and also does not leave the program. This is because you compare the new entered value. Example Type s and you are in the cast position[0]; soon after I typed the counter "i" increments in the case going to position 1 you would be comparing cast[1] and not cast[0] containing’s' for those reasons does not come out of the loop. After still have other problems, you can not store several names in an array, this has to use matrix(array) in C would have to declare cast[30][30] because if you want to access the second name for example you would access cast[1]... Let’s have the detailed answer.

Includes the string library. h to compare strings and use memset, I declared the array cast as [30][30]. I did a 20 and gave fflush(stdin) to clear the memory buffer then there in "if" I compare if the guy typed "s" if I play the position that the loop stopped for the variable "k" if it does not enter the loop it is worth 20 and use the memset to reset the position of the "s" that it will not be a cast, then I give a break to close the loop, in the other is I simply put while it is smaller than the variable k printa the cast. Doubts comment :)

#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]);
}
}

Browser other questions tagged

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