Pressing a Button makes a new thing

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2009
Posts: 186
Reputation: BlackStar is an unknown quantity at this point 
Solved Threads: 0
BlackStar BlackStar is offline Offline
Junior Poster

Pressing a Button makes a new thing

 
0
  #1
May 10th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Pressing a Button makes a new thing

 
0
  #2
May 10th, 2009
int count = 1; , you should declare it outside your while-loop
Last edited by tux4life; May 10th, 2009 at 3:11 pm.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 186
Reputation: BlackStar is an unknown quantity at this point 
Solved Threads: 0
BlackStar BlackStar is offline Offline
Junior Poster

Re: Pressing a Button makes a new thing

 
0
  #3
May 10th, 2009
But I'll still get a infinite loop though, wouldn't I?
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Pressing a Button makes a new thing

 
0
  #4
May 10th, 2009
Customer *CUST = new Customer; will cause a memory leak also
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 186
Reputation: BlackStar is an unknown quantity at this point 
Solved Threads: 0
BlackStar BlackStar is offline Offline
Junior Poster

Re: Pressing a Button makes a new thing

 
0
  #5
May 10th, 2009
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 @_@
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Pressing a Button makes a new thing

 
0
  #6
May 10th, 2009
>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
Last edited by tux4life; May 10th, 2009 at 3:28 pm.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 672
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 99
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

Re: Pressing a Button makes a new thing

 
0
  #7
May 10th, 2009
  1. char action;
  2. cout<<"how many?";
  3. cin>>action;
  4.  
  5. if(sub_choice(action))
  6. {
  7. while (action == 'C')
  8. {
  9. Customer *CUST = new Customer;
  10. int count = 1;
  11. CUST->lineNumber = count;
  12. count++;
  13. CUST = CUST-> next;
  14. }
  15. }
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?
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Pressing a Button makes a new thing

 
0
  #8
May 10th, 2009
You also have to keep in mind that there's a difference between the lowercase 'c' and the uppercase 'C'
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 186
Reputation: BlackStar is an unknown quantity at this point 
Solved Threads: 0
BlackStar BlackStar is offline Offline
Junior Poster

Re: Pressing a Button makes a new thing

 
0
  #9
May 10th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Pressing a Button makes a new thing

 
0
  #10
May 10th, 2009
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?
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC