I'm writing a vigesmial calculator (base 20 number system) and using liked list and pointers. I am in the early stages
so when the program runs it will always display the current vigesimal number, the hard part is i want it to display "0" when the program first runs
i have a simple cout statement and then using a Print() method to display the current LinkedList (LList) and can't for the life of me think of how to achieve this. i think i have no hair left
i am writing the whole code but the problem lies in the LList:Menu() method. It will not compile due to when the program first starts the Linkedlist is full of random crap (i think?) it works if i substitute the print() method with a zero but i want it to display the current number at all times.(sorry it may sound confusing)
to sum it up what do i need to think about when trying to display the linked list as zero when it first starts then changes via user input.
#include <iostream>
usingnamespace std;
#undef NULL //undefines anything from a class that already has
//the same name
typedefint element;
constint NULL=0;
constchar SENTINEL ='#' ;
class listnode{
public:
element data;
listnode* next;
}; // no private data
class LList{
private:
listnode* head;
listnode* tail;
public:
LList();
~LList();
void ReadBackwards();
void Clean();
void Menu();
char Read_element();
void Vigesimalinput();
void AddVigesimal();
void MultiplyVigesimal();
void HelpMenu();
void Print();
};
int main(){
//main function, everthing will be executed from top to bottom
LList A;
cout<<"\n""Vigesimal Calculator, Version 1.0" <<endl;
cout<<"(c) 2012, Tony Cebak"<<endl;
cout<<endl;
A.listnode* head=0
A.Menu();
}
LList::LList(){
//This is the constructor, LList will live!
//Pre:None
//Post: The N.O is valid
head=NULL;
}
LList::~LList(){
//This is the destructor, it will give LList a clean death
//Pre: The N.O. is valid
//Post: The N.O. is valid and empty and all borrowed system
// resources have been given back
Clean();
}
void LList::Clean(){
//This will basically "proprley" delete objects LList
//Pre: the N.O. LList is valid
//Post: The N.O. is now empty, and all of it's previous listnodes
// have been given to the system memory pool ("heap")
listnode* temp;
while(head != NULL){
temp=head;
head=head -> next;
delete temp;
}
}
void LList::ReadBackwards(){
//This will put the user input into a linked list backwards
//Pre: The N.O LList is valid
//Post: the N.O. LList is now valid, filled with data provided
// by the user in backwards order
char userval;
listnode* temp;
Clean();
cout<<"Enter elements, "<<SENTINEL<<" to stop";
userval=Read_element();
while(userval != SENTINEL){
temp= new listnode;
temp -> data=userval;
temp -> next = head;
if (head == NULL)
tail=temp;
else
;
head=temp;
userval=Read_element();
}
}
char LList::Read_element(){
//Perfors my data type checking validation like a true player
char val;
cin >> val;
while(!cin.good()){
cout<<"Invalid response, should be a number 0-9 or a";
cout<<" letter A-J, try again: ";
cin.clear();
cin.ignore(80, '\n');
cin >>val;
}
return val;
}
void LList::Menu(){
//This displays the vigesimal number that is current. then ask
//for a user input.
char useranswer;
int answer;
answer=0;
cout<<"Current vigesmial number is: "<<Print()<<endl;
cout<<endl;
cout<<"Command (h for help) : ";
useranswer=Read_element();
answer=useranswer;
if ((answer == 101) || (answer == 69))
Vigesimalinput();
elseif ((answer == 97) || (answer == 65 ))
AddVigesimal();
elseif ((answer == 109) || (answer == 77 ))
MultiplyVigesimal();
elseif ((answer == 104) || (answer == 72))
HelpMenu();
elseif ((answer == 113) || (answer == 81))
Menu();
else
cout<<"Invalid menu option (h for help)""\n"<<endl;
Menu();
}
void LList::Vigesimalinput(){
ReadBackwards();
}
void LList::AddVigesimal(){
}
void LList::MultiplyVigesimal(){
}
void LList::HelpMenu(){
//This will display the commands for the user! power to the user!
cout<<"\n"<<"Valid commands are:"<<endl;
cout<<" e enter enter the current vigesimal number"
<<"from the keyboard"<<endl;
cout<<" a add add a new vigesimal number to the"
<<"current vig. number"<<endl;
cout<<" m multiply multiply a new vigesimal number by"
<<"the current vig. number"<<endl;
cout<<" h help show the help menu"<<endl;
cout<<" q quit quit the program""\n"<<endl;
Menu();
}
void LList::Print(){
//Pre; The N.O LList is valid
//Post: the N.O LList is unchaged and its elements have been
// displayed
listnode* temp;
temp=head;
while(temp != NULL){
cout<<temp -> data<< endl;
temp=temp -> next; //pointer increament
}
}
Not that I understand the whole point. But it doesn't look too bad.
Remove line 45. It does not make sense and it does not compile.
You can also remove line 21. You don't use tail and you don't need to.
I'd suggest to make listnode more 'intelligent'.
For instance if it had a constructor it could set 'next' to NULL and if it had a destructor it could delete 'next' and so all subsequent objects. Clean would be
The while loop of Read_element() (which could be a function of listnode) doesn't make sense since the user cannot enter anything but char. Instead you need to verify whether it is the required input with if
Another problem is that you call Menu() recursively. Better not. It's very error prone. One problem is that you might come to a point where you can't find the way out of the menu.
thank you coder777 i like constructor and destructer for listnode
i would delete tail but it's an assignment so it's required.
I'm tweaking Read_element() due to you can input 0-9 and a-j. (should be fun)
but do you know why menu does not work due to its call to print() since nothing is initialized but that's what i thought the listnode constructor would fix.