-1
#include "stdafx.h"
#include <iostream>
#include <string.h>
using namespace std;
struct dateS
{
    int day;
    int month;
    int year;
};
struct book
{
    char name[20];
    char author[20];
    char category[20];
    dateS currentDate;
    int number;
    int price;
};
dateS *dates[100];
book *mas[100];
int _count;
void line(int x)
{
    if (x == 1)
     cout << "------------------------------" << endl;
    else
        cout << endl << "=== === === === === === === === === === === === === === === === === === === ===" << endl << endl;
}
void addNew()
{
    mas[_count] = new book;
    cout << "Enter book data:" << endl << endl;
    cout << "Name: ";
    cin.getline(mas[_count]->name,20);
    line(1);
    cout << "Author: ";
    cin.getline(mas[_count]->author,20);
    line(1);
    cout << "Category: ";
    cin.getline(mas[_count]->category,20);
    line(1);
    cout << "Date of receipt [dd.mm.yyyy]: ";
    char *temp;
    temp = new char[11];
    cin >> temp;
    mas[_count]->currentDate.day = atoi(strtok(temp, "."));
    mas[_count]->currentDate.month = atoi(strtok(temp + 3, "."));
    mas[_count]->currentDate.year = atoi(strtok(temp + 6, "."));
    line(1);
    cout << "Number of copies: ";
    cin >> mas[_count]->number;
    line(1);
    cout << "Price: ";
    cin >> mas[_count]->price;
    line(1);
    _count++;
}
void showAll()
{
    for (int i = 0; i < _count; i++)
    {
        cout << "Name: " << mas[i]->name << endl;
        line(1);
        cout << "Author: " << mas[i]->author << endl;
        line(1);
        cout << "Category: " << mas[i]->category << endl;
        line(1);
        cout << "Date of receipt: " << mas[i]->currentDate.day << "." << mas[i]->currentDate.month << "." << mas[i]->currentDate.year << endl;
        line(1);
        cout << "Number of copies: " << mas[i]->number << endl;
        line(1);
        cout << "Price: " << mas[i]->price << endl;
        line(1);
    }
}
void findCtgry()
{
    char *x;
    x = new char[20];
    cout << "Enter category that you want to find: ";
    cin >> x;
    //*x = tolower(*x);
    cout << endl << "Books of this category:" << endl;
    for (int i = 0; i < _count; i++)
    {
        if (!strcmp(x, mas[i]->category))
            cout << i + 1 << "." << mas[i]->name << endl;
    }
}
void findAuth()
{
    char *y;
    y = new char[20];
    cout << "Enter author  that you want to find: ";
    cin >> y;
    //*y = tolower(*y);
    cout << endl << "This author's boks:" << endl;
    for (int i = 0; i < _count; i++)
    {
        if (!strcmp(y, mas[i]->author))
            cout << i + 1 << "." << mas[i]->name << endl;
    }
}
/*void sortDate()
{
    for (int i = _count - 1; i < _count; i++)
    {
        for (int j = 0; j < i; j++)
        {

        }
    }
}*/

int _tmain(int argc, _TCHAR* argv[])
{
    _count = 0;
    cout << "Add at least one book." << endl;
    line(2);
    addNew();
    line(2);
    int choice;
    //choice = new int;
    for (;;)
    {
        cout << "What do you want to do?" << endl;
        line(1);
        cout << "1 - Add new book." << endl;
        cout << "2 - Show all books data." << endl;
        cout << "3 - Find a book by category." << endl;
        cout << "4 - Find a book by author." << endl;
        cout << "5 - Sort books by date of receipt." << endl;
        cout << "6 - Show the price of all books." << endl;
        cout << "7 - Exit." << endl << "Make your choice: ";
        cin >> choice;
        line(2);
        switch (choice)
        {
        case 1:
        {
            addNew();
            line(2);
            break;
        }
        case 2:
        {
            showAll();
            line(2);
            break;
        }
        case 3:
        {
            findCtgry();
            line(2);
            break;
        }
        case 4:
        {
            findAuth();
            line(2);
            break;
        }
        case 5:
        {
            //sortDate();
            line(2);
            break;
        }
        case 6:
        {
            //price();
            line(2);
            break;
        }
        case 7:
        {
            cout << "Goodbye!" << endl << endl;
            system("pause");
            return 0;
        }
        default:
        {
            cout << "Wrong choice. Please try again!" << endl;
            line(2);
            system("pause");
            return 0;
        }
        }
    }
    cout << endl;
    system("pause");
    return 0;
}

Проблема в том, функция addNew() позволяет ввести название книги только первый раз, во всех остальных случаях она просто пропускает ввод названия. Не обращайте внимания на остальной бред в коде, я застрял именно тут. Пробовал cin.clear(), не помогло.

3
  • 1
    Operator >> оставляет в потоке все whitespaces, включая символ новой строки. И getline его считывает. clear не помогает, поскольку всего лишь убирает флаги состояния потока. Нужна функция ignore(), которая прочитает остаток строки и уберет его. (После cin >> choice) stackoverflow.com/a/1744756/8491726 8 окт 2017 в 13:08
  • Спасибо, заработало)
    – Далер
    8 окт 2017 в 13:11
  • @ArtemyVysotsky для ответов есть форма ниже, пожалуйста
    – sanmai
    12 окт 2017 в 1:06

1 ответ 1

0

Operator >> оставляет в потоке все whitespaces, включая символ новой строки. И getline его считывает.

clear не помогает, поскольку всего лишь убирает флаги состояния потока.

Нужна функция ignore(), которая прочитает остаток строки и уберет его. (После cin >> choice)

cin >> choice;
cin.ignore();
line(2);

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.