943,545 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1971
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Dec 13th, 2007
0

trying to loop my program

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




C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 30
Solved Threads: 0
Light Poster
jrice528 is offline Offline
32 posts
since Oct 2007
Dec 13th, 2007
0

Re: trying to loop my program

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
stokey is offline Offline
6 posts
since Dec 2007
Dec 13th, 2007
0

Re: trying to loop my program

>>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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,947 posts
since Aug 2005
Dec 13th, 2007
0

Re: trying to loop my program

Click to Expand / Collapse  Quote originally posted by jrice528 ...
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.
C++ Syntax (Toggle Plain Text)
  1. while(1)
  2. {
  3. // do something
  4. }
Last edited by Ancient Dragon; Dec 13th, 2007 at 10:22 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,947 posts
since Aug 2005
Dec 13th, 2007
0

Re: trying to loop my program

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.
C++ Syntax (Toggle Plain Text)
  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)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
stokey is offline Offline
6 posts
since Dec 2007
Dec 14th, 2007
0

Re: trying to loop my program

You won't get any warnings if you add a break statement when the user chooses 0 (the exit option).

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 193
Solved Threads: 25
Posting Pro
Jishnu is offline Offline
518 posts
since Oct 2006
Dec 15th, 2007
0

Re: trying to loop my program

You should use switch case instead of "if"
C-+
Reputation Points: 10
Solved Threads: 0
Newbie Poster
C-+ is offline Offline
10 posts
since Dec 2007
Dec 16th, 2007
0

Re: trying to loop my program

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

C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 14
Solved Threads: 7
Junior Poster
rajatC is offline Offline
109 posts
since Dec 2007
Dec 16th, 2007
0

Re: trying to loop my program

Click to Expand / Collapse  Quote originally posted by rajatC ...
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.
Moderator
Reputation Points: 3275
Solved Threads: 890
Posting Sage
WaltP is offline Offline
7,716 posts
since May 2006
Dec 16th, 2007
0

Re: trying to loop my program

@waltp

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

and
dont
C++ Syntax (Toggle Plain Text)
  1. return
from your
C++ Syntax (Toggle Plain Text)
  1. case
statement

why????

and please comment on the solution...

thanks & regards...
Reputation Points: 14
Solved Threads: 7
Junior Poster
rajatC is offline Offline
109 posts
since Dec 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Loop back to previous loop?
Next Thread in C++ Forum Timeline: Need Help Plz





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC