Menu
CoddyTech

Final: Booking Desk

Lesson 48 of 48 in Coddy's C++ for Go Developers course.

challenge icon

Challenge

Easy

Input: nonnegative capacity then signed operations. Positive values attempt reservations; negative values attempt releases. Reserve succeeds only if enough free capacity; release succeeds only if that many are reserved. Zero succeeds without change. Desk.used and free expose current counts; rejected operations leave both unchanged. Print used, free, rejected count.

Editable modules: Desk.cpp, Desk.hpp. Each required type has its own named header and implementation. Preserve all public signatures. The locked main.cpp reads stdin and calls the API directly. It also checks public API behavior independently where stated. Return values rather than printing inside the API. Integer inputs and results fit signed int; text is ASCII unless stated otherwise.

Try it yourself

#include "Desk.hpp"
#include <iostream>
int main(){
{Desk a(4),b(9);if(!a.reserve(3)||a.reserve(2)||a.release(4)||!a.release(1)||a.used()!=2||a.free()!=2||b.free()!=9||a.reserve(-1)||a.release(-1))return 21;}
std::vector<std::string> rows; std::string line; while(std::getline(std::cin,line)) rows.push_back(line);
std::istringstream in(rows.empty()?"":rows[0]);int n,x,reject=0;in>>n;Desk d(n);while(in>>x){bool ok=x>=0?d.reserve(x):d.release(-x);if(!ok)++reject;}std::cout<<d.used()<<" "<<d.free()<<" "<<reject<<"\n";
}

All lessons in C++ for Go Developers

Practice on your own: Online C++ compiler