My code compiles but it doesn\'t run properly. My switch statement is not callin
ID: 3696312 • Letter: M
Question
My code compiles but it doesn't run properly. My switch statement is not calling the functions. Please help me, fix my code. Any help is greatly appreciated.
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;
#undef NULL
const int NULL = 0;
typedef char element;
#define SENTINEL '#'
element read_element();
int read_int();
void display_menu();
class listnode {
public:
element data;
listnode *next;
};
class LList {
private:
listnode *head;
listnode *tail;
public:
void Run_Read();
void Print();
void Read();
void Run_Print();
void InsertTail(element val);
void Clean();
element DeleteHead();
LList();
~LList();
void Addition();
void Multiply();
void Reverse();
void ReverseUtil(listnode *curr, listnode *prev, listnode **head);
void InsertHead(element val);
int AsciiConverter(listnode* node);
void Steal(LList& Result);
void addM(LList& a, LList &B)/>/>;
void Duplicate(LList& Source);
};
int main(){
LList L;
char menu_option;
do{
L.Run_Print();
display_menu();
menu_option = read_element();
switch(menu_option){
case 'e':
case 'E':
L.Run_Read();
break;
case 'a':
case 'A':
cout << "adding" << endl;
L.Addition();
cout << "Adding complete." << endl;
break;
case 'm':
case 'M':
L.Multiply();
break;
case 'h':
case 'H':
display_menu();
break;
case 'q':
case 'Q':
cout << "Quitting the program." << endl;
break;
default:
cout << "Not a valid input." << endl;
break;
}
}while(menu_option != 'q');
}
void LList::Print(){
//Pre: The N.O. LList is valid.
//Post: The N.O. LList is valid and is made up of elements
//provided by the user.
Clean();
element userval;
cout << "Enter elements," << SENTINEL << " to stop: ";
userval = read_element();
while(userval != SENTINEL){
InsertTail(userval);
userval = read_element();
}
}
char read_element(){
char user_input;
cin >> user_input;
while (!cin.good()){
cout << "Try again: ";
cin.clear();
cin.ignore(80,' ');
cin >> user_input;
}
}
int read_int() {
//variable dec+def
int user_input; //input - user input
//type checking
cin >> user_input;
while (!cin.good()){
cout << "Response must be a whole number, try again: ";
cin.clear();
cin.ignore(80,' ');
cin >> user_input;
}
}
int read_int() {
//variable dec+def
int user_input; //input - user input
//type checking
cin >> user_input;
while (!cin.good()){
cout << "Response must be a whole number, try again: ";
cin.clear();
cin.ignore(80, ' ');
cin >> user_input;
}
return user_input;
}
void display_menu(){
cout << "Options:" << endl
<< "e enter the current hexadecimal number"
<< "from the keyboard." << endl
<< "a add a new hexadecimal number to"
<< "the current hexadecimal number" << endl
<< "m multiply a new hexadecimal number by the"
<< "current hexadecimal number" << endl
<< "h help the user by displaying these choices" << endl
<< "q quit the program" << endl
<< "Enter a command: ";
}
void LList::InsertTail(element val) {
//Pre: The N.O. LList is valid.
//Post: The N.O. LList is unchanged, except it now has a
//new listnode at its tail end containing val.
listnode * temp;
temp = new listnode;
temp -> data = val;
temp -> next = NULL;
if (head == NULL)
head = temp;
else
tail -> next = temp;
tail = temp;
}
void LList::Clean(){
//Pre: The N.O. LList is valid.
//Post: The N.O. LList is valid and empty and the memory
//for its listnodes have been given back to the system
//memory pool.
while(head != NULL)
DeleteHead();
}
element LList::DeleteHead() {
listnode * temp;
element val;
temp = head;
head = head -> next;
val = temp -> data;
delete temp;
return temp -> data;
}
LList::LList() {
//Pre: none
//Post: The N.O. LList is valid and empty.
head = NULL;
}
LList::~LList() {
//Pre: The N.O. LList is valid.
//Post: The N.O. LList is valid and empty and all of the
//memory of its listnodes have been given back to the
//system memory pool.
Clean();
}
void LList::InsertHead(element val) {
listnode * temp;
temp = new listnode;
temp -> data = val;
temp -> next = head;
if(head == NULL)
tail = temp;
else;
head = temp;
}
void LList::Addition(){
LList Add;//Local Object for elements to be added to Native Object
LList Result;//Local Object for sum of Add elements and NO elements
int num1;//Decimal value of Native Object elements
int num2;//Decimal value of Add elements
int carry;//To be added to next set of values
int sum;//Result of addition
element val;//Hexadecimal value of sum
listnode * temp1;
listnode * temp2;
cout << "Enter hex to add: ";
Add.Reverse();
temp1 = head;
temp2 = Add.head;
carry = 0;
while (temp1 != NULL && temp2 != NULL) {
if(carry != 0)
Result.InsertTail(carry + 48);
else
;
num1 = AsciiConverter(temp1);//Returns decimal value of hexadecimal
num1 += carry;
num2 = AsciiConverter(temp2); //Returns decimal value of hexadecimal
sum = (num1 + num2) % 16; //Base-16 addition
carry = (num1 + num2) / 16;
//Convert back to ascii for hexadecimal character
if (sum <= 9)
val = sum + 48;
else
val = sum + 55;
//Putting the hexadecimal characters on local link list
Result.InsertTail(val);
//Move to next element on link lists
temp1 = temp1->next;
temp2 = temp2->next;
}
//Attach native object link list head and tail to local link list
Steal(Result);
}
int LList::AsciiConverter(listnode* listnode){
int num;
if (listnode != NULL)
num = listnode->data;
else
num = 0;
if (num >= 48 && num <= 57)
num -= 48;
else if (num >= 65 && num <= 70)
num -= 55;
else
;
return num;
}
void LList::Reverse(){
//Pre:
//Post:
LList Result;
while(head != NULL){
Result.InsertHead(DeleteHead());
Steal(Result);
}
}
void LList::Run_Print(){
cout << "Current hexadecimal number is: " << endl;
Print();
cout << "Command(h for help): " << endl;
}
void LList::Steal(LList& Result){
Clean();
head = Result.head;
tail = Result.tail;
Result.head = NULL;
}
int getSize(class listnode *listnode)
{
int size = 0;
while (listnode != NULL)
{
listnode = listnode->next;
size++;
}
return size;
}
void LList::Multiply(){
LList Mult;
LList e;
LList f;
Mult.Reverse();
listnode* temp1;
listnode* temp2;
int num1;
int num2;
int product;
int carry = 0;
double sum = 0;
double extra;
float n = 0.0;
float i = 0.0;
temp1 = head;
temp2 = Mult.head;
num1 = AsciiConverter(temp1);//Returns decimal value of hexadecimal
num1 += carry;
num2 = AsciiConverter(temp2); //Returns decimal value of hexadecimal
product = (num1 * num2) % 16; //Base-16 addition
carry = (num1 * num2) / 16;
while (temp2 != NULL){
product = temp1 ->data;
product = product * temp2 -> data;
product = product + carry;
if (product >15){
carry = product/16;
product = product % 16;
}
else{
carry = 0;
product = product%16;
}
n = n + 1.0;
temp1 = temp1 -> next;
e.InsertTail(product);
if (temp1 == NULL){
addM(f,e);
n = 0;
i = i + 1.0;
temp1 = head;
temp2 = temp2-> next;
}
}
}
void LList::addM(LList &a, LList &B)/>/>{
LList copy;
listnode* temp1 = a.head;
listnode* temp2 = b.head;
int sum = 0;
int carry = 0;
while(temp1 || temp2){
if(temp1)
sum = sum + temp1->data;
else;
if(temp2)
sum = sum + temp2->data;
else;
sum = sum + carry;
carry = 0;
while (sum > 19 ){
sum = sum - 20;
carry++;
}
copy.InsertTail(sum);
sum = 0;
if (temp1)
temp1 = temp1->next;
if (temp2)
temp2 = temp2->next;
}
a.Clean();
a.Duplicate(copy);
b.Clean();
}
void LList::Duplicate(LList& Source){
listnode *temp;
Clean();
temp = Source.head;
while(temp != NULL){
InsertTail(temp -> data);
temp = temp -> next;
}
}
void LList::Run_Read(){
cout << "current list is: " << endl;
Read();
}
Explanation / Answer
// kindly check in your program whether you have declared Read() function or not.
//and little modifications i have done in your program for rectify errors i have change LList to Llist. only
// and if your program has that error that is not declared Read() function.
My code compiles but it doesn't run properly. My switch statement is not calling the functions. Please help me, fix my code. Any help is greatly appreciated.
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;
#undef NULL
const int NULL = 0;
typedef char element;
#define SENTINEL '#'
element read_element();
int read_int();
void display_menu();
class listnode {
public:
element data;
listnode *next;
};
class LList {
private:
listnode *head;
listnode *tail;
public:
void Run_Read();
void Print();
void Read();
void Run_Print();
void InsertTail(element val);
void Clean();
element DeleteHead();
LList();
~LList();
void Addition();
void Multiply();
void Reverse();
void ReverseUtil(listnode *curr, listnode *prev, listnode **head);
void InsertHead(element val);
int AsciiConverter(listnode* node);
void Steal(LList& Result);
void addM(LList& a, LList &B)/>/>;
void Duplicate(LList& Source);
};
int main(){
LList L;
char menu_option;
do{
L.Run_Print();
display_menu();
menu_option = read_element();
switch(menu_option){
case 'e':
case 'E':
L.Run_Read();
break;
case 'a':
case 'A':
cout << "adding" << endl;
L.Addition();
cout << "Adding complete." << endl;
break;
case 'm':
case 'M':
L.Multiply();
break;
case 'h':
case 'H':
display_menu();
break;
case 'q':
case 'Q':
cout << "Quitting the program." << endl;
break;
default:
cout << "Not a valid input." << endl;
break;
}
}while(menu_option != 'q');
}
void LList::Print(){
//Pre: The N.O. LList is valid.
//Post: The N.O. LList is valid and is made up of elements
//provided by the user.
Clean();
element userval;
cout << "Enter elements," << SENTINEL << " to stop: ";
userval = read_element();
while(userval != SENTINEL){
InsertTail(userval);
userval = read_element();
}
}
char read_element(){
char user_input;
cin >> user_input;
while (!cin.good()){
cout << "Try again: ";
cin.clear();
cin.ignore(80,' ');
cin >> user_input;
}
}
int read_int() {
//variable dec+def
int user_input; //input - user input
//type checking
cin >> user_input;
while (!cin.good()){
cout << "Response must be a whole number, try again: ";
cin.clear();
cin.ignore(80,' ');
cin >> user_input;
}
}
int read_int() {
//variable dec+def
int user_input; //input - user input
//type checking
cin >> user_input;
while (!cin.good()){
cout << "Response must be a whole number, try again: ";
cin.clear();
cin.ignore(80, ' ');
cin >> user_input;
}
return user_input;
}
void display_menu(){
cout << "Options:" << endl
<< "e enter the current hexadecimal number"
<< "from the keyboard." << endl
<< "a add a new hexadecimal number to"
<< "the current hexadecimal number" << endl
<< "m multiply a new hexadecimal number by the"
<< "current hexadecimal number" << endl
<< "h help the user by displaying these choices" << endl
<< "q quit the program" << endl
<< "Enter a command: ";
}
void LList::InsertTail(element val) {
//Pre: The N.O. LList is valid.
//Post: The N.O. LList is unchanged, except it now has a
//new listnode at its tail end containing val.
listnode * temp;
temp = new listnode;
temp -> data = val;
temp -> next = NULL;
if (head == NULL)
head = temp;
else
tail -> next = temp;
tail = temp;
}
void LList::Clean(){
//Pre: The N.O. LList is valid.
//Post: The N.O. LList is valid and empty and the memory
//for its listnodes have been given back to the system
//memory pool.
while(head != NULL)
DeleteHead();
}
element LList::DeleteHead() {
listnode * temp;
element val;
temp = head;
head = head -> next;
val = temp -> data;
delete temp;
return temp -> data;
}
LList::LList() {
//Pre: none
//Post: The N.O. LList is valid and empty.
head = NULL;
}
LList::~LList() {
//Pre: The N.O. LList is valid.
//Post: The N.O. LList is valid and empty and all of the
//memory of its listnodes have been given back to the
//system memory pool.
Clean();
}
void LList::InsertHead(element val) {
listnode * temp;
temp = new listnode;
temp -> data = val;
temp -> next = head;
if(head == NULL)
tail = temp;
else;
head = temp;
}
void LList::Addition(){
LList Add;//Local Object for elements to be added to Native Object
LList Result;//Local Object for sum of Add elements and NO elements
int num1;//Decimal value of Native Object elements
int num2;//Decimal value of Add elements
int carry;//To be added to next set of values
int sum;//Result of addition
element val;//Hexadecimal value of sum
listnode * temp1;
listnode * temp2;
cout << "Enter hex to add: ";
Add.Reverse();
temp1 = head;
temp2 = Add.head;
carry = 0;
while (temp1 != NULL && temp2 != NULL) {
if(carry != 0)
Result.InsertTail(carry + 48);
else
;
num1 = AsciiConverter(temp1);//Returns decimal value of hexadecimal
num1 += carry;
num2 = AsciiConverter(temp2); //Returns decimal value of hexadecimal
sum = (num1 + num2) % 16; //Base-16 addition
carry = (num1 + num2) / 16;
//Convert back to ascii for hexadecimal character
if (sum <= 9)
val = sum + 48;
else
val = sum + 55;
//Putting the hexadecimal characters on local link list
Result.InsertTail(val);
//Move to next element on link lists
temp1 = temp1->next;
temp2 = temp2->next;
}
//Attach native object link list head and tail to local link list
Steal(Result);
}
int LList::AsciiConverter(listnode* listnode){
int num;
if (listnode != NULL)
num = listnode->data;
else
num = 0;
if (num >= 48 && num <= 57)
num -= 48;
else if (num >= 65 && num <= 70)
num -= 55;
else
;
return num;
}
void LList::Reverse(){
//Pre:
//Post:
LList Result;
while(head != NULL){
Result.InsertHead(DeleteHead());
Steal(Result);
}
}
void LList::Run_Print(){
cout << "Current hexadecimal number is: " << endl;
Print();
cout << "Command(h for help): " << endl;
}
void LList::Steal(LList& Result){
Clean();
head = Result.head;
tail = Result.tail;
Result.head = NULL;
}
int getSize(class listnode *listnode)
{
int size = 0;
while (listnode != NULL)
{
listnode = listnode->next;
size++;
}
return size;
}
void LList::Multiply(){
LList Mult;
LList e;
LList f;
Mult.Reverse();
listnode* temp1;
listnode* temp2;
int num1;
int num2;
int product;
int carry = 0;
double sum = 0;
double extra;
float n = 0.0;
float i = 0.0;
temp1 = head;
temp2 = Mult.head;
num1 = AsciiConverter(temp1);//Returns decimal value of hexadecimal
num1 += carry;
num2 = AsciiConverter(temp2); //Returns decimal value of hexadecimal
product = (num1 * num2) % 16; //Base-16 addition
carry = (num1 * num2) / 16;
while (temp2 != NULL){
product = temp1 ->data;
product = product * temp2 -> data;
product = product + carry;
if (product >15){
carry = product/16;
product = product % 16;
}
else{
carry = 0;
product = product%16;
}
n = n + 1.0;
temp1 = temp1 -> next;
e.InsertTail(product);
if (temp1 == NULL){
addM(f,e);
n = 0;
i = i + 1.0;
temp1 = head;
temp2 = temp2-> next;
}
}
}
void LList::addM(LList &a, LList &B)/>/>{
LList copy;
listnode* temp1 = a.head;
listnode* temp2 = b.head;
int sum = 0;
int carry = 0;
while(temp1 || temp2){
if(temp1)
sum = sum + temp1->data;
else;
if(temp2)
sum = sum + temp2->data;
else;
sum = sum + carry;
carry = 0;
while (sum > 19 ){
sum = sum - 20;
carry++;
}
copy.InsertTail(sum);
sum = 0;
if (temp1)
temp1 = temp1->next;
if (temp2)
temp2 = temp2->next;
}
a.Clean();
a.Duplicate(copy);
b.Clean();
}
void LList::Duplicate(LList& Source){
listnode *temp;
Clean();
temp = Source.head;
while(temp != NULL){
InsertTail(temp -> data);
temp = temp -> next;
}
}
void LList::Run_Read(){
cout << "current list is: " << endl;
Read();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.