0

Now with the above functions it would be pretty simple to plug in the code for your restaurant program

struct restaurant
{
    restaurant() {number = 0;}
    restaurant(string nm, int n) {name = nm; number = n;}
    void SayHello() {cout << "Hello, I'm " << name << "\n"; }
    std::string GetName() {return name;}

    int number;
    std::string name;
    struct node* registers;

};

int main()
{
    struct node* head = NULL;
    int nRestaurants = 0;
    cout << "Enter the number of restaurants that you want\n";
    cin >> nRestaurants;
    cin.ignore();
    for(int i = 0; i < nRestaurants; i++)
    {
        stringstream str;
        str << "Wendy's #";
        str << i+1;
        string name = str.str();
        struct restaurant* item = new struct restaurant(name, i+1);
        InsertTail(&head, item);

    }

    struct node* n = head;
    while( n != NULL)
    {
        cout << ((struct restaurant *)n->data)->GetName() << "\n";
        n = n->next;
    }
    int number = 0;
    cout << "Which node to you want (1-5) ?\n";
    cin >> number;
    n = GetItem(head, number-1);
    ((struct restaurant *)n->data)->SayHello();

    DeleteList(&head);
}
0

Thanks for that AD.
But, question... Does that mean my way is truly impossible?

0

Thanks for that AD.
But, question... Does that mean my way is truly impossible?

No, its possible, just that its cumbersome because you will wind up duplicating the same code over and over and over for each type of linked list. Programmers try not to do that.

0

Yeah, I get that. But since I'm not duplicating yet, I don't want to worry about that problem yet. Since i'm having problem with just one, lol.

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

using namespace std;
  
                                                     
        struct Node 
        { 
           Node * next;
	       int value;
	      
                          };
		   				   

     int main()
   {    

    int data = 0;

    cout<<"How Many?"<<endl;
    cin>>data;

    Node * head = NULL;
    Node * tail = NULL;
     
    int counter =1;
    
    while (counter <= data)
    {
     Node * nod = new Node;
     nod->value = counter;
     nod->next = NULL;
     
     if(counter==1)
     {
       head= nod;
       tail= head;
                      }
     else
     {
       tail->next = nod;
       tail= tail->next;
         
                            }
                   counter++;     
                                  }
    
    //display contents of the lists
    Node *ptr = head;
    while(ptr!=NULL)
    {
        cout<<ptr->value<<endl;
        ptr= ptr->next;
                                  }
    
    int data2 = 0;
    
    cout<<"which one?"<<endl;
    cin>>data2;
    
    Node *cur = head;
    
  //for(Node * cur = head; cur!= NULL; cur= cur->next)
   //
        for(int i =0; i<data2;i++)
         {
                if(data2 == cur->value)
          {
           cout<<"this is what you chosen"<<cur->value<<endl;
           
                                 }
           else
           {
               cout<<" not in the list"<<endl;
                                 }
                                       }
                                     //}
                                              
 system("PAUSE");
    return 0;     
         }

this is what happens at run time.

how many?
5
1
2
3
4
5
which one?
3
not in the list
not in the list
not in the list


is it because the pointer isn't pointing to the list?

0

a little bit of changed

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

using namespace std;
  
                                                     
        struct Node 
        { 
           Node * next;
	       int value;
	      
                          };
		   				   

     int main()
   {    

    int data = 0;

    cout<<"How Many?"<<endl;
    cin>>data;

    Node * head = NULL;
    Node * tail = NULL;
     
    int counter =1;
    
    while (counter <= data)
    {
     Node * nod = new Node;
     nod->value = counter;
     nod->next = NULL;
     
     if(counter==1)
     {
       head= nod;
       tail= head;
                      }
     else
     {
       tail->next = nod;
       tail= tail->next;
         
                            }
                   counter++;     
                                  }
    
    //display contents of the lists
    Node *ptr = head; 
    while(ptr!=NULL)
    {
        cout<<ptr->value<<endl;
        ptr= ptr->next;
                                  }
    
    int data2 = 0;
    
    cout<<"which one?"<<endl;
    cin>>data2;
    
    Node *cur = ptr;
    
  //for(Node * cur = head; cur!= NULL; cur= cur->next)
   //
        for(int i =0; i<data2;i++)
         {
                if(data2 == cur->value)
          {
           cout<<"this is what you chosen"<<cur->value<<endl;
           
                                 }
           else
           {
               cout<<" not in the list"<<endl;
                                 }
                                       }
                                     //}
                                              
            system("PAUSE");
       return 0;     
               }//end of main

i changed the cur = ptr
instead of cur = head.

run time I get

how many
4
1
2
3
4
which one?

then it crashes.

0
while(ptr!=NULL)
 {
        cout<<ptr->value<<endl;
        ptr= ptr->next;
}
//ptr now equals NULL

int data2 = 0;
cout<<"which one?"<<endl;
cin>>data2;
    
Node *cur = ptr;
//cur now equals NULL
    
for(int i = 0; i < data2; i++)
{
     if(data2 == cur->value)
//how can you try to dereference a pointer whose value equals NULL

>>is it because the pointer isn't pointing to the list?

No.

for(int i =0; i<data2;i++)
{
    if(data2 == cur->value) //cur->value always equals 1.  you don't advance cur as you do the loops.  Therefore this will never be true
    {
        cout<<"this is what you chosen"<<cur->value<<endl;
     }
     else
     {
          cout<<" not in the list"<<endl;
      }
}
0
for(int i =0; i<data2;i++)
{
cur= cur->next;  // just added
    if(data2 == cur->value) //cur->value always equals 1.  you don't advance cur as you do the loops.  Therefore this will never be true
    {
        cout<<"this is what you chosen"<<cur->value<<endl;
     }
     else
     {
          cout<<" not in the list"<<endl;
      }
}

what if I put the cur=cur->next in the loop so that cur moves.
though, when I did this my compiler on run time, it crashed.

0

Try something like this:

Node *cur = head;

for(int i = 0; i < data2; i++)
{
     if(data2 == cur->value)
     {
        cout<< cur->value << " is in one of the first " << data2 << " nodes of the list" << endl;
      }
      else
      {
          cout << cur->value << " is not equal to " << data2 << endl;
      }
      cur = cur->next;
}
0
#include <iostream>
#include <cstdlib>

using namespace std;

struct Node{
       int value;
       Node *next;
       
       };
       
       
       bool menu(const char& m)
       {
            return m == 'x'|| m == 'z';
            
            
            }
       int main()
       
       {
           int data = 0;
           cout<<"How many"<<endl;
           cin>>data;
           
           Node *head = NULL;
           Node *tail = NULL;
           
           int counter = 1;
           
           while(counter <= data)
           {
              Node * list = new  Node;
              list -> value = counter;
              list->next = NULL;
              
              if(counter == 1)
              {
                head = list;
                tail = head;         
                         
                         }   
               else
               {
                   tail -> next = list;
                   tail = tail->next;
                   
                         }      
                         counter++;
                         
                         }
                         
                           //display contents of the lists
    Node *ptr = head; 
    while(ptr!=NULL)
    {
        cout<<ptr->value<<endl;
        ptr= ptr->next;
                                  }
           
           char n;
           cout<<"choose from menu: "<<endl;
           cin>>n;
           
           if(menu(n))
           {
            int data2=0;
            cout<<"which one do you want?"<<endl;
            cin>>data2;
            
            Node *cur = head;
            
            for(int i=0; i<data2; i++)
            {
                  if(data2==cur->value)
                  {
                      cout<<"Your choice is:   "<<cur->value<<endl;
                                       
                                       }  
            else
            {
                cout<<"try agian"<<endl;
                
                                       }
                    
                         }
           
           }
           system("PAUSE");
           return 0;
           }

this code, run time error.

how many
4
1
2
3
4
choose from menu:
x
which one do you want?
3
try again
try again
try again
press any key to continue . . .

// but if you type in one
how many
4
1
2
3
4
choose from menu:
x
which one do you want?
1
your choice is: 1
press any key to continue. .

so it works for one, but not other numbers?

0
#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;
  
                                                     
        struct Node 
        { 
           Node * next;
	       int value;
	      
                          };
		   				   

     int main()
   {    

    int data = 0;

    cout<<"How Many?"<<endl;
    cin>>data;

    Node * head = NULL;
    Node * tail = NULL;
     
    int counter =1;
    
    while (counter <= data)
    {
     Node * nod = new Node;
     nod->value = counter;
     nod->next = NULL;
     
     if(counter==1)
     {
       head= nod;
       tail= head;
                      }
     else
     {
       tail->next = nod;
       tail= tail->next;
         
                            }
                   counter++;     
                                  }
    
    //display contents of the lists
    Node *ptr = head; 
    while(ptr!=NULL)
    {
        cout<<ptr->value<<endl;
        ptr= ptr->next;
                                  }
    
    int data2 = 0;
    
    cout<<"which one?"<<endl;
    cin>>data2;
    
    Node *cur = head;
    
  //for(Node * cur = head; cur!= NULL; cur= cur->next)
   //
        for(int i =0; i<data2;i++)
         {
                if(data2 == cur->value)
          {
           cout<<"this is what you chosen"<<cur->value<<endl;
           
                                 }
           else
           {
               cout<<" not in the list"<<endl;
                                 }
           cur= cur->next;
                                       }
                                     //}
                                              
            system("PAUSE");
       return 0;     
               }//end of main

but for this code, it works .
Weird.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.