hey guys
we were asked to do this project in my collage it's about the products of a market
the problem i am facing in the code is that when i am filling the structure array's name and when i try to print it in the first cout it is printed but in the second cout a symbol is printed instead
just like in the pic below
#include<iostream>
#include<cstdlib>
usingnamespace std;
int op;
int c=0;
int operation()
{
cout<<"Please choose one of the following operations"<<endl;
cout<<"1 to add a product"<<endl;
cout<<"2 to view the products"<<endl;
cout<<"3 to sell a product"<<endl;
cout<<"4 to check empty space"<<endl;
cout<<"5 to exit"<<endl;
cin>>op;
return op;
}
struct products
{
char name[];
int quantity;
float price;
};
int main()
{
products arr[100];
cout<<"Welcome To your products manager"<<endl<<endl;
begin:
operation();
switch (op)
{
case 1:
cout<<"You choose to add a product"<<endl<<endl;
for(int i = (1+c) ; i <= 100 ; i++)
{
if(i>100)
{
cout<<"You exceed the number of your products"<<endl;
}
else
{
cout<<"Please enter the name of the product no."<<i<<endl;
cin>>arr[i].name;
cout<<"Now please enter the quantity of "<<arr[i].name<<endl;
cin>>arr[i].quantity;
cout<<"Now please enter the price of "<<arr[i].name<<endl;
cin>>arr[i].price;
cout<<"Product is successfully added"<<endl<<endl;
c++;
goto begin;
}
}
break;
case 2:
cout<<"You choose to view the products"<<endl<<endl;
if(c==0)
{
cout<<"You have no products yet"<<endl<<endl;
goto begin;
}
else{
for(int i =1 ; i <=c ; i++)
{
cout<<"Product no."<<i<<" name is "<<arr[i].name<<endl;
cout<<"Product no."<<i<<" quantity is "<<arr[i].quantity<<endl;
cout<<"Product no."<<i<<" price is "<<arr[i].price;
cout<<endl<<endl;
}
goto begin;
}
break;
case 3:
cout<<"You choose to sell a product"<<endl<<endl;
break;
case 4:
cout<<"You choose to check the empty space"<<endl<<endl;
cout<<"You still have "<<100-c<<" empty space"<<endl<<endl;
goto begin;
break;
case 5:
cout<<"You choose to Terminate the program"<<endl<<endl;
exit(0);
}
return 0;
}
struct products
{
char name[];
int quantity;
float price;
};
char name[]; declares an array with size 0. It cannot safely hold anything and all your problems stems from it. Use std::string here. It is way easier that way.
another problem guys
when i want sell a product i want t to check for it's availability first
so i want it to check all the structure array's name without saying every time that the product is not found but in the following code it keeps saying it isn't found until the if (y==arr[i].name) condition is true
case 3:
char y[100];
int z;
cout<<"You choose to sell a product"<<endl<<endl;
cout<<"Please enter the product's name to be sold"<<endl;
cin>>y;
for(int i =1 ; i <=c ; i++)
{
if(y==arr[i].name)
{
cout<<"Your product is found now please enter the quantity to be sold"<<endl;
cin>>z;
if (z>arr[i].quantity)
{
cout<<"The products number shouldn't be less than the sold ones"<<endl<<endl;
}
else
{
cout<<"The ramming quantity is "<<arr[i].quantity-z<<endl<<endl;
arr[i].quantity=arr[i].quantity-z;
cout<<"Total selling price is "<<arr[i].price*z<<endl<<endl;
}
}
else
cout<<"The product is not found"<<endl<<endl;
}
goto begin;
break;
also i want the user not to enter the same name for a product
but this code gives runtime error
is this initialization arr[i-1].name="index";if(arr[i].name==arr[i-1].name) is wrong ?
case 1:
int i;
arr[i-1].name="index";
cout<<"You choose to add a product"<<endl<<endl;
for(int i = (1+c) ; i <= 100 ; i++)
{
if(i>100)
{
cout<<"You exceed the number of your products"<<endl;
}
else
{
cout<<"Please enter the name of the product no."<<i<<endl;
cin>>arr[i].name;
if(arr[i].name==arr[i-1].name)
{
cout<<"The products can't have the same name"<<endl<<endl;
}
else
{
cout<<"Now please enter the quantity of "<<arr[i].name<<endl;
cin>>arr[i].quantity;
cout<<"Now please enter the price of "<<arr[i].name<<endl;
cin>>arr[i].price;
cout<<"Product is successfully added"<<endl<<endl;
c++;
goto begin;
}
}
}
break;
y==arr[i].nameYou cannot compare c-strings like that. Use strncmp function. arr[i-1].name="index"; You cannot assign c-strings like that. Use strncpy function.
#include<iostream>
#include<cstdlib>
usingnamespace std;
int op;
int c=0;
int operation()
{
cout<<"Please choose one of the following operations"<<endl;
cout<<"1 to add a product"<<endl;
cout<<"2 to view the products"<<endl;
cout<<"3 to sell a product"<<endl;
cout<<"4 to check empty space"<<endl;
cout<<"5 to exit"<<endl;
cin>>op;
return op;
}
struct products
{
std::string name;
int quantity;
float price;
};
int main()
{
products arr[100];
cout<<"Welcome To your products manager"<<endl<<endl;
begin:
operation();
switch (op)
{
case 1:
cout<<"You choose to add a product"<<endl<<endl;
for(int i = (1+c) ; i <= 100 ; i++)
{
if(i>100)
{
cout<<"You exceed the number of your products"<<endl;
}
else
{
cout<<"Please enter the name of the product no."<<i<<endl;
cin>>arr[i].name;
if(arr[i].name==arr[i-1].name)
{
cout<<"The products can't have the same name"<<endl<<endl;
}
else
{
cout<<"Now please enter the quantity of "<<arr[i].name<<endl;
cin>>arr[i].quantity;
cout<<"Now please enter the price of "<<arr[i].name<<endl;
cin>>arr[i].price;
cout<<"Product is successfully added"<<endl<<endl;
c++;
goto begin;
}
}
}
break;
case 2:
cout<<"You choose to view the products"<<endl<<endl;
if(c==0)
{
cout<<"You have no products yet"<<endl<<endl;
goto begin;
}
else
{
for(int i =1 ; i <=c ; i++)
{
cout<<"Product no."<<i<<" name is "<<arr[i].name<<endl;
cout<<"Product no."<<i<<" quantity is "<<arr[i].quantity<<endl;
cout<<"Product no."<<i<<" price is "<<arr[i].price;
cout<<endl<<endl;
}
goto begin;
}
break;
case 3:
char y[100];
int z;
cout<<"You choose to sell a product"<<endl<<endl;
cout<<"Please enter the product's name to be sold"<<endl;
cin>>y;
for(int i =1 ; i <=c ; i++)
{
if (y==arr[i].name)
{
cout<<"Your product is found now please enter the quantity to be sold"<<endl;
cin>>z;
if (z>arr[i].quantity)
{
cout<<"The products number shouldn't be less than the sold ones"<<endl<<endl;
}
else
{
cout<<"The ramming quantity is "<<arr[i].quantity-z<<endl<<endl;
arr[i].quantity=arr[i].quantity-z;
cout<<"Total selling price is "<<arr[i].price*z<<endl<<endl;
}
}
else
cout<<"The product is not found"<<endl<<endl;
}
goto begin;
break;
case 4:
cout<<"You choose to check the empty space"<<endl<<endl;
cout<<"You still have "<<100-c<<" empty space"<<endl<<endl;
goto begin;
break;
case 5:
cout<<"You choose to Terminate the program"<<endl<<endl;
exit(0);
}
return 0;
}
if (strncmp (y,arr[i].name,100) == 0)
{
cout<<"Your product is found now please enter the quantity to be sold"<<endl;
cin>>z;
if (z>arr[i].quantity)
{
cout<<"The products number shouldn't be less than the sold ones"<<endl<<endl;
}
else
{
cout<<"The ramming quantity is "<<arr[i].quantity-z<<endl<<endl;
arr[i].quantity=arr[i].quantity-z;
cout<<"Total selling price is "<<arr[i].price*z<<endl<<endl;
}
}
else
cout<<"The product is not found"<<endl<<endl;
}
gives this error http://s30.postimg.org/69uw331oh/Untitled.png
should argument 2 be a constant ?
i just want it to keep comparing and if it false it shows that it isn't found one time only