hi guys. i'm still a beginner in c++ . I have a problem in my code. The Products entered after Product 1 overwrites the prices/stock/sold/left in product. How can i solve this problem?
here is my code:
int ENTERPROD()
{
int numprod;
do
{
cout<<"ENTER NUMBER OF PRODUCTS FOR INVENTORY: ";
cin>>numprod;
if(numprod<0)
cout<<"Error! Positive Input for Inventory Only!"<<endl;
}while(numprod<0);
return numprod;
}
/////////////////////////////////////
void INPUTPROD(int val)
{
int i, x;
Products prod[1000];
try
{
for(i=0;i<val;i++)
{
cin.ignore();
cout<<"Product"<<i+1<<":";
getline(cin,prod[i].name);
cin.ignore();
do
{
cout<<"How many "<<prod[i].name<<"?";
cin>>prod[i].count;
if (prod[i].count<0)
cout<<"Error! Positive Input for Inventory Only!"<<endl;
}while(prod[i].count<0); //end do while
for(x=0;x<prod[i].count;x++)
{
cin.ignore();
cout<<prod[i].name<<"["<<x+1<<"] :";
getline(cin,prod[x].pname);
cin.ignore();
cout<<endl;
do
{
cout<<"Price: Php";
cin>>prod[x].price;
if(prod[x].price<0)
cout<<"Error! Price should be Positive !"<<endl;
}while (prod[x].price<0);
do
{
cout<<"Stock: ";
cin>>prod[x].stock;
if(prod[x].stock<0)
cout<<"Error! Stock should be Positive !"<<endl;
}while(prod[x].stock<0);
do
{
cout<<"Sold: ";
cin>>prod[x].sold;
prod[x].left= prod[x].stock - prod[x].sold;
if(prod[x].sold<0)
cout<<"Error! Sold should be positive !"<<endl;
if(prod[x].sold>prod[x].stock)
throw nvalid();
}while(prod[x].sold<0);
cout<<endl;
}//end inner for
cout<<endl;
}//end outer for
}//end try
catch(nvalid)
{
for(i=0;i<val;i++)
{
for(x=0;x<prod[i].count;x++)
{
do
{
cout<<"Error! Number of Stocks is Less than Sold !";
cout<<endl;
class pcattempt{};
class rattempt{};
class nvalid{};
using namespace std;
//Username is Gloren26
//password is 12345
//////// function that takes the direction of x and y coordinates////////
void gotoxy( HANDLE StdOut, SHORT x, SHORT y )
{
// Set the cursor position.
COORD Cord;
Cord.X = x;
Cord.Y = y;
SetConsoleCursorPosition( StdOut, Cord );
}
////////////use this structure name in creating the members of inventory//////////////
struct Products
{
string name;
string pname;
int count;
int stock;
double price;
int sold;
int left;
};
//////////////////////////////code for getting the password//////////////////////////////////
string EnterPassword()
{
string NumAsString="";
char ch=getch();//h
while (ch!='\r'){//true
cout<<'*';//*****
NumAsString+=ch;//"hello"
ch=getch();//enter/return
}
return NumAsString;
}
/////////////////////prototypes of functions////////////////////////////////////////////////
void password();
int ENTERPROD();
void INPUTPROD(int val);
void PDISPLAY(Products temp[] , int sval);
for(i=0; i<val; i++)
{
cin.ignore();
cout<<"Product"<<i+1<<":";
getline(cin,prod[i].name);
cin.ignore();
do {
cout<<"How many "<<prod[i].name<<"?";
cin>>prod[i].count;
if (prod[i].count<0)
cout<<"Error! Positive Input for Inventory Only!"<<endl;
} while(prod[i].count<0); //end do while
for(x=0; x<prod[i].count; x++)
{
cin.ignore();
cout<<prod[i].name<<"["<<x+1<<"] :";
getline(cin,prod[x].pname);
cin.ignore();
cout<<endl;
You have the user enter information for a certain value at prod[i], then you go and change it back to prod[0] and have them input different information. Why?
In a sense, you're erasing all of the information over and over again, and only the final product's information is actually stored correctly.
Volatile Pulse . Thank you for your reply. I have finished my code. I realized what you said and that I am working a table output. I changed "Proudcts prod[1000]" to prod[10][10] . I used 2d arrays and changed the whole arrays.
I have to agree with what Moschops said too about the try-catch thing. It's very redundant in such simple code when a simple loop would suffice. You shouldn't have to worry about error handling in something so simple, atleast not in my opinion. It could also greatly cut down on the lines in your program and you should get the same results just by using return statements.