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)
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)
Since I'm sticking with linked lists.
How do I correct the memory leak problem.
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;
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";
}
}
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 ;)
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?
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 :)
That needs to be there because without it, I can't enter enter the while loop @_@
What does sub_choice ( d) do???
it removes the customer from the line.
But that problem isn't really now.
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
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.
Still not sure how to make it so that after you hit C once, you get a 1. Instead of hitting it twice.
? If you've hit a key once, you haven't hit it twice :P
^
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.
Can you give me that code ?
#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.
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
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
Cetc
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.
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):
new
return a NULL-pointer
when allocation has failed, then you should use new(nothrow) :)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?
Check out your code and look and what creates a new customer.
for which response is that to?
first part or the second part of my last post?
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?
actually, now everytime i hit enter. it's like I'm pressing c
A switch with one case, it's allowed but I would use an if-statement instead :)
>actually, now everytime i hit enter. it's like I'm pressing c
You could also use cin.get() with 'c' as a delimiter (Though I'm not sure whether this is the best method to do this): http://www.cplusplus.com/reference/iostream/istream/get/
what's the syntax of delim?
The problem with your code is that . Once you enter 'C' It goes into an infinite loop making and deleting pointers.
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.
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.
}
}
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.