So I am having an issue with my program not re-prompting after purposefully putting in a wrong input just to make sure my program is able to properly validate and range check. The specific function is on line 77, called "int getObjects() { " where my program asks the user between 4 options on what they would like to convert to different types of temperature scales. When I do not type in an option that is one of the four, the code doesn't give the error message and just kinda stops. It doesn't do anything until I restart the program.
So at first I tried putting cout statements on every line of code that relates to the function to see what might cause the issue, and so far I've had my eyes on line 212 function "bool ValWord".
For my assignment, our teacher requires to follow these specifications, and this one was one of them. To go in depth, "Specification A2 - Input Validate C1 Use input validation to confirm the client entered the correct word. Create a function ValWord which examines the input string and returns a boolean true if the word is correct, false if incorrect. Code incorrect values to trigger re-prompting and reentry of the word. Keep looping until true is returned."
So I put a cout statement on this bool function, and when I did it looped my message hundreds of times over till it crashed my Replit, so I believe that this might be the underlying issue, but I do not know how to solve it. I am only intending for this code to just state the error, reprompt, and loop back.
Here is my code
// Source File Header
// ccctc.cpp
// My name, CISP 360
// Teacher's Name
// 3/26/2023
#include <iostream>
#include <string>
#include <cctype>
#include <ctime>
#include <iomanip>
using namespace std;
// Colors
string color[] = {"\033[0m", "\033[31m", "\033[32m", "\033[34m", "\033[35m", "\033[36m", "\033[33m"};
// Function Prototypes
void ProgramGreeting();
int getTempScale();
string stringtoUpperCase(string input);
int getObjects();
float ValTemp(int object);
void calcTempScale(float tempFahrenheit, int scaleType);
bool ValWord(string);
int Prompt (string prompText, int userInput);
float Prompt (string promptText, float userInput);
int main() {
ProgramGreeting();
int objCode = getObjects();
float temp = ValTemp(objCode);
int scale = getTempScale();
calcTempScale((float) temp, scale);
// Program Ending
cout << color[1] << "End of the Program. Goodbye." << endl;
int pEnd;
cin.clear();
cin >> pEnd;
return 0;
}
// Program Greeting
void ProgramGreeting() {
cout << "ccctc.cpp\nMyName, CISP 360\nTeacherName\n3/26/2023\n" << endl;
time_t now = time(0);
char* date_time = ctime(&now);
cout << "Today's date is: " << date_time << endl;
cout << "Hello user! Welcome to this program. This program is used to convert a Fahrenheit temperature of a cat, a cot, and a cap to either Celsius, Kelvin, or Rankine." << endl;
cout << " " << endl;
}
// Specification C2 -- Select Temp Scale Menu
int getTempScale() {
string input;
while (true) {
cout << color[2] << "MENU OPTIONS" << endl;
cout << color[5] << "============" << color[0] << endl;
cout << "(1) Kelvin" << endl;
cout << "(2) Celsius" << endl;
cout << "(3) Rankine" << endl;
cout << "(4) All of the above" << endl;
cout << color[3] << "Please choose which scale you wish to use: " << color[0];
cin >> input;
cout << endl;
if (input == "1") {return 1;}
if (input == "2") {return 2;}
if (input == "3") {return 3;}
if (input == "4") {return 4;}
cout << color[1] << "Invalid Menu Selection, please enter a proper input." << endl;
}
}
// Specification C1 -- Only Valid Words
int getObjects() {
string object;
while (true) {
cout << color[2] << "MENU OPTIONS" << endl;
cout << color[5] << "============" << color[0] << endl;
cout << "1. Cat" << endl;
cout << "2. Cot" << endl;
cout << "3. Cap" << endl;
cout << "4. Cop" << endl;
cout << color[3] << "Please enter the object you would like to calculate and convert: " << color[0];
cin >> object;
cin.ignore();
object = stringtoUpperCase(object);
while (true) {
if (ValWord(object)) {
cout << "Bruh";
break;
}
}
if (object == "CAT") {
return 1;
}
else if (object == "COT") {
return 2;
}
else if (object == "CAP") {
return 3;
}
else if (object == "COP") {
return 4;
}
else {
cout << "Invalid, please try again! " << endl;
return 0;
}
}
}
bool ValFlo (float tempIn, float upperVal, float lowerVal) {
if (tempIn <= upperVal + 0.2 && tempIn >= lowerVal - 0.2) {
return true;
}
else {
return false;
}
}
float ValTemp (int obj) {
while(true) {
// float tempIn;
float tempIn = Prompt("\nTemperature Range - Cat: 86 to 102 °F.\nTemperature Range - Cot: 54 to 80 °F.\nTemperature Range - Cap: 72 to 88 °F.\nTemperature Range - Cop : 75 to 108 °F", 0.0f);
//cin >> tempIn;
switch(obj)
{
case 1: {
float upperVal = 102;
float lowerVal = 86;
if (ValFlo(tempIn, upperVal, lowerVal)) {
return tempIn;
}
else {
cout << "Your cat's temperature is not viable, please try again. " << endl;
break;
}
}
case 2: {
float upperVal = 80;
float lowerVal = 54;
if (ValFlo(tempIn, upperVal, lowerVal)) {
return tempIn;
}
else {
cout << "Your cot's temperature is not viable, please try again. " << endl;
break;
}
}
case 3: {
float upperVal = 88;
float lowerVal = 72;
if (ValFlo(tempIn, upperVal, lowerVal)) {
return tempIn;
}
else {
cout << "Your cot's temperature is not viable, please try again. " << endl;
break;
}
}
case 4: {
float upperVal = 108;
float lowerVal = 75;
if (ValFlo(tempIn, upperVal, lowerVal)) {
return tempIn;
}
else {
cout << "Your cot's temperature is not viable, please try again. " << endl;
break;
}
}
}
}
}
void calcTempScale (float tempFahrenheit, int scaleType) {
float kelvins = ((tempFahrenheit - 32) * 5/9) + 273.15;
float celsius = (tempFahrenheit - 32) * 5/9;
float rankine = (tempFahrenheit + 459.67);
switch (scaleType)
{
case 1:
cout << "Your temperature in kelvin is: " << kelvins << endl;
break;
case 2:
cout << "Your temperature in celsius is: " << celsius << endl;
break;
case 3:
cout << "Your temperature in rankine is: " << rankine << endl;
break;
case 4:
cout << "Your temperature in kelvin is: " << kelvins << endl;
cout << "Your temperature in celsius is: " << celsius << endl;
cout << "Your temperature in rankine is: " << rankine << endl;
break;
default:
cout << "Error" << endl;
break;
}
}
string stringtoUpperCase(string input) {
for (int i = 0; i < input.length(); i++)
{
input[i] = toupper(input[i]);
}
return input;
}
bool ValWord (string objectChoice) {
if (objectChoice == "CAT" || objectChoice == "COT" || objectChoice == "CAP" || objectChoice == "COP")
return true;
else
return false;
}
int Prompt(string promptText, int userInput) {
int numInput;
cout << promptText << endl;
cout << "\nChoose between 1-4: ";
cin >> numInput;
return numInput;
}
float Prompt(string promptText, float userInput) {
float numInput;
cout << promptText << endl;
cout << "\nEnter a viable temperature value: \n";
cin >> numInput;
return numInput;
}
Output:
ccctc.cpp
MyName, CISP 360
TeacherName
3/26/2023
Today's date is: Sun Mar 26 07:15:38 2023
Hello user! Welcome to this program. This program is used to convert a Fahrenheit temperature of a cat, a cot, and a cap to either Celsius, Kelvin, or Rankine.
MENU OPTIONS
============
1. Cat
2. Cot
3. Cap
4. Cop
Please enter the object you would like to calculate: cat
Bruh
Temperature Range - Cat: 86 to 102 °F.
Temperature Range - Cot: 54 to 80 °F.
Temperature Range - Cap: 72 to 88 °F.
Temperature Range - Cop : 75 to 108 °F
Enter a viable temperature value:
86
MENU OPTIONS
============
(1) Kelvin
(2) Celsius
(3) Rankine
(4) All of the above
Please choose which scale you wish to use: 4
Your temperature in kelvin is: 303.15
Your temperature in celsius is: 30
Your temperature in rankine is: 545.67
End of the Program. Goodbye.