Oct 9, 2014 at 4:20am UTC
You didn't tell us what the problem is. Compiling error? Output not giving the desired result? etc. etc.
Oct 9, 2014 at 4:32am UTC
I thnk the problem is you're using statc variable in your member of class, in that case all of the object that you create will have a same name, price, and quantity
Oct 9, 2014 at 4:40am UTC
in the 'int list()' function, you 'return main();'
returning main() has to be wrong.
return an integer or char to main(), but not return the main() function.
Oct 9, 2014 at 7:08am UTC
Hyperfine: I'm apologize. output isn't giving the desired result.
LendraDwi: I'll try to remove the static keyword.
jt1: I'll correct it .
Thanks for reply !
Oct 11, 2014 at 9:21am UTC
Hi guys, i made some change on my code, but it's still have problem. First, at the function void add(),after the cin >> name, it become infinity loop.
#include <iostream>
#include <iomanip>
using namespace std;
class item {
private:
char name;
float price;
int quantity;
public:
void set_item(char n, float p, int q);
void set_item_name(char n);
void set_item_price(float p);
void set_item_quantity(int q);
char get_item_name();
float get_item_price();
int get_item_quantity();
};
void item :: set_item_name(char n){
name = n;
}
void item :: set_item_price(float p){
price = p;
}
void item :: set_item_quantity(int q){
quantity = q;
}
char item :: get_item_name(){
return name;
}
float item :: get_item_price(){
return price;
}
int item :: get_item_quantity(){
return quantity;
}
void item :: set_item(char n, float p, int q){
name = n;
price = p;
quantity = q;
}
void add();
void edit();
void search();
void list();
void edit_code(int x);
void edit_name(int x);
void search_code(int x);
void search_name(int x);
int inf = 0;
int s[10000];
int i = 0;
int test = i - 1;
int last_item;
class item;
int main(){
int function;
do{
cout << "1. List\n2. Add\n3. Edit\n4. Search\n";
cout <<"\nEnter :";
cin >> function;
if(function == 1)
list();
else if(function == 2)
add();
else if(function == 3)
edit();
else if(function == 4)
search();
else
cout << "\nERROR!";
} while(inf != 1);
cout << "\n\n";
return 0;
}
void add() {
char name;
float price;
int quantity;
int continue_;
item s[10000];
do{
cout << "\nEnter item name :";
cin >> name;
cout << "\nEnter item price :";
cin >> price;
cout << "\nEnter item quantity :";
cin >> quantity;
s[i].set_item(name, price, quantity);
cout << "\n\nContinue to Add? (Y or N) :";
cin >> continue_;
last_item += 1;
i++;
cout << "\n\n";
}while( continue_ == 'Y' || continue_ == 'y');
}
void edit() {
int x, continue_;
do{
cout << "\nEnter item name or item's code :";
cin >> x;
if( isdigit(x))
edit_code(x);
else
edit_name(x);
cout << "\nContinue edit? (Y or N) :";
cin >> continue_;
cout << "\n\n";
}while( continue_ == 'Y' || continue_ == 'y');
}
void edit_code(int x){
char test;
char n;
float p;
int q;
item s[10000];
cout << "\nEnter N(edit name)/ P(edit price)/ Q(edit quantity) :";
cin >> test;
if( test == 'N'|| test == 'n'){
cout << "\nEnter new name :";
cin >> n;
s[x].set_item_name(n);
}
else if ( test == 'P' || test == 'p'){
cout << "\nEnter new price :";
cin >> p;
s[x].set_item_price(p);
}
else if( test == 'Q' || test == 'q'){
cout << "\nEnter new quantity :";
cin >> q;
s[x].set_item_quantity(q);
}
else
cout << "Error!";
}
void edit_name(int x){
int loop = 0;
char test;
char n;
float p;
int q;
item s[10000];
for( loop ; loop < last_item + 1; loop ++){
if(s[loop].get_item_name() == x)
break;
}
cout << "\nEnter N(edit name)/ P(edit price)/ Q(edit quantity) :";
cin >> test;
if( test == 'N'|| test == 'n'){
cout << "\nEnter new name :";
cin >> n;
s[loop].set_item_name(n);
}
else if ( test == 'P' || test == 'p'){
cout << "\nEnter new price :";
cin >> p;
s[loop].set_item_price(p);
}
else if( test == 'Q' || test == 'q'){
cout << "\nEnter new quantity :";
cin >> q;
s[loop].set_item_quantity(q);
}
else
cout << "\nError!";
}
void search(){
int x;
int continue_;
do{
cout << "\nEnter item name or item's code :";
cin >> x;
if( isdigit(x))
search_code(x);
else
search_name(x);
cout << "\nContinue search? (Y or N) :";
cin >> continue_;
cout << "\n\n";
}while( continue_ != 'Y' || continue_ != 'y');
}
void search_code(int x){
item s[10000];
cout << "\nCode :" << x << "\nName :" << s[x].get_item_name() << "\nPrice :" << s[x].get_item_price() << "\nQuantity :" << s[x].get_item_quantity();
}
void search_name(int x){
int loop = 0;
item s[10000];
for( loop ; loop < last_item + 1; loop ++){
if(s[loop].get_item_name() == x)
break;
}
cout << "\nCode :" << x << "\nName :" << s[x].get_item_name() << "\nPrice :" << s[x].get_item_price() << "\nQuantity :" << s[x].get_item_quantity();
}
void list(){
item s[10000];
int t;
do{
if (test < 0){
cout << "\n\nEmpty!";
}
else if (test >= 0){
cout << "\nCode\tName\tPrice\tQuantity";
for(i = 0; i < last_item+1; i++ )
cout << "\n" << i << "\t" << s[i].get_item_name() << "\t" << s[i].get_item_price() << "\t" << s[i].get_item_quantity();
}
cout << "\n\nPress Q to go menu";
cin >> t;
cout << "\n\n";
}while ( t != 'q' || t != 'Q');
}