C++ Linked list issues with strings -
i'm quite new c++ , trying insert phone number , name inside of linked list user can set.i'm wondering i'm going wrong, i've been watching video after video using integers in linked lists , not strings.
so if main taken out program shows (0)errors , (0)warnings think problem in main.
so in main i'm trying cin nam(which have inside function part of set_first). i've tried passing address think i'm pulling @ straws that.
so question is: can point me in right direction, or let me know how put name , phone number node user specifies?
here's programs far descending main:
#include <iostream> #include <cstdlib> #include "l_list_1.h" using namespace std; int main() { l_list_1 obr; std::string nam; cout << "name"; cin >> nam; obr.set_first(nam); //i'm trying give nam since that's have inside set_first function. return 0; }
my header file looks this:
#ifndef l_list_1_h #define l_list_1_h class l_list_1 { public: l_list_1(); void set_first(std::string nam, int phnum); void delete_node(std::string del_nams, int num_del); void display(); char menu(); private: typedef struct node { std::string user_name; int user_phone; node* next; }* nodeptr; nodeptr head; nodeptr curr; nodeptr tail; nodeptr temp; }; #endif // l_list_1_h
and cpp file here:
#include <iostream> #include <cstdlib> #include <string> #include "l_list_1.h" using namespace std; l_list_1::l_list_1() { head = null; curr = null; tail = null; } void l_list_1::set_first(std::string nam, int phnum) { nodeptr n = new node; n->next = null; n->user_name = nam; n->user_phone = phnum; cout << "name"; cin >> nam; if (head != null) { curr = head; while (curr->next != null) { curr = curr->next; } curr->next = n; } else { head = n; } } void l_list_1::delete_node(std::string del_nams, int num_del){ nodeptr delptr = null; temp = head; curr = head; while(curr != null && curr->user_name != del_nams && curr->user_phone != num_del){ temp = curr; curr = curr->next; } if(curr == null){ cout << del_nams << "was not in list"; cout << num_del << "was not in list"; delete delptr; } else{ delptr = curr; curr = curr-> next; temp->next = curr; delete delptr; cout << "the item" << del_nams << "was deleted"; cout << "the item" << num_del << "was deleted"; } } void l_list_1::display(){ curr = head; while(curr != null){ cout << curr->user_name; cout << curr->user_phone << endl; curr = curr->next; } }
for starters member function set_first
declared 2 parameters
void set_first(std::string nam, int phnum);
however call 1 argument
obr.set_first(nam);
in function (and in second function deletes nodes) not set tail of list.
also not clear these statements in function
cout << "name"; cin >> nam;
you should remove them.
and there no sense declare following data members
nodeptr curr; nodeptr temp;
instead of them use local variables in methods.
take account should include header <string>
#include <string>
and not see header <cstdlib>
used in program. may remove header.
Comments
Post a Comment