| | |
trying to loop my program
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2007
Posts: 32
Reputation:
Solved Threads: 0
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?
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)
#include <iomanip> #include <cstdlib> #include <iostream> #include "RetailItem.h" #include "RetailStore.h" using namespace std; const unsigned W = 20; int main(int argc, char *argv[]) { RetailItem r; RetailStore s; char z; s.setZero(); do { cout<<"This is Programming Assignment #7"<<endl; cout<<"THE RETAIL STORE MANAGER"<<endl; cout<<"By Jeremy Rice of CSCI 111"<<endl; cout<<"Please choose from the following options"<<endl; cout<<"\n 1 - Display total inventory"<<endl; cout<<"\n 2 - Search for an item"<<endl; cout<<"\n 3 - Add a new item to inventory"<<endl; cout<<"\n 4 - Sell/Subtract units of an item"<<endl; cout<<"\n 5 - Buy/Add units of an item"<<endl; cout<<"\n 6 - Change description of an item"<<endl; cout<<"\n 7 - Delete an item from the inventory"<<endl; cout<<"\n 8 - List items that need ordered"<<endl; cout<<"\n 0 - Quit"<<endl; cout<<"Your choice: "; cin>>s.choice; // IF CHOICE ONE IS ENTERED // DISPLAY ALL INVENTORY if (s.choice == 1) { cout<<"Item"<<setw(20)<<"Description"<<setw(20)<<"Units on Hand"; cout<<setw(15)<<"Price ($)"; cout<<setw(15)<<"Subtotal($)"<<endl; s.getStoreInventory(); } //IF CHOICE TWO IS ENTERED //SEARCH ITEM BY ITS DESCRIPTION if (s.choice == 2) { s.getSearch(); } // IF CHOICE THREE IS ENTERED // ADD INVENTORY TO THE STORE if (s.choice == 3) { s.getAddToStore(); } // IF CHOICE FOUR IS ENTERED // SUBTRACT UNITS FROM ITEMS if (s.choice == 4) { cout<<"Item"<<setw(20)<<"Description"<<setw(20)<<"Units on Hand"; cout<<setw(15)<<"Price ($)"; cout<<endl; s.setSubtractUnit(); } // IF CHOICE FIVE IS ENTERED // ADD UNITS TO ITEMS if (s.choice == 5) { cout<<"Item"<<setw(20)<<"Description"<<setw(20)<<"Units on Hand"<<setw(15)<<"Price ($)"; cout<<endl; s.setAddUnit(); } // IF CHOICE SIX IS ENTERED // CHANGES THE DESCRIPTION OF AN ITEM if (s.choice == 6) { s.setChangeDescription(); } // DELETE AN ITEM FROM THE INVENTORY if (s.choice == 7) { s.getDeleteItem(); } // IF CHOICE EIGHT IS ENTERED // LIST AND RE ORDER if (s.choice == 8) { s.setOrderItems(); } // IF ZERO IS ENTERED // EXIT PROGRAM if (s.choice == 0) { system("pause"); return 0; } cout<<"Continue? (Y/N): "; cin>>z; } while ((z == 'y')||(z == 'Y')); }
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
Edit: made a smiley out of my parenthesis :O
Last edited by stokey; Dec 13th, 2007 at 10:15 pm.
>>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.
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.
•
•
•
•
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)
while(1) { // do something }
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.
•
•
•
•
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)
while(1) { // do something }
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)
while(1) { ... if(/*if user wants to quit*/) { break; } }
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.
"The only truly secure computer is one buried in concrete, with the power turned off and the network cable cut." - Anonymous.
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 ::
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....
what's the big difference???plz explain...
anyways i m posting what i would have done ::
C++ Syntax (Toggle Plain Text)
s.choice=1 while(s.choice) { print_menu(); cin>>s.choice; //you may check for a valid input choice... switch(s.choice) { case 1: //do the required work continue; case 2: //do the required work continue; .... .... case 0: //quit.. return 0; OR break; } }
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
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
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
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
@waltp
Sorry but i am new to this community..i didnt know about this....i will take care of this in future..
and
dont from your statement
why????
and please comment on the solution...
thanks & regards...
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)
return
C++ Syntax (Toggle Plain Text)
case
why????
and please comment on the solution...
thanks & regards...
![]() |
Similar Threads
- infinite loop... (C++)
- for loop error (C)
- loop to create arrays when reading a file (Java)
- Loop program help (C++)
- Someone Be My Program Study Buddy (C++)
- need help w/for loop (C++)
- a program about fractions (C++)
- Interpretation of an instructors C++ program... (C++)
Other Threads in the C++ Forum
- Previous Thread: programming C or C++ on my symbian phone S60 v3 (nokia e61i)
- Next Thread: Need Help Plz
Views: 1623 | Replies: 10
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






