Programme faisant l'impasse sur getline () sans prendre l'entrée d'utilisateur [double]

StackOverflow https://stackoverflow.com/questions/3731529

  •  03-10-2019
  •  | 
  •  

Question

    

Cette question a déjà une réponse ici:

         

Ceci est un problème très étrange, quand mon programme demande à l'utilisateur de l'adresse, au lieu d'attendre l'entrée, il semble ignorer la fonction complètement getline ()

Answerinput:

cout << "would you like to add another entry to the archive? (Y/N):";

cin >> answer;

cout << endl;
cout << endl;

answer = toupper(answer);


 switch(answer)
    {
    case 'Y':
        Entrynumber++;

        cout << "began record number " << Entrynumber << "+ 1." << endl;

        cout << "Enter the last name of the person to be entered" << endl;

        cin >> stringentry;
        cout << endl;

        stringlength = stringentry.length();

        strcpy(Record[Entrynumber].Last_Name, stringentry.c_str());


        Record[Entrynumber].Last_Name[stringlength] = '*';



        cout << "Enter the first name of the person" << endl;

        cin >> stringentry;
        cout << endl;

        stringlength = stringentry.length();

        strcpy(Record[Entrynumber].First_Name, stringentry.c_str());

        Record[Entrynumber].First_Name[stringlength] = '*';

        cout << "Enter the SSN of the person" << endl;
        cin >> Record[Entrynumber].SSN;
        cout << endl;

        cout << "Enter the age of the person" << endl;
        cin >> Record[Entrynumber].Age;
        cout << endl;

        cout << "Enter the address of the person" << endl;


        cin.getline(Record[Entrynumber].Address,70);


        cout << endl;


        stringentry = Record[Entrynumber].Address;

        stringlength = stringentry.length();



        Record[Entrynumber].Address[stringlength] = '*';

        cout << "you entered:" << endl;



        for(jim = 0 ; Record[Entrynumber].Last_Name[jim + 1] != '*' ; jim++)
        {
            cout << Record[Entrynumber].Last_Name[jim];
        }

        cout << ',' ;


        for(jim = 0 ; Record[Entrynumber].First_Name[jim + 1] != '*' ; jim++)
        {
            cout << Record[Entrynumber].First_Name[jim];
        }

        cout << endl;

        cout << Record[Entrynumber].SSN << endl;
        cout << Record[Entrynumber].Age << endl;

        for(jim = 0 ; Record[Entrynumber].Address[jim + 1] != '*' ; jim++)
        {
            cout << Record[Entrynumber].Address[jim];
        }
        cout << endl;
        cout << endl;


        goto Answerinput;
    case 'N':
        cout << "ok" << endl;
        break;
    default:
        cout << "invalid answer" << endl;
        goto Answerinput;
    }

sortie de console

would you like to add another entry to
the archive? (Y/N):Y

began record number 6+ 1. 


 Enter the last name of the person to be entered 
 John


 Enter the first name of the person 
 John

 Enter the SSN of the person  22222222

 Enter the age of the person  22

 Enter the address of the person

 you entered: 
Joh,Joh 
22222222 
22
 *¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦
 //////////////22 more lines of'|'//////////////////////////////////////////////
 ... 
¦¦¦¦¦¦¦¦l3-j

 would you like to add another entry to the archive? (Y/N):

Les deux cin.getline () et getline () font la même chose.

J'utilise MVC ++ 2008.

Tous les champs dans le tableau enregistrement sont struct, Record [Entrynumber] .Address est un tableau de caractères.

Était-ce utile?

La solution

Cin quitte probablement le retour du chariot dans la mémoire tampon qui getLine récupère. Essayez

cin.ignore(1000, '\n');

cin.getline(Record[Entrynumber].Address,70);

Le >> opérateur ne supprime pas le retour à la ligne après la récupération de données, mais ignore les espaces au avant de récupérer des données, alors que GetLine seulement récupère tout ce qui est là-dedans, et supprime le « \ n » après avoir lu car il est en dehors de la ligne, il est 'devient'.

Autres conseils

Voyant que la façon dont il pourrait y avoir d'entrée qui reste dans la mémoire tampon qui est votre getline en train de lire d'abord, je vous suggère de vous effacer la mémoire tampon avant d'essayer d'entrer les données suivantes:

std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top