class Employee { public: Employee() {}; ~Employee() {}; void Add_data(); // добавить данные void Show_data(); // просмотреть данные void Delete_data(); // удалить данные //переменные для персонала char name_employee; // ограничение до 100 символов float employe_id; // табельный номер float ExpEmployee; // стаж float PriceOfHour; // Зарплата за час float HoursEmployee; // кол-во часов работы float GetTotalSalary(float hoursalary, float hours) const { return (hoursalary * hours); }; // результат зарплаты float GetBonus(float exp, float hoursalary) const; // прибавка }; // Метод класса, который выполняет роль добавление данных в файл void Employee::Add_data() { // временные перменные int ans(0); char name_employee[30]; float employeid(0), exp(0), hourprise(0), hours(0); // Работа с файлом do { cout << "To keep previous record?\n1 - Yes\n2 - No\n"; cin >> ans; } while (!ans == 1 || !ans == 2); /* создать объект для записи и автоматически переставляет указатель текущего символа потока в конец. */ ofstream fout("db_employee.txt", ios_base::app); if (ans==2) // уекаем поток после открытия // и открывает файл для записи. ofstream fout("db_employee.txt", ios_base::trunc | ios_base::out); // Работа с данныеми об сотруднике cout << "Enter employee FIO: "; cin.sync(); //очистка буфера fgets(name_employee, sizeof(name_employee), stdin); //// считать строку из стандартного потока ввода fout << "\nFIO: " << name_employee << endl; //запись в файл cout << "\nEnter Eployee id: "; cin >> employeid; fout << "Eployee id: " << employeid << endl; cout << "\nEnter employee experience (in hours): "; cin >> exp; fout << "Experience (in hours): " << exp << endl; cout << "\nEnter employee salary per hour: "; cin >> hourprise; fout << "Salary per hour: " << hourprise << endl; fout << "Resulting salary: " << Employee::GetTotalSalary(hourprise, exp) << endl; fout << "Bonus: " << Employee::GetBonus(exp, hourprise) << endl << endl; fout.close(); } 

enter image description here I program on Vicual Studio 2015 C ++. Everything works correctly, except for FIO input. Please, help ))

Reported as a duplicate by the participants αλεχολυτ , aleksandr barakin , Sasha Omelchenko , Community Spirit 22 May '17 at 20:39 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • one
    Just recommendation - if possible, avoid posting links to pictures, if there is a possibility of copying + paste text information. - 0xdb

1 answer 1

Standard question ...

Everything is as usual - after reading cin >> ans , the buffer remains '\ n' ...

To you after that

 } while (!ans == 1 || !ans == 2); 

just need to ignore all characters before the newline character -

 cin.ignore(numeric_limits<streamsize>::max(), '\n');