help

closed account (48CM4iN6)
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;


cout<<"Sold: ";
cin>>prod[x].sold;

if(prod[x].sold<=prod[x].stock)
prod[x].left = prod[x].stock - prod[x].sold;

}while(prod[x].sold>prod[x].stock);

}//end for

}//end outer for

}//end catch

system("cls");
PDISPLAY(prod,val);

} //end INPUTPROD

//////////////////////////////////
void PDISPLAY(Products temp[], int sval)
{
int i, x, ctr=0,ctrl;

HANDLE hStdout = GetStdHandle( STD_OUTPUT_HANDLE );

cout<<"\t\t ***** Inventory System C++.2 ***** \n";

cout<<"PROD.NO PRODUCT Name \t Price\tStock\tSold\tLeft \n";

for (i=0;i<sval;i++)
{

gotoxy(hStdout,3,2+i+ctr);
cout<<"["<<i+1<<"]";
gotoxy(hStdout,11,2+i+ctr);
cout<<temp[i].name;

for(x=0;x<temp[i].count;x++)
{
ctrl=2+i+ctr;

gotoxy(hStdout,20,ctrl);
cout<<temp[x].pname ;
temp[x].pname++;
gotoxy(hStdout,41,ctrl);
cout<<temp[x].stock;

gotoxy(hStdout,49,ctrl);
cout<<temp[x].sold;

gotoxy(hStdout,57,ctrl);
cout<<temp[x].left;

gotoxy(hStdout,33,ctrl);
cout<<setprecision(2)<<temp[x].price;

ctr++;

}//end inner for

}//end outer for

}//end PDISPLAY
Last edited on
These lines are incorrect:
for(x=0;x<prod[i].count;x++)
I believe you wanted something like:
for(x=0;x<sizeof(prod);x++)

But since I have no idea what Products is, I can't ensure that is right.

Also, when pasting code, keep it in the [code][/code] tags so it's easier to read.
Last edited on
closed account (48CM4iN6)
ok . I am going to post my whole code. Try to run it to be able to understand my problem.

Here:



#include<iostream>
#include<string>
#include<cctype>
#include<cstdlib>
#include <iomanip>
#include <windows.h>
#include<conio.h>
#include<ctype.h>

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


////////////////////////////////////////////////////////////////////

void main() // DO NOT ADD or REVISE ANYTHING FROM THIS FUNCTION
{
int count;

//password();

cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);


cout<<" ***** INVENTORY SYSTEM CS127L 4TH QTR*****"<<endl<<endl;



count=ENTERPROD();

cout<<endl<<"ENTER "<<count<<" PRODUCTS"<<endl;



INPUTPROD(count);


cout<<endl;

}//end main


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


cout<<"Sold: ";
cin>>prod[x].sold;

if(prod[x].sold<=prod[x].stock)
prod[x].left = prod[x].stock - prod[x].sold;

}while(prod[x].sold>prod[x].stock);

}//end for

}//end outer for

}//end catch

system("cls");
PDISPLAY(prod,val);

} //end INPUTPROD

//////////////////////////////////
void PDISPLAY(Products temp[], int sval)
{
int i, x, ctr=0,ctrl;

HANDLE hStdout = GetStdHandle( STD_OUTPUT_HANDLE );

cout<<"\t\t ***** Inventory System C++.2 ***** \n";

cout<<"PROD.NO PRODUCT Name \t Price\tStock\tSold\tLeft \n";

for (i=0;i<sval;i++)
{

gotoxy(hStdout,3,2+i+ctr);
cout<<"["<<i+1<<"]";
gotoxy(hStdout,11,2+i+ctr);
cout<<temp[i].name;

for(x=0;x<temp[i].count;x++)
{
ctrl=2+i+ctr;

gotoxy(hStdout,20,ctrl);
cout<<temp[x].pname ;
temp[x].pname++;
gotoxy(hStdout,41,ctrl);
cout<<temp[x].stock;

gotoxy(hStdout,49,ctrl);
cout<<temp[x].sold;

gotoxy(hStdout,57,ctrl);
cout<<temp[x].left;

gotoxy(hStdout,33,ctrl);
cout<<setprecision(2)<<temp[x].price;

ctr++;

}//end inner for

}//end outer for

}//end PDISPLAY

//////////////////////////////////////////////////////////////////////////////////
void password()
{

int pa=0;
bool flag=true;
string user;

cout<<"USERNAME: "; //Username: Gloren26
cin>>user;

system("cls");
cout<<"enter password:"; //password: 12345



while(flag)
{
string password=EnterPassword();//"hello"

try
{
if((password=="12345")&&(user=="Gloren26"))
{
throw pcattempt();
}//end if

else
throw rattempt();


} //end try

catch(pcattempt)
{

cout<<"\nprocessing your password";

for(int i=1;i<=4;i++)
{ Sleep(1000);
cout<<".";
}//end for

Sleep(3000);
flag=false;
system("cls");
}//end catch

catch(rattempt)
{
system("cls");
cout<<"Invalid Username or Password!\n";
cout<<"USERNAME: "; //Username: Gloren26
cin>>user;
system("cls");
cout<<"enter password:";
pa++;

if (pa>=3)
{
flag=false;
system("cls");
cout<<"You have Exceeded the attempts allowed "<<endl;
exit(1);
}//end if
continue;
}//end catch



}//end while


}
As an aside, using exceptions (try-catch) instead of proper return values and flow control is a bad thing.
I'm having a tough time understanding what's supposed to be happening here:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
      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.
closed account (48CM4iN6)
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.
Topic archived. No new replies allowed.