Проблемы со структурами данных на книжной полке - PullRequest
1 голос
/ 24 марта 2019

Я делаю книжную полку шириной s (1 Проблема, с которой я сталкиваюсь, заключается в том, что при var = 'E' программа должна отобразить оставшиеся книги на полке, а затем выйти из этой проблемы и перейти к другой проблеме, но когда 'E' введена, оставшиеся книги на полке не будет отображаться, и программа не будет завершена. Я попытался возиться с условием циклов while, которое вложено в общий цикл while.

#include <iostream>
#include <vector>

using namespace std;

struct book{
    int id;
    int w;
};

int main(){
//std::vector::~vector
    //create instance of book
    book my_book;
    //initialize the placeholders
    int s, removed_book, back_width;
    char var;
    //create the vector 
    vector<book>shelf;
    while(true){
        //enter the s value 
        s = 0;
        cout << "enter the s value: " << endl;
        cin >> s;
        int w_total = 0;

        //be able to exit the program
        if(s == -1){
            return 0;
        }
        int x = 1;
        //while remaining space
        while(x!=0){              //need to fix this up

            cout << "enter the action(A,R,E): " << endl;
            cin >> var >> my_book.id >> my_book.w;


            //if A
            if(var == 'A'){
                //get info about the book
                /*
                cout << "enter id: " << endl;
                cin >> my_book.id;
                cout << "width(w): " << endl;
                cin >> my_book.w;
                */
                w_total += my_book.w;
                shelf.insert(shelf.begin(),my_book);
                cout << "total width(1): " << w_total << endl;

                if(w_total > s){
                    while(w_total >= s){
                        //remove the rightmost(back) book
                        w_total = w_total - shelf.back().w;
                        cout << "total width(2): " << w_total << endl;
                        shelf.erase(shelf.end()-1);
                    }
                }
            }
            //if R
            else if(var == 'R'){
                //cout << "which book to be removed? : " << endl;
                //cin >> removed_book;
                removed_book = my_book.id;
                for(int i = 0; i < s; i++){
                    if(shelf[i].id == removed_book){
                        shelf.erase(shelf.begin()+i);      
                    }
                }
            }
            //if E
            else if(var == 'E'){
                cout << "remaining books on shelf: " << endl;
                for(int i = 0; i < shelf.size(); i++){
                    if(shelf[i].id!=0){
                        cout << "id: "<<shelf[i].id << endl;
                    }   
                }
                //maybe put the display in here?
                x = 1;  
            }
        }
        //print out the remaining shelf

        shelf.clear(); 
        //erase the shelfs(vectors) contents
        //increase problem number
    }
return 0;
}

Ожидаемый результат:

10(shelf width)
A 1 3(Add id width)
A 2 5
E
-->PROBLEM 1: 2 1

1 Ответ

2 голосов
/ 24 марта 2019

cin >> var >> my_book.id >> my_book.w просит пользователя ввести три вещи: символ и два целых числа. Вы должны ввести все три, прежде чем действие в var будет проверено и выполнено.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...