Hi guys,
I'm trying to get my program to accept a string of characters as input and store each character as an item on a linked list (which i'm very new to). The best way to go about doing this, as far as I can tell, is to store the character string into a character array, then go through the array, storing each element onto its own node. Compiled fine, but then I got a segmentation fault. My specific problem is in the ReadBackward function at the very bottom of my code. Open to any other suggestions as to how to make anything better. Thanks people!
#include <iostream>
#include <iomanip>
#include <cctype>
usingnamespace std;
typedefint element;
constchar SENTINEL = '#';
class listnode {
public:
element data;
listnode *next;
};
class LList {
private:
listnode *head;
listnode *tail;
public:
LList ();
~LList ();
void Print ();
void Clean ();
void ReadForward ();
void ReadBackward ();
void ShowHelp ();
void Action ();
};
int main (){
LList A;
A.Action ();
}
void LList :: Action (){
//PRE: The N.O. LList is valid
//POST: The N.O. LList has been displayed, and the user has selected
//which action to make on the current number.
char command;
int num;
cout << "The current vigesimal number is: " << endl;
Print ();
cout << "Command (h for help): " << endl;
cin >> command;
num = command;
if (num == 69 || num == 101)
ReadBackward ();
elseif (num == 72 || num == 104)
ShowHelp ();
elseif (num == 81 || num == 113)
cout << "\nFinishing Vigesimal Calculator, v1.0\n";
else{
cout << "Invalid command. Enter a command from the ";
cout << "menu (h for help): " << endl;
}
}
void LList :: ShowHelp (){
//PRE: The user has asked for the help menu to be displayed
//POST: The help menu has been displayed
cout << "\nValid commands are: " << endl;
cout << setw(2) << "e " << left << setw(10) << "enter" << "enter the";
cout << " current vigesimal number from the keyboard" << endl;
cout << setw(3) << "a " << left << setw(10) << "add" << "add a new ";
cout << "vigesimal number to the current vig. number" << endl;
cout << setw(3) << "m " << left << setw(10) << "multiply";
cout << "multiply a new vigesimal number by the current vig. number.";
cout << endl << setw(3) << "h " << left << setw(10) << "help";
cout << "show this help menu" << endl << setw(3) << "q " << left;
cout << setw(10) << "quit" << "quit the program" << endl;
Action ();
}
void LList :: Print (){
//PRE: The N.O. LList is valid.
//POST: The N.O. LList is unchanged and its elements have been
//displayed.
listnode *temp;
temp = head;
if (temp != NULL){
cout << temp -> data << endl;
temp = temp -> next;
}
else
cout << "0\n";
}
void LList :: ReadBackward (){
//PRE: The N.O. is valid.
// POST: The N.O. LList will be valid using data provided by the user
//and will be sorted in a backwards order.
element userval = 100;
listnode *temp;
char vig [userval];
Clean ();
cout << "Enter vigesimal digit, followed by " << SENTINEL << ".\n";
cin.getline(vig, userval);
int i = 0;
while (vig [i] != SENTINEL){
temp = new listnode;
temp -> data = vig [i];
temp -> next = head;
if (head == NULL)
tail = temp;
else
;
head = temp;
i++;
}
Action ();
}