0

I'm sticking with Link list tho @_@

It's your choice, but remember that the list container from STL is also a linked list (a doubly linked list)

0

Since I'm sticking with linked lists.
How do I correct the memory leak problem.

0

Since I'm sticking with linked lists.
How do I correct the memory leak problem.

Deleting that pointer would do the trick.

//Example Code
int * temp=new int;
delete temp;
0
char action;
cout<<"how many?";
cin>>action;

if(sub_choice(action))
{int count = 1;
      while (action != 'x')
   {
        cin>>action;
       switch(action)
     {
       case 'c':
    {
     Customer *CUST = new Customer;
       CUST->lineNumber = count;
        count++;
        CUST = CUST-> next;
       delete CUST;   // you mean this pointer?
   }
break;

default:
cout<<"hello";
                    }
}
0

This would stop the memory leak , But would not make cust accessible.I mean the new customer profile will get deleted.

You should find a way of handling new pointers.

For this,Just imagine how a linked list works and how should you define it ;)

0

Another thing.
I find myself pressing C twice to get something.
Like when I'm couting C.
After the first C, I should see a 1.
But i have to type in C twice, to see C 1.

And can you word what you said in another way?

0
char action;
cout<<"how many?";
cin>>action;

if(sub_choice(action))
{int count = 1;
      while (action != 'x')
   {
        cin>>action;
       switch(action)
     {
       case 'c':
    {
     Customer *CUST = new Customer;
       CUST->lineNumber = count;
        count++;
        CUST = CUST-> next;
       delete CUST;   // you mean this pointer?
   }
break;

default:
cout<<"hello";
                    }
}

Thats probarbly the reason you are getting 1. after 2 times because you are taking in data twice. Removing the red part would solve it.

Secondly.

Customer *CUST ;

I think Customer must be defined outside the while loop.
And then Figure out the way to handle the linked list.
If you think that the linked List is getting a little complex. there is always a vector :)

0

That needs to be there because without it, I can't enter enter the while loop @_@

0

it removes the customer from the line.
But that problem isn't really now.

0

I am really unable to analyse this . Firstly How can you remove a customer without creating the customer.

I think you should add in a case such as 'r' in the while loop to remove a customer.

And how do you plan on removing a customer from the line?

Make sure that all the functions such as remove customer add customer talk to customer comes within the while loop and be sure On how you will be handling customers with the link-list

0

Hmm, I realize something today as I try to debug my program.
After I hit c, then i hit another key, and keep hitting that key.
It enters into a indefnite loop.

0

Still not sure how to make it so that after you hit C once, you get a 1. Instead of hitting it twice.

0

^
Huh? xD

Say you hit C
still a blinking cursor
You hit C again,
that's when a 1 pops out.

I'm asking for just
C
1

instead of
C
C
1

And I'm not sure why it does this.
C
A
C
breaks the program and starts a infinite loop.

0
#include <iostream>
#include <cstdlib>

using namespace std;

struct Node{
       Node *next;
       int value;
       
       };
       
       
       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++;
                         
                         }
           
           char n;
           cout<<"choose from menu: "<<endl;
           cin>>n;
           
           if(menu(n))
           {
            int data2=0;
            cout<<"which one do you watn?"<<endl;
            cin>>data2;
            
            while(n != 't')
            {
                    cin>>n;
                    switch (n)
                    {
                           
                    case 'x':
                    data2++;
                    cout<<data2;
                    
                    break;
                    default:
                            cout<<"hey"<<endl;
                            }
                           }
            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;
                
                                       }
                    cur=cur->next;
                         }
           
           }
           system("PAUSE");
           return 0;
           }

type x twice, and it'll go into an indefinite loop.

0

I'm wondering something.

can input be something like

CCCCCCCC - this creates the amount of C's customers.

or should it be
C
C
C
C
C
C

etc

0

I'm wondering something.

can input be something like

CCCCCCCC - this creates the amount of C's customers.

or should it be
C
C
C
C
C
C

etc

With the above code. You will be able to enter CCCCCCC and create many C's

After you enter 'C' you should press ENTER or RETURN for the command to execute. I never seen an alternative to that. So i really cant help out with enter 'C' and execute Command.

0

Did you read my remark on system("pause"); in my signature ?
BTW, is there any specific reason to write this:

Node * head = NULL;
Node * tail = NULL;

?

Remark(s):

  • If you want let new return a NULL-pointer when allocation has failed, then you should use new(nothrow) :)
  • You don't check whether the memory allocation has failed or not, this is a bad programming practice :)
    I can't seem to find the code which checks for the 'C' input :( ...
0

when I type in CCCCCCCCCCC it says invaid input, which was what my else statement says if it wasn't C.

And does anyone know why the while loop breaks into indefnite loop?

0

for which response is that to?
first part or the second part of my last post?

0
char action;
cout<<"how many?";
cin>>action;

Customer *cust = new Customer;
Customer * head = NULL;
Customer * tail = NULL;
head = Customer;
tail = head;

if(sub_choice(action))
{int count = 1;
      while (action == 'c')
   {

       switch(action)
     {
       case 'c':
    {

      tail -> next = new Customer;
      tail = tail->next;
       CUST->lineNumber = count;
        count++;

   }
break;

default:
cout<<"hello";
                    }
}

is this better?

0

The problem with your code is that . Once you enter 'C' It goes into an infinite loop making and deleting pointers.

0

So how would I change it into doing what it's properly doing?
I keep going over it, and the logic seems to be right.

0

i GUESS YOU WILL NEED TO cin>>action; inside the while loop.

If you want the loop to run for ever each time taking in C.
U can do this

while(true)
{
cin>>action;
switch(action)
{
case 'c':
//DO something.
}
}
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.