I’m trying to write a small program however the part that user has to answer if they wanna repeat or not has an issue, which is if use enter without typing anything the cin will just makes empty lines like the screen shot I uploaded. enter image description here
so I tried to use getline but the complier doesn’t recognize it, so I tried to remove std::ws, didnt help; I also tried to change char to string for repeat that way getline can get the entire thing instead of 1 char, but still didnt work here is my code
#include <iostream>
#include <cmath>
int main() {
const double roomCapacity = 10;
double numberofGuests, remianingSpace;
char repeat;
do {
std::cout << "Enter the number of guests: ";
std::cin >> numberofGuests;
if(numberofGuests > roomCapacity){
;
std::cout << "number of guests exceed the room capacity" << std::endl;
}
else {
remianingSpace = roomCapacity - numberofGuests;
std::cout << "Number of guests are within the limit" << std::endl;;
std::cout << "Remaining space is: " << remianingSpace;
}
do {
std::cout << "Would you like to repeat the process? (y/n)";
std::cin >> repeat;
if (std::cin.fail()) {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), 'n');
}
else {
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), 'n');
}
if (repeat != 'y' && repeat != 'Y' && repeat != 'n' && repeat != 'N') {
std::cout << "Invalid inpiut. Please enter 'y or 'n'" << std::endl;
}
} while (repeat != 'y' && repeat != 'Y' && repeat != 'n' && repeat != 'N');
} while (repeat == 'y' || repeat == 'Y');
return 0;
}
I tried to remove std::ws, I tried to make repeat a string
Shahab is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.