if I get this down, then I'll complete my assignment.

I want my program to have the ability that whoever is using the program has the ability to control how many customers a restaurant has.

Say Pressing C = new customer.
And depending on how many times they press c, they should have a number associate them with it.

Say he types in CCC, then the first C should have a 1, and the second should have a 2, and the third should have a 3.

Say you have the structs, and etc.
And we're not in the main.

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

if(sub_choice(action))
{
      while (action == 'C')
   {
     Customer *CUST = new Customer;
      int count = 1;
       CUST->lineNumber = count;
        count++;
        CUST = CUST-> next;
                    }
}

but all this does, when compile is output a series of 111111111111111

Recommended Answers

int count = 1; , you should declare it outside your while-loop :)

Jump to Post

Customer *CUST = new Customer; will cause a memory leak also :)

Jump to Post

>would that fix the memory leak?
No
>also, how can I stop the infinite loop
use the break statement :)

By the way: Customer *head = NULL; is a very dangerous instruction, you're assigning a value to an unknown memory address :)

Jump to Post
char action;
cout<<"how many?";
cin>>action;

if(sub_choice(action))
{
      while (action == 'C')
   {
     Customer *CUST = new Customer;
      int count = 1;
       CUST->lineNumber = count;
        count++;
        CUST = CUST-> next;
                    }
}

You get a Infinite Loop because the value of action doesnt change once you give the …

Jump to Post

You also have to keep in mind that there's a difference between the lowercase 'c' and the uppercase 'C' :)

Jump to Post

All 77 Replies

int count = 1; , you should declare it outside your while-loop :)

But I'll still get a infinite loop though, wouldn't I?

Customer *CUST = new Customer; will cause a memory leak also :)

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

if(sub_choice(action))
{
  int count = 1;
  Customer *head = NULL;
      while (action == 'C')
   {
    head = CUST;
     Customer *CUST = new Customer;      
       CUST->lineNumber = count;
        count++;
     CUST = CUST->next;
                    }
}

would that fix the memory leak?
also, how can I stop the infinite loop @_@

>would that fix the memory leak?
No
>also, how can I stop the infinite loop
use the break statement :)

By the way: Customer *head = NULL; is a very dangerous instruction, you're assigning a value to an unknown memory address :)

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

if(sub_choice(action))
{
      while (action == 'C')
   {
     Customer *CUST = new Customer;
      int count = 1;
       CUST->lineNumber = count;
        count++;
        CUST = CUST-> next;
                    }
}

You get a Infinite Loop because the value of action doesnt change once you give the input as c.

And a memory leak because you dont delete the variables created by the NEW STATEMENT.

So Heres the thing.

THink user types 'ccc'
you read 'c' so now the input stream has 'cc' left in it.
Then you are running the while loop.
only depending upon the first 'c';

SO what do you do to take in the second and third 'c' left in the input stream?

You also have to keep in mind that there's a difference between the lowercase 'c' and the uppercase 'C' :)

so I should use a switch statement? instead of a if statement?

and I'm not sure how to move onto the next c on the input stream.

So if I didn't misunderstand you, you want:

  1. let the user press the 'c'-key on his keyboard and count the number of times the 'c'-key was pressed
  2. create an array of structures with as it's upper bound the number of times the 'c'-key was pressed

Is that right?

>>so I should use a switch statement? instead of a if statement?
While loops use break switches too.

>>and I'm not sure how to move onto the next c on the input stream.
If
cin>>action;
reads the first character , then the second can be read by the another use of cin>>action ; :)

so I should use a switch statement? instead of a if statement?

and I'm not sure how to move onto the next c on the input stream.

switch and if/else-if statements are essentially the same (they both evaluate conditions), but the difference between them is that a switch is executed faster and it's better for your code readability (if you're testing for much different values)

By the way: break can also be used to stop a loop (like the while or for loop)

@Tux , basically that's what I want c to do.


Well i'm not really testing for different values, so if else statments would be better?

@ sky -
Still kind of lost...

is it

if
cin>>action
cin>>action ?

switch and if/else-if statements are essentially the same (they both evaluate conditions), but the difference between them is that a switch is executed faster and it's better for your code readability (if you're testing for much different values)

The only backdrop for switch statements is that they can be used against constant values.

Which is perfect for what you want ;)

Hey put cin>>action; inside your While Loop. So once you enter anything apart from C.the while loop breaks.

@Tux , basically that's what I want c to do.

Why don't you just get the input as a whole string then?

  1. Get the input as a whole string
  2. Loop trough the string character by character and if the character is a 'c' or a 'C' then you increase a counter by one, after the loop has finished you know how much structures you need in your array :)

Or...why don't you just let the user enter the desired number?

Why don't you just get the input as a whole string then?

  1. Get the input as a whole string
  2. Loop trough the string character by character and if the character is a 'c' or a 'C' then you increase a counter by one, after the loop has finished you know how much structures you need in your array :)

Or...why don't you just let the user enter the desired number?

I think that the user doesnt know how many customers are present.
So whenever a customer enters his restaurant(or any other shop) He presses 'C' to create a new
Customer Profile.
Is that the case BlackStar?

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

if(sub_choice(action))
{    int count = 1;
      while (action == 'C')
   {
     cin>>action;  // is this what you meant by putting the cin>>action here in the while loop?
     Customer *CUST = new Customer;
       CUST->lineNumber = count;
        count++;
        CUST = CUST-> next;
                    }
}

Yup, so the amount of customer profile = to how many times he presses c.

I think that the user doesnt know how many customers are present.
So whenever a customer enters his restaurant(or any other shop) He presses 'C' to create a new
Customer Profile.
Is that the case BlackStar?

In that case it might be useful for him to use a vector of auto_ptr s :)

Yup, so the amount of customer profile = to how many times he presses c.

So you can actually just ask the user how many customer profiles he wants and get it as an integer for example?

So are there any other things involved apart from Counting the number of Customers?

what if I don't use a vector of auto_ptrs

well ask the customer what they want.
then once they are done.
they get removed from the line.

So How Do you want to exit from the program.

Let us assume that You want to exit when you press 'x';

so

char action;
while(action!=x)
{
cin>>action;
   switch(action)
{
case 'c':
case 'C':
//do something over here;
}


}

That would be the appropriate code.

What was wrong with you code was that the while loop was only focused on one single action

what if I don't use a vector of auto_ptrs

Then you're making it yourself difficult, but why don't you just get the whole line of 'c's first? The only thing left then is count how many times the 'c' occurred and use dynamic memory allocation to allocate the appropriate amount of memory for it ...

Then you're making it yourself difficult, but why don't you just get the whole line of 'c's first? The only thing left then is count how many times the 'c' occurred and use dynamic memory allocation to allocate the appropriate amount of memory for it ...

I think that using vector of customers would be more appropriate for this .

Black Star Uses character based input and not string based because, Its like a real Shop Scenario.
WHERE CUSTOMERS CAN arrive at any point of time.

I think that using vector of customers would be more appropriate for this .

Yeah that's absolutely right, I was so much into 'thinking in pointers' that I just forgot about doing it in the easy way :)

Black Star Uses character based input and not string based because, Its like a real Shop Scenario.
WHERE CUSTOMERS CAN arrive at any point of time.

I finally fully understand what he's trying to do, in this case using vectors is definitely the way he should go !!

I'm new..
Is memory leak problem solved..

I'm new..
Is memory leak problem solved..

We don't know, if he'll use vectors, the memory leak and his whole problem will be solved immediately :)

I'm sticking with Link list tho @_@

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.