cataninja Posted December 28, 2015 at 05:02 PM Report Share #591106 Posted December 28, 2015 at 05:02 PM (edited) Olá pessoal 🙂 tenho aqui um problema. Eu queria que o programa parasse quando não insiro nenhum nome mas, mesmo com o break, ele não termina como deve ser. Alguém me pode ajudar? #include <stdio.h> #include <string.h> int main() { char nome[50], sobrenome[50]; while (1) { printf("Nome: "); scanf("%s", nome); printf("Sobrenome: "); scanf("%s", sobrenome); if (strlen(nome)==0) break; else printf("%s %s\n", nome, sobrenome); } } Edited December 28, 2015 at 06:36 PM by thoga31 GeSHi Link to comment Share on other sites More sharing options...
HappyHippyHippo Posted December 28, 2015 at 05:10 PM Report Share #591108 Posted December 28, 2015 at 05:10 PM o teu problema é que o scanf espera ler alguma coisa, ignorando tudo que seja caracteres '\n' que lhe mandes comer. ter um break ou não, é completamente irrelevante porque nunca sais da função scanf ... esperimenta este código, carregando comente no ENTER: #include <stdlib.h> #include <stdio.h> int main(int argc, char** argv) { char nome[50] = {0}; printf("Nome : "); fflush(stdout); scanf("%s", nome); printf("Readed : %s\n", nome); return 0; } para fazeres o que esperas, deverás usar outro tipo de função: #include <stdlib.h> #include <stdio.h> #include <string.h> int main(int argc, char** argv) { char nome[50] = {0}; size_t tamanho = 0; printf("Nome : "); fflush(stdout); fgets(nome, 50, stdin); tamanho = strlen(nome); if (nome[tamanho - 1] == '\n') nome[tamanho - 1] = 0; printf("Readed : %s\n", nome); return 0; } 1 Report IRC : sim, é algo que ainda existe >> #p@p Portugol Plus Link to comment Share on other sites More sharing options...
cataninja Posted December 28, 2015 at 05:26 PM Author Report Share #591110 Posted December 28, 2015 at 05:26 PM Já entendi, muito obrigada! 😉 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now