trying to loop my program

Reply

Join Date: Oct 2007
Posts: 32
Reputation: jrice528 is an unknown quantity at this point 
Solved Threads: 0
jrice528 jrice528 is offline Offline
Light Poster

trying to loop my program

 
0
  #1
Dec 13th, 2007
In my program I am using the do-while loop to go through program..

my question is how do I loop the program without the do-while loop...
rather that when the user enters menu number 0, it quits the program...
Teacher uses a script to grade the programs and on hsi example it just runs over and over without it asking "continue?"

any advice?




  1. #include <iomanip>
  2. #include <cstdlib>
  3. #include <iostream>
  4. #include "RetailItem.h"
  5. #include "RetailStore.h"
  6.  
  7. using namespace std;
  8. const unsigned W = 20;
  9.  
  10.  
  11. int main(int argc, char *argv[])
  12. {
  13. RetailItem r;
  14. RetailStore s;
  15. char z;
  16.  
  17. s.setZero();
  18.  
  19. do
  20. {
  21. cout<<"This is Programming Assignment #7"<<endl;
  22. cout<<"THE RETAIL STORE MANAGER"<<endl;
  23. cout<<"By Jeremy Rice of CSCI 111"<<endl;
  24. cout<<"Please choose from the following options"<<endl;
  25. cout<<"\n 1 - Display total inventory"<<endl;
  26. cout<<"\n 2 - Search for an item"<<endl;
  27. cout<<"\n 3 - Add a new item to inventory"<<endl;
  28. cout<<"\n 4 - Sell/Subtract units of an item"<<endl;
  29. cout<<"\n 5 - Buy/Add units of an item"<<endl;
  30. cout<<"\n 6 - Change description of an item"<<endl;
  31. cout<<"\n 7 - Delete an item from the inventory"<<endl;
  32. cout<<"\n 8 - List items that need ordered"<<endl;
  33. cout<<"\n 0 - Quit"<<endl;
  34. cout<<"Your choice: ";
  35. cin>>s.choice;
  36.  
  37. // IF CHOICE ONE IS ENTERED
  38. // DISPLAY ALL INVENTORY
  39.  
  40. if (s.choice == 1)
  41. {
  42. cout<<"Item"<<setw(20)<<"Description"<<setw(20)<<"Units on Hand";
  43. cout<<setw(15)<<"Price ($)";
  44. cout<<setw(15)<<"Subtotal($)"<<endl;
  45. s.getStoreInventory();
  46. }
  47.  
  48. //IF CHOICE TWO IS ENTERED
  49. //SEARCH ITEM BY ITS DESCRIPTION
  50.  
  51. if (s.choice == 2)
  52. {
  53. s.getSearch();
  54. }
  55.  
  56. // IF CHOICE THREE IS ENTERED
  57. // ADD INVENTORY TO THE STORE
  58.  
  59. if (s.choice == 3)
  60. {
  61. s.getAddToStore();
  62. }
  63.  
  64.  
  65. // IF CHOICE FOUR IS ENTERED
  66. // SUBTRACT UNITS FROM ITEMS
  67.  
  68. if (s.choice == 4)
  69. {
  70. cout<<"Item"<<setw(20)<<"Description"<<setw(20)<<"Units on Hand";
  71. cout<<setw(15)<<"Price ($)";
  72. cout<<endl;
  73. s.setSubtractUnit();
  74. }
  75.  
  76. // IF CHOICE FIVE IS ENTERED
  77. // ADD UNITS TO ITEMS
  78.  
  79. if (s.choice == 5)
  80. {
  81. cout<<"Item"<<setw(20)<<"Description"<<setw(20)<<"Units on Hand"<<setw(15)<<"Price ($)";
  82. cout<<endl;
  83. s.setAddUnit();
  84. }
  85. // IF CHOICE SIX IS ENTERED
  86. // CHANGES THE DESCRIPTION OF AN ITEM
  87.  
  88. if (s.choice == 6)
  89. {
  90. s.setChangeDescription();
  91. }
  92.  
  93. // DELETE AN ITEM FROM THE INVENTORY
  94.  
  95. if (s.choice == 7)
  96. {
  97. s.getDeleteItem();
  98. }
  99. // IF CHOICE EIGHT IS ENTERED
  100. // LIST AND RE ORDER
  101.  
  102. if (s.choice == 8)
  103. {
  104. s.setOrderItems();
  105. }
  106.  
  107.  
  108. // IF ZERO IS ENTERED
  109. // EXIT PROGRAM
  110.  
  111. if (s.choice == 0)
  112. {
  113. system("pause");
  114. return 0;
  115. }
  116. cout<<"Continue? (Y/N): ";
  117. cin>>z;
  118. }
  119. while ((z == 'y')||(z == 'Y'));
  120. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 6
Reputation: stokey is an unknown quantity at this point 
Solved Threads: 0
stokey's Avatar
stokey stokey is offline Offline
Newbie Poster

Re: trying to loop my program

 
0
  #2
Dec 13th, 2007
I'm not sure if this will work for you, I'm really new to c++ but if you put cin.get(); before the user input 0 to exit statement(may have to put several of the cin.get(); ) the program should stay open. I am still in the stage of using return 0; to exit a program so I'm not sure if this will work for you or not.

Edit: made a smiley out of my parenthesis :O
Last edited by stokey; Dec 13th, 2007 at 10:15 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,151
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1436
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Most Valuable Poster

Re: trying to loop my program

 
0
  #3
Dec 13th, 2007
>>Edit: made a smiley out of my parenthesis :O
That happens a lot. If you scroll down the page you will see a checkbox to disable smilies in text. You may have to click the Go Advanced button to see that checkbox.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,151
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1436
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Most Valuable Poster

Re: trying to loop my program

 
0
  #4
Dec 13th, 2007
Originally Posted by jrice528 View Post
In my program I am using the do-while loop to go through program..

my question is how do I loop the program without the do-while loop...
rather that when the user enters menu number 0, it quits the program...
Teacher uses a script to grade the programs and on hsi example it just runs over and over without it asking "continue?"

any advice?
you could replace the do-while loop with a simple loop that never ends. Is that what you mean? But having a Quit option I think is a better solution because it tells the user how to exit the program.
  1. while(1)
  2. {
  3. // do something
  4. }
Last edited by Ancient Dragon; Dec 13th, 2007 at 10:22 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 6
Reputation: stokey is an unknown quantity at this point 
Solved Threads: 0
stokey's Avatar
stokey stokey is offline Offline
Newbie Poster

Re: trying to loop my program

 
0
  #5
Dec 13th, 2007
Originally Posted by Ancient Dragon View Post
you could replace the do-while loop with a simple loop that never ends. Is that what you mean? But having a Quit option I think is a better solution because it tells the user how to exit the program.
  1. while(1)
  2. {
  3. // do something
  4. }
That may create some warnings with certain compilers if I'm not mistaken but I've only used what I received for class (ie. MS Visual Studio)
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 514
Reputation: Jishnu will become famous soon enough Jishnu will become famous soon enough 
Solved Threads: 26
Jishnu's Avatar
Jishnu Jishnu is offline Offline
Posting Pro

Re: trying to loop my program

 
0
  #6
Dec 14th, 2007
You won't get any warnings if you add a break statement when the user chooses 0 (the exit option).

  1. while(1)
  2. {
  3. ...
  4. if(/*if user wants to quit*/)
  5. {
  6. break;
  7. }
  8. }
Last edited by Jishnu; Dec 14th, 2007 at 3:11 am.
"You know you're a computer geek when you try to shoo a fly away from the monitor screen with your cursor. That just happened to me. It was scary." - Juuso Heimonen.

"The only truly secure computer is one buried in concrete, with the power turned off and the network cable cut." - Anonymous.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 10
Reputation: C-+ is an unknown quantity at this point 
Solved Threads: 0
C-+ C-+ is offline Offline
Newbie Poster

Re: trying to loop my program

 
0
  #7
Dec 15th, 2007
You should use switch case instead of "if"
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 109
Reputation: rajatC is an unknown quantity at this point 
Solved Threads: 7
rajatC's Avatar
rajatC rajatC is offline Offline
Junior Poster

Re: trying to loop my program

 
0
  #8
Dec 16th, 2007
well i m not getting what exactly do u want ....u dont want to use do-while but people are suggesting solution with while...
what's the big difference???plz explain...
anyways i m posting what i would have done ::

  1. s.choice=1
  2.  
  3. while(s.choice)
  4. {
  5. print_menu();
  6. cin>>s.choice; //you may check for a valid input choice...
  7. switch(s.choice)
  8. {
  9. case 1: //do the required work
  10. continue;
  11. case 2: //do the required work
  12. continue;
  13. ....
  14. ....
  15. case 0: //quit..
  16. return 0; OR break;
  17. }
  18. }
plz comment...

PS: i will suggeust u to keep main function as small as possible...it will be easy to understand...
do all the work in functions and just call them in main...it will give better readability....
Last edited by Ancient Dragon; Dec 17th, 2007 at 12:32 am. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: trying to loop my program

 
0
  #9
Dec 16th, 2007
Originally Posted by rajatC View Post
plz comment...
OK, here goes:
1) No CODE tags -- they are mentioned in The Rules as well as all over the site. Even where you typed in your post.

2) English!!!!
i m -- I'm
u -- you
dont -- don't
plz -- please
also mentioned in The Rules. This is not a chat room.

3) Don't return from your case statement.
Last edited by WaltP; Dec 16th, 2007 at 12:59 pm.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 109
Reputation: rajatC is an unknown quantity at this point 
Solved Threads: 7
rajatC's Avatar
rajatC rajatC is offline Offline
Junior Poster

Re: trying to loop my program

 
0
  #10
Dec 16th, 2007
@waltp

Sorry but i am new to this community..i didnt know about this....i will take care of this in future..

and
dont
  1. return
from your
  1. case
statement

why????

and please comment on the solution...

thanks & regards...
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