mercredi 29 mars 2017

How to prevent skipping data input without using cin.ignore() in C++ [duplicate]

This question already has an answer here:

#include "stdafx.h"
#include<iostream>
#include<fstream>
#include<string>

using namespace std;
int User();
int User()
{
int X = 0 , Y = 0;
do
{
    system("cls");
    cout << "Welcome \n"
        << "1.) Manager \n";

    cin >> X;
    switch (X)
    {
    case 1:Y = 1;
        break;
    default:
        Y = 0;
        break;
    }

} while (Y = 0);
return X;
}

void Check()
{
system("cls");
char PasswordiF[100];
char Passwordou[100];

fstream infile("Password.txt", ios::in | ios::out | ios::app);
infile.getline(PasswordiF, 100);
cout <<"Enter\n";
cin.getline(Passwordou, 100);
cout << endl << PasswordiF << endl << Passwordou;
}

int main()
{
int USER;
USER = User();

switch (USER)
{
case 1:
    Check();
    break;
}
return 0;
}

I cant enter the data in the program when i use getline. I cant use string because i am not allowed to. PasswordiF is the password inside the file which is being read properly as shown in the picture . But the problem is that i can`t read Passwordou from the user without using cin.ignore(); in the previous line. When i am going to continue through with my project i must jump from function to function so is there any other alternative to change the above code to accept my data without using cin.ignore().

Snapshot

Aucun commentaire:

Enregistrer un commentaire