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?

#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'));
}

Recommended Answers

All 10 Replies

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

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.

while(1)
{
   // do something
}

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.

while(1)
{
   // do something
}

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)

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

while(1)
{
    ...
    if(/*if user wants to quit*/)
    {
        break;
    }
}

You should use switch case instead of "if"

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

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

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

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.

@waltp

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

and
dont

return

from your

case

statement
why????

and please comment on the solution...

thanks & regards...

@waltp

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

So you simply ignoreed all the references to The Rules when you signed up? Not good... :icon_rolleyes:

and
dont

return

from your

case

statement
why????

Because it's just as easy to exit the loop and let the real return take care of it. And it's bad programming practice... Ideally, functions should only return from one place. This is not a rule set in stone, but a guideline. It can't always be accomplished.


and please comment on the solution...

thanks & regards...

It works fine.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.