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