Pointers with linked list start with value '0'

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <iostream>
using namespace std;

#undef NULL		//undefines anything from a class that already has
			//the same name


typedef int element;
const int NULL=0;
const char 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();
	else if ((answer == 97) || (answer == 65 ))
		AddVigesimal();
	else if ((answer == 109) || (answer == 77 ))
		MultiplyVigesimal();
	else if ((answer == 104) || (answer == 72))
		HelpMenu();
	else if ((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
1
2
3
4
5
6
void LList::Clean(){
	if(head != NULL){
		delete head;
		head=NULL;
		}
	}


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.
I'd say that Print() should work when the next member of listnode is always set properly.

You just can't write Print() into the stream like on line 126. Call it separately.

Currently Print() shows nothing because you don't add any elements to the list. AddVigesimal() does nothing
Topic archived. No new replies allowed.