String reading with two or more names

Asked

Viewed 2,799 times

0

How do I read a string with a compound name for example: "Vitor Martins"? I’m doing this program that stores a student’s name and note but it gives a bug when you type two names.

    #include<stdio.h>
    #include<string.h>

    #define alunos 3

int main(){
int x;
float nota[alunos];
char nome[15];

for(x=0; x<alunos; x++){
    printf("Digite o nome do %d(o) aluno: ", x+1);
    scanf("%s", nome);

    printf("Digite a nota do aluno %s: ", nome);
    scanf("%f", &nota[x]);

    printf("\n");
}

}

  • 2

    read this: http://answall.com/q/42981/101. This question is probably duplicated if not from some other, this problem is recurring.

4 answers

1

you can use:

fgets (nome, 15, stdin); 

or you can use the scanf even applying a regex that will eliminate a space, because if I remember well the scanf recognizes a space as a possible line break to end its reading, example:

scanf ("%[^\n]%*c", nome);
  • Still giving the same problem, when typing the other students' names the program jumps to the note and does not let type the name, only the note

  • After performing a scanf it is good to clean the input buffer with fflush(stdin), provavelemnte this will end your problem of skipping reading.

  • Beware! The function fflush only operates in buffers output and its use in an input buffer (ex: fflush(stdin)) is not recommended, has undefined behavior (link - in English)

1


The general rule is: don’t try to do anything complicated with scanf.

The purpose of the scanf function is to work with structured data, normally read from a file.

To work with interactive data reading you usually need to use a library, such as "ncurses" on Linux, or else analyze each line "manually", which is laborious.

I’ve never seen a program "in real life" that uses scanf, only in examples and school exercises. :)

Having said all this, you can do something, as in the example below, but I think it does not make up for the work, it will always end up being "half mouth". :)

#include<stdio.h>
#include<string.h>

#define N_ALUNOS 3

static void ignoreRestOfLine(void)
{
   char c;
   while((c = getchar()) != '\n' && c != EOF)
        /* discard */ ;
}

int main()
{
   int i;
   float nota[N_ALUNOS];
   char nome[15];

   for (i = 0; i < N_ALUNOS; i++)
   {
      printf("Digite o nome do %d(o) aluno: ", i+1);
      scanf("%14[ .a-zA-Z]%*[^\n]\n", nome);

      printf("Digite a nota do aluno %s: ", nome);
      scanf("%f", &nota[i]);
      ignoreRestOfLine();

      printf("\n");
   }

}

0

Hey Friends I know the post is old, but I will post here a solution for anyone starting in C++ and for some reason came here... copy and paste this also in the line above int x: setlocale(LC_ALL, "English"); //serves for character accentuation inserir a descrição da imagem aqui

  • It is difficult to copy and paste code that is in an image,

  • Sorry... I’ll leave the link to my repository on github https://github.com/marcosranes/VS_Github_Repos/blob/master/Alunos_StackOverFlow/Alunos_SOF.cpp

-2

If I am not mistaken, it is possible to use the number of characters in the %s which will include space as a character. Ex:

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

This allows up to 50 characters in the variable (including spaces) and ends when pressing Enter (using \n).

Browser other questions tagged

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