Hi guys,

I am trying to modify my program so that when i entered an invalid number, it will say that this is invalid and loop me back to the starting point. I just cant find the point to do that. either it will just say invalid and continues, or it goes into endless loop.

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
struct product
{
	int pCode;
	string pDesc;
	double pCost;
	bool sPromo;
};

double lookupPrice(int);

int main()
{
int prodCode = 0;
	double pprice;



	pprice = lookupPrice(prodCode);
	cout << "Price is "<<pprice;
	
	
	


	cin.ignore();
	cin.ignore();
	return 0;


}
double lookupPrice(int prodCode)
{
	int flag=0;
	double pprice2;
	double pprice3;
	int salequantity;
	product products[4] =
	{
			{1000, "Pilot Blue Pen", 1.20 , 1},
			{1001, "Pilot Black Pen", 1.35 , 0},
			{1002, "File", 1.50 , 0},
			{1003, "Exercise Book", 0.90 , 1}
	};
	while(flag==0)
	{
	
	cout << "Please enter 0 to end, a product code to continue: "; // 
	cin >> prodCode; // when i put an invalid product code which is pCode, it should prompt me invalid and loop back to the previous statement. 


	if (prodCode == 0) 
		{
			flag = 1;
			return (pprice3);
		}
			


	else if(prodCode=products[4].pCode)
		{

		cout << "Please enter quantity: ";
	cin >> salequantity;



	
	cout <<endl;
	cout << setiosflags(ios::left);


	for ( int i=0; i < 4; i++)
			
	
	if (prodCode == products[i].pCode)
		
		if (products[i].sPromo==1)
			{
	
		pprice2=products[i].pCost-products[i].pCost/10;
		
			}

	else 

			{
	
		pprice2=products[i].pCost;
	
			}
		}
	else
	{
		cout<<"invalid";}
	pprice3 = pprice2 * salequantity;
	}
	
		
	
		return (pprice3);
	
}

Take in product code as a string rather than an integer, and then verify that all of the characters in the string are digits.

Just as an aside, why are you passing in the value for product code into the function if you're just going to ask the user to input it?

well... this is actually for an assignment and i need to fulfill the conditions :O


Write a main program that reads in a series of product code and sales quantity interactively. It invokes the function lookupPrice, providing the product code to look up for the price. The sales amount is computed by multiplying the sales quantity with this returned price. The total sales are accumulated for each entry. The program uses the sentinel value, 0 to end the data entry. The program computes a further discount by using the information given in Table 5. A sample run of the program is as follows.

Write a main program that reads in a series of product code

Ah, okay. So all of your input (loops included) should be in main, then. Try implementing the string validation part and post back if you have trouble.

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

typedef struct prod  {
   int pCode;
   char pDesc[256];
   float pCost;
   bool sPromo;
   float promoPrice;  //use a member for the promo price

 }product;

float lookupPrice ();

int main()  {
   float TotalPrice;

   TotalPrice = lookupPrice();
   cout << "Price is "<< TotalPrice;
	        
//   cin.ignore();
//   cin.ignore();
   return 0;
 }

float lookupPrice ()  {
   int prodCode = 0;
                            
   bool flag=1;             
   float pprice=0.0;        
   float SaleQuantity=0.0;  

   product products[]={  {1000, "Pilot Blue Pen", 1.20 , 1},
                         {1001, "Pilot Black Pen", 1.35 , 0},                        
                         {1002, "File", 1.50 , 0},
                         {1003, "Exercise Book", 0.90 , 1}
                      };
  
   while (flag)  {
   
      cout << "Please enter 0 to end, a product code to continue: "; // 
      cin >> prodCode; // when i put an invalid product code which is pCode, it should prompt me invalid and loop back to the previous statement. 

      if (prodCode == 0)  {      
         break;
       }
				
      cout <<endl;
      cout << setiosflags(ios::left);
      bool ProductFound=0;

      for ( int i=0; i < 4; i++)  {
         
         if (prodCode == products[i].pCode)  {
            ProductFound=1;
            cout << "Please enter quantity: ";
            cin >> SaleQuantity;

            if (products[i].sPromo)  {  
                products[i].promoPrice=products[i].pCost-products[i].pCost/10;
                pprice=SaleQuantity*products[i].promoPrice;                
              }

            else if (!products[i].sPromo) {
               pprice=products[i].pCost*SaleQuantity;
             }
          }         
       }
      if (!ProductFound) cout << "\nInvalid input...\n";
      std::cout << "\nContinue?\n";
      cin >> flag; // 0 or 1 but you can further customize this
    }  
   return (pprice);   
 }
commented: You've been asked... -1
commented: Thanks for calling my attention to this post. Don't do this. +0

thanks to both of you! especially caut baia! i did some modifications as per your idea and almost got it! thanks!

If something is unclear you are welcome to ask.I don't know where you could stumble ..so feel free

If something is unclear you are welcome to ask.

Yes, why do you keep giving complete, compilable, cookie cutter solutions that the OP can simply turn in for a grade even thought you have been asked more than repeatedly not to do this?

commented: Keep hammering on this, it's worth the effort. +2

Well because i want to.And my effort in actually compiling the code and making some adjustments made a difference beside yours.. It actually helped.Get off my back man.If he wants to learn he will without you freaking out every time i post some code.Otherwise he will just get a high grade and mind his own interests while it can still be considered help.I'd say .. the fact that you got some power to affect someones reputation makes it hard for you to resist not to.Boys with sticks .. some can't just play nice

If he wants to learn he will

He won't if he just copies your code verbatim.

the fact that you got some power to affect someones reputation

That doesn't matter. Even if I had zero rep power, I would still give you the comments (it also helps others to be aware of what you're up to).

Boys with sticks ..

You're the one that needs to grow up and learn to be a part of a community.

First of all it's not up to you to decide if i can be of help or not.That is up to who asks for help to judge.He showed that he tried and not just came here asking for code without trying to solve his problem on his own.Again if someone is interested in learning will do so by trying to understand the actual code and enhance his knowledge.And it is you who should grow up by trying to cope with the fact that in any community there are people who do not share your opinions and are likely as free as you to consider things their way.I think i didn't break any rules since everyone is posting code around and i didn't see you crying out on every thread but those in which i posted.I had a problem a few days ago and i would have appreciated some code which indeed i received from someone and i was very thankful.I may be wrong .. but you may be too.Try to consider this

@jonsca yes you correct that we do not like people just drop in their assignments and expect our members to provide solution. One of the announcements says We only give homework help to those who show effort however that doesn't mean we will punish people if they provide full solutions.

@caut_baia we do appreciate that yo do take your time to help people, but really is it worth to give whole solution without original poster sweating little more with it, working on given clues/advice. How much would they learn without trying by simple copy and paste of solution? Maybe perhaps in the future you can provide just snipped to get assignment moving forward then whole solution.

I hope this is solved in satisfactory for each side.

He won't if he just copies your code verbatim.


That doesn't matter. Even if I had zero rep power, I would still give you the comments (it also helps others to be aware of what you're up to).


You're the one that needs to grow up and learn to be a part of a community.

well, I am trying to learn what has gone wrong. In fact the ones that you all taught me here was not really covered in the lecture and i have to really learn from books. give me a break as i just start learning 1 month ago from scratch

give me a break

I have a long-standing disagreement with that user, and absolutely none of that was criticism was meant to be directed at you, so I apologize if you got caught in the middle of it.

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.