a program for products

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

http://s30.postimg.org/50qgy3kht/Untitled.png

the second problem is when i try to view the the products
the names in never printed only symbols

http://s14.postimg.org/shq9ujnip/Untitled1.png


sorry for my bad english
thank you :)

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include<iostream>
#include<cstdlib>
using namespace 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;
}
1
2
3
4
5
6
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.
thanks bro
works perfectly
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


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
26
27
28
29
30
31
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 ?
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
26
27
28
29
30
31
32
33
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;


thanks
Last edited on
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.

Or throw away c-strings and use std::string.
Last edited on
Which error?
for(int i =1 ; i <=c ; i++) This loop looks extremely sketchy. Does arr[c] really exist?
here is the full code

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include<iostream>
#include<cstdlib>
using namespace 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;
}
1
2
3
int c=0;
//...
int i = (1+c)
You start filling your arrays from arr[1] instead of arr[0]

i <= 100In the end you will try to write to arr[100] which does not exist.

corrected the two loops
the first one
was
for(int i = (1+c) ; i <= 100 ; i++)
now
for(int i = c ; i < 100 ;)

and the second loop
was
for(int i =1 ; i <=c ; i++)
now
for(int i =0 ; i <c ; i++)

but still the same problems of availability and entering the same names
Are you sure that you properly rebuild your program?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
You either use strncmp or use std::string. Not both
done
thanks for your responses
Topic archived. No new replies allowed.