Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Programming Switch Statement Menu Validation help please! I have a program that's working ok but when it comes to the menu, it's not validating

C++ Programming Switch Statement Menu Validation help please!

I have a program that's working ok but when it comes to the menu, it's not validating when a user inputs an alphabet character or special character. This is located within the Main.cpp at the switch statements. The program validates when the user enters an integer other than the numeric options in the menu. Is there any way this program can catch also alphabetical characters AND special characters? The program should validate numerical values not part of the menu option, alphabetical characters and special characters. If a user inputs any of those as a menu option an error message should appear like: "Invalid entry." Any help is greatly appreciated!! Here is my code:

Main.cpp

#include

#include

#include "romanType.h"

using namespace std;

int main()

{

//Declare variables

string roman;

char continueNum = 'y';

int choice;

romanType print;

while (continueNum == 'y' || continueNum == 'Y')

{

//Execute program instructions

cout << "Enter any roman numeral then I will convert it to it's decimal equivalent (upper case characters only): ";

cin >> roman;

cout << endl;

//do-while loop so user can choose if he or she wants to print or convert the roman numeral

do {

cout << " What would you like to do?" << endl;

cout << "1. Print Roman Numeral" << endl;

cout << "2. Convert to Decimal Number" << endl;

cout << "3. Exit" << endl;

cin >> choice;

cout << endl;

switch (choice)

{

//Print Roman Numeral

case 1:

print.setRoman(roman);

print.printRoman(); //Prints Roman Numeral

break;

//Convert Roman Numeral

case 2:

print.setRoman(roman);

print.printDecimal(); //Prints Decimal

break;

//Exit program

case 3:

cout << "Good-bye!" << endl;

system("pause");

exit(0);

//Default message if user inputs invalid menu option

default:

cout << "Invalid option." << endl;

break;

}

} while (choice != 3);

}

}

romanType.cpp

#include "romanType.h"

//DEFAULT CONSTRUCTOR

romanType::romanType(string new_roman_numeral)

{

roman_numeral = new_roman_numeral;

decimal_number = 0;

}

//setRoman assigns its parameter to new_roman_numeral and invokes toDecimal

void romanType::setRoman(const string& new_roman_numeral)

{

roman_numeral = new_roman_numeral;

toDecimal();

}

//Print Roman Numeral

void romanType::printRoman()

{

cout << "The entered roman numeral is: " << roman_numeral << endl;

}

//Print Decimal

void romanType::printDecimal()

{

cout << "The decimal equivalent of " << roman_numeral << " is: " << decimal_number << endl;

}

//Conversion to Decimal

void romanType::toDecimal()

{

int length = roman_numeral.length();

int previous = 0;

bool error = false;

int i = 0;

int sum = 0;

while ((error == false) && (i < length))

{

switch (roman_numeral[i])

{

case 'M':

sum += 1000;

if (previous < 1000)

{

sum -= 2 * previous;

}

previous = 1000;

break;

case 'D':

sum += 500;

if (previous < 500)

{

sum -= 2 * previous;

}

previous = 500;

break;

case 'C':

sum += 100;

if (previous < 100)

{

sum -= 2 * previous;

}

previous = 100;

break;

case 'L':

sum += 50;

if (previous < 50)

{

sum -= 2 * previous;

}

previous = 50;

break;

case 'X':

sum += 10;

if (previous < 10)

{

sum -= 2 * previous;

}

previous = 10;

break;

case 'V':

sum += 5;

if (previous < 5)

{

sum -= 2 * previous;

}

previous = 5;

break;

case 'I':

sum += 1;

if (previous < 1)

{

sum -= 2 * previous;

}

previous = 1;

break;

default:

cout << roman_numeral[i] << " is not a Roman Numeral!" << endl;

error = true;

sum = 0;

}

i++;

}

decimal_number = sum;

}

romanType.h

#include

#include

using namespace std;

class romanType

{

public:

romanType(string = ""); //default constructor

void setRoman(const string& new_roman_numeral); //assigns its parameter to new_roman_numeral and invokes toDecimal

void printRoman(); //displays roman_numeral

void printDecimal(); //displays decimal_number

private:

string roman_numeral;

unsigned decimal_number;

void toDecimal(); //converts too decimal

};

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Medical Image Databases

Authors: Stephen T.C. Wong

1st Edition

1461375398, 978-1461375395

More Books

Students also viewed these Databases questions