954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Pressing a Button makes a new thing

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

BlackStar
Junior Poster
188 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

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

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

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

BlackStar
Junior Poster
188 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

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

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 


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 @_@

BlackStar
Junior Poster
188 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

>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 :)

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 
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?

Sky Diploma
Practically a Posting Shark
865 posts since Mar 2008
Reputation Points: 673
Solved Threads: 131
 

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

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

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.

BlackStar
Junior Poster
188 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

So if I didn't misunderstand you, you want: let the user press the 'c'-key on his keyboard and count the number of times the 'c'-key was pressed
create an array of structures with as it's upper bound the number of times the 'c'-key was pressed
Is that right?

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

>>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 ; :)

Sky Diploma
Practically a Posting Shark
865 posts since Mar 2008
Reputation Points: 673
Solved Threads: 131
 

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)

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

@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 ?

BlackStar
Junior Poster
188 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 
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 ;)

Sky Diploma
Practically a Posting Shark
865 posts since Mar 2008
Reputation Points: 673
Solved Threads: 131
 

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

Sky Diploma
Practically a Posting Shark
865 posts since Mar 2008
Reputation Points: 673
Solved Threads: 131
 
@Tux , basically that's what I want c to do.

Why don't you just get the input as a whole string then?Get the input as a whole string
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?

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 
Why don't you just get the input as a whole string then?
  • Get the input as a whole string
  • 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?

Sky Diploma
Practically a Posting Shark
865 posts since Mar 2008
Reputation Points: 673
Solved Threads: 131
 


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;
}
}

BlackStar
Junior Poster
188 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

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

BlackStar
Junior Poster
188 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 
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?

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You