putting a character string into a linked list

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!

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
#include <iostream>
#include <iomanip>
#include <cctype>

using namespace std;

typedef int element;
const char 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 ();
        else if (num == 72 || num == 104)
                ShowHelp ();
        else if (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 ();
}
Topic archived. No new replies allowed.