The whole name doesn’t appear

Asked

Viewed 167 times

2

In C language as I can show the whole name when asked to enter the user name, I think it is not correct the definition of the char, my code is this:

int main(int argc, char** argv) {
char nome[100];
fflush(stdin);
int idade;// para garantir que o inteiro utilza 2Bytes
int montdep;// Inteiro utiliza 4Bytes
long int numconta;// para garantir que inteiro utiliza sempre 4Bytes

printf("Introduza o seu nome:\n");
scanf("%s %s",nome);
    printf("Introduza a sua idade:\n");
    scanf("%d",&idade);

    if(idade<=-1)
    {
        printf("Não se aceita valores negativos\nReinicie programa");
    }  
    else
    {
        printf("Introduza o valor a depositar:\n");
        scanf("%d",&montdep);
        printf("Introduza o numero de conta :\n");
        scanf("%ld",&numconta);  
        printf("%s de %d anos, depositou %d€ na conta %ld",nome,idade,montdep,numconta);
    }    
}

and the result is this

Introduza o seu nome:
Jose Esquina
Introduza a sua idade:
25
Introduza o valor a depositar:
2222
Introduza o numero de conta :
22222222222222
Jose de 25 anos, depositou 2222€ na conta 22222222222222

I wanted the whole name that was introduced to appear and only Jose

2 answers

1


Place scanf("%s %s" reads two words for two different variables and not just one. If you want to read several words for one string and end only at Enter can change the reading to:

scanf("%[^\n]s",nome);

That will only end in \n. Note that this can be dangerous however if the person entering the data puts more data than is supposed, in this case the 100 that defined.

If you want to get around this situation you can switch to a reading with fgets, which already allows you to specify the maximum read size:

fgets(nome, 100, stdin);

Note on 100 as the second parameter which is the maximum number of characters to read. However \n introduced by the user will be on nome also, which you can remove by doing:

nome[strlen(nome) - 1] = '\0';

Documentation for the scanf and to the fgets

Example in Ideone

  • thanks Isac, it worked as wanted with the scanf that explained to me

0

You forgot to & store name, note that your line is ,nome, I switched to => &nome and also the char character for string but can also leave as char if you want, and note that you have two %s %s to store a single variable, this is not necessary

int main(int argc, char** argv) {
string nome[100];
fflush(stdin);
int idade;// para garantir que o inteiro utilza 2Bytes
int montdep;// Inteiro utiliza 4Bytes
long int numconta;// para garantir que inteiro utiliza sempre 4Bytes

printf("Introduza o seu nome:\n");
scanf("%[^\n]s", &nome);

printf("Introduza a sua idade:\n");
scanf("%d", &idade);

if(idade<=-1){
    printf("Não se aceita valores negativos\nReinicie programa");
} else {
    printf("Introduza o valor a depositar:\n");
    scanf("%d",&montdep);
    printf("Introduza o numero de conta :\n");
    scanf("%ld",&numconta);  
    printf("%s de %d anos, depositou %d€ na conta %ld",nome, idade, montdep, numconta);
}    
}
  • gives me error in string, does not let define so

  • puts as char again and tries, and puts the error here that appears or the displayed result

Browser other questions tagged

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