Singly Linked List

I have the following code. But, the program is terminating without any user input. I want to know how this can be fixed.

#include <iostream>
#include <cctype>
#include <cstdlib>
#include <string>

using namespace std;

class StringNode {
private:
string elem;
double miles;
StringNode* next;

friend class StringLinkedList;
};

class StringLinkedList {
public:
StringLinkedList();
void addBack(const string& e, double &m);
void searchAndPrint();
bool invalidCode(string& c);
private:
StringNode *head;
StringNode *tail;
};

StringLinkedList::StringLinkedList()
{head->elem = "LGA";
head->miles = 0.0;
tail = head;}

void StringLinkedList::addBack (const string& e, double &m) {
StringNode* v = new StringNode;
v->elem = e;
v->miles = m;
tail->next = v;
tail = tail->next;
}

//---------------------------------------------------
//function to print contents of list
void StringLinkedList::searchAndPrint(){
//node to traverse the list
//initialized to head
StringNode *currentNode = head;
string val;
cout << "Enter code to search. ";
cin >> val;

while(invalidCode(val)){
cout << "Please enter 3 character code with capital letters. ";
cin >> val;
}

for (int i = 0; i < val.size(); i++)
val.at(i) = toupper(val.at(i));

while(currentNode->elem!=val && currentNode)
currentNode = currentNode->next;

if(!currentNode)
cout << "\nNo match found.\n";

//print all entries, until
//currentNode points to null
while(currentNode){
//print content of current node
cout << currentNode->elem << " " << currentNode->miles << " ";

//move to next node
currentNode = currentNode->next;
}
}

bool StringLinkedList::invalidCode(string& c)
{
if(c.size()!=3)
return true;
for (int i = 0; i < c.size(); i++) {
if(!isalpha(c.at(i)))
return true;
}
return false;
}


void showMenu();
void readStr(string&, double &);
bool invalidCode(string&);

int main()
{
cout << "AIRPORT CODES AND DISTANCES\n\n";

//create a StringLinkedList object
StringLinkedList str;

//variables to store user choice
//and rainfall
int choice = 0;
string code;
double milesFromLast;
double milesFromLGA = 0.0;

//show the menu in a do-while loop
do{
showMenu();

cout << endl << "Enter your choice: ";
cin >> choice;

//validate choice using while loop
while(choice > 3 || choice < 0){
cout << "Please enter a valid choice: ";
cin >> choice;
}
cout<<endl;

//use switch statement to help
switch(choice){
case 1:
readStr(code, milesFromLast);
milesFromLGA+=milesFromLast;

//insert into list
str.addBack(code, totalMiles);
cout << endl;
break;

case 2:
str.searchAndPrint();
break;

default:
break;
}
cout<<endl;
} while(choice >= 1 && choice <= 3);

//return 0 to mark successful termination
system("pause");
return 0;
}

void showMenu(){
cout << "\n1. Insert new entry";
cout << "\n2. Search code and print";
cout << "\n3. Quit\n";
}

void readStr(string& c, double &m){
cout << "Enter 3 character airport code with capital letters: ";
cin >> c;
while(invalidCode(c)){
cout << "Please enter 3 character code with capital letters. ";
cin >> c;
}

for (int i = 0; i < c.size(); i++)
c.at(i) = toupper(c.at(i));

cout << "Enter miles from the last airport: ";
cin >> m;

//validate input
while(m < 0){
cout << "Please enter nonnegative value: ";
cin >> m;
}
}

bool invalidCode(string& c)
{
if(c.size()!=3)
return true;
for (int i = 0; i < c.size(); i++) {
if(!isalpha(c.at(i)))
return true;
}
return false;
}
add code tags <> on the side editor.
it isn't running because it won't compile.
137:19: error: 'totalMiles' was not declared in this scope
I have the following code

Where did you get it?
Look at your StringLinkedList constructor:
1
2
3
4
StringLinkedList::StringLinkedList()
{head->elem = "LGA";
head->miles = 0.0;
tail = head;}

Choose the indentation style you like better, but assigning to dangling pointers involves undefined behaviour.
Topic archived. No new replies allowed.