Question: I have the below code that runs without errors but the problem I am having is that if you enter the wrong number for radius

I have the below code that runs without errors but the problem I am having is that if you enter the wrong number for radius it tells you correctly that it must be 1 or greater but instead of asking again it moves right to Fill color of the circle instead. If you enter nothing or blank it just goes to another line and doesn't complete. What is wrong with my error checking code?
// Circle.h
#ifndef CIRCLE_H
#define CIRCLE_H
#include
class Circle {
private:
int radius; // Radius of the circle (must be 1 or greater)
std::string colorFill; // Fill color of the circle (cannot be blank)
public:
// Constructors
Circle(); // Zero-argument constructor
// Functions
double calcDiameter() const; // Calculate and return the diameter of the circle
double calcArea() const; // Calculate and return the area of the circle
double calcCircumference() const; // Calculate and return the circumference of the circle
void setRadius(int r); // Set the radius of the circle
int getRadius() const; // Get the radius of the circle
void setColorFill(const std::string& color); // Set the fill color of the circle
std::string getColorFill() const; // Get the fill color of the circle
// Non-class function
void displayCircleInfo() const; // Display the formatted output of the circle information
};
#endif
// Circle.cpp
#include "Circle.h"
#include
#include
const double PI =3.14;
// Constructors
Circle::Circle() : radius(0), colorFill(""){}
// Functions
double Circle::calcDiameter() const {
return static_cast(radius)*2.0;
}
double Circle::calcArea() const {
return PI * pow(static_cast(radius),2.0);
}
double Circle::calcCircumference() const {
return 2.0* PI * static_cast(radius);
}
void Circle::setRadius(int r){
if (r >=1){
radius = r;
}
else {
std::cerr << "Error: Radius must be 1 or greater." << std::endl;
}
}
int Circle::getRadius() const {
return radius;
}
void Circle::setColorFill(const std::string& color){
if (!color.empty()){
colorFill = color;
}
else {
std::cerr << "Error: Fill color cannot be blank." << std::endl;
}
}
std::string Circle::getColorFill() const {
return colorFill;
}
// Non-class function
void Circle::displayCircleInfo() const {
std::cout << "Circle Information:" << std::endl;
std::cout << "Radius: "<< getRadius()<< std::endl;
std::cout << "Fill Color: "<< getColorFill()<< std::endl;
std::cout << "Diameter: "<< calcDiameter()<< std::endl;
std::cout << "Area: "<< calcArea()<< std::endl;
std::cout << "Circumference: "<< calcCircumference()<< std::endl;
}
// CircleDriver.cpp
#include "Circle.h"
#include
#include
void displayCircleInfo(const Circle& circle){
std::cout << std::setw(15)<< std::left << "Radius:" << std::setw(10)<< std::left << circle.getRadius()<< std::endl;
std::cout << std::setw(15)<< std::left << "Diameter:" << std::setw(10)<< std::left << circle.calcDiameter()<< std::endl;
std::cout << std::setw(15)<< std::left << "Area:" << std::setw(10)<< std::left << circle.calcArea()<< std::endl;
std::cout << std::setw(15)<< std::left << "Circumference:" << std::setw(10)<< std::left << circle.calcCircumference()<< std::endl;
std::cout << std::setw(15)<< std::left << "Color Fill:" << std::setw(10)<< std::left << circle.getColorFill()<< std::endl;
}
int main()
{
std::cout << "Brandon Gauntt -- Lab 2- Class Creation "<< std::endl;
int radius;
Circle circle;
do {
std::cout << "Radius of Circle as a positive number (-1 to exit): ";
std::cin >> radius;
if (radius ==-1){
break;
}
circle.setRadius(radius);
std::string color;
std::cout << "Fill Color of Circle (blank not allowed): ";
std::cin >> color;
circle.setColorFill(color);
displayCircleInfo(circle);
// Pause for the user to read the displayed information
system("pause");
} while (true);
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!