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