(urgent) Two warning relating Initialization.

Hello Guys, I have been working on this problem, it has taken more than 2 days but I still couldn't figure out how can I fix this program. The problem is that When I run the program, it runs fine, but The Calculation or The Billing when you go in the Customer section gives out Zero. I believe that the problem is in " int c[16} "area As I cannot figure out how to pass it properly through the program.

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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
    #include <iostream>
    #include<fstream>
    #include <stdlib.h>
    using namespace std;
    void get_data();
    void show_data();
    void select_Items();
    void calculate();
    class product // class for product
    {
        int pno;
        char name[50];
        float price;
    public:
        struct menuitem  //definition of a struct to store data about resturant
    {
        string menu_list;
        double price;
    };

    menuitem menu[16]; //Instance of a struct to store data of 15 resturant items
    void get_data(); //prototype of a function to loads data about the items in struct
    void show_data(); // prototype of a function show the loaded data
    void select_Items(); //prototype of a function  to select the items
    void calculate(); //prototype of a function to calculate the bill
    int c[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

        void create_product() // function to create a product
        {
            cout << "\n Enter The Product No. of The Product ";
            cin >> pno;
            cout << "\n Enter The Name of The Product ";
            cin >> name;
            cout << "\n Enter The Price of The Product ";
            cin >> price;

        }

        void show_product() // function to display product
        {
            cout << "\n The Product No. of the product : " << pno;
            cout << "\n The Name of The Product : ";
            puts(name);
            cout << " Price of the product : " << price;

        }

        int retpno() // functions for returning values of stored in protected variables
        {
            return pno;
        }

        float retprice()
        {
            return price;
        }

        char* retname()
        {
            return name;
        }


    };

    fstream fp; // variable for file stream
    product pr; // variable of product type

    void write_product() // function for writing product to file
    {
        fp.open("meal.txt", ios::out | ios::app); // filed open for output to file with appending
        pr.create_product();
        fp.write((char*)&pr, sizeof(product)); //type casting class product variable pr into character type and storing in file
        fp.close();
        cout << "\n\n The Product Has Been Created ";

    }

    void display_all() // fucntion for displaying record from file
    {

        cout << "\n\n\n\t\t+++Displaying all record+++\n\n";
        fp.open("meal.txt", ios::in);
        while (fp.read((char*)&pr, sizeof(product)))
        {
            pr.show_product();
            cout << "\n\n====================================\n";

        }
        fp.close();

    }

    void display_sp(int n) // function for displaying specific record from file
    {
        int flag = 0;
        fp.open("meal.txt", ios::in);
        while (fp.read((char*)&pr, sizeof(product)))
        {
            if (pr.retpno() == n) // using if condition to find record by matching with input
            {

                pr.show_product(); // displaying product record if found
                flag = 1;
            }
        }
        fp.close();
        if (flag == 0)
            cout << "\n\nNo record with this number found";

    }

    void modify_product() // function to modify product by overwriting it
    {
        int no, found = 0;

        cout << "\n\n\tTo Modify ";
        cout << "\n\n\tPlease Enter The Product No. of The Product";
        cin >> no;
        fp.open("meal.txt", ios::in | ios::out);
        while (fp.read((char*)&pr, sizeof(product)) && found == 0) // search for record until whole file is read and until found flag is 0
        {
            if (pr.retpno() == no)
            {
                pr.show_product();
                cout << "\nPlease Enter The New Details of Product" << endl;
                pr.create_product();
                int pos = -1 * static_cast<int>(sizeof(pr));  // type casting position of product record and overwriting new product at its place
                fp.seekp(pos, ios::cur);
                fp.write((char*)&pr, sizeof(product));
                cout << "\n\n\t Record Updated";
                found = 1;
            }
        }
        fp.close();
        if (found == 0)
            cout << "\n\n Record Not Found ";

    }
    void delete_product() //function for deleting record from file
    {
        int no;

        cout << "\n\n\n\tDelete Record";
        cout << "\n\nPlease Enter The product no. of The Product You Want To Delete";
        cin >> no;
        fp.open("meal.txt", ios::in | ios::out); // placing all record except the one requested by user in temp file
        fstream fp2;
        fp2.open("cover.txt", ios::out);
        fp.seekg(0, ios::beg);
        while (fp.read((char*)&pr, sizeof(product)))
        {
            if (pr.retpno() != no)
            {
                fp2.write((char*)&pr, sizeof(product));
            }
        }
        fp2.close();
        fp.close();
        remove("meal.txt"); // deleting current file
        rename("cover.txt", "meal.txt"); // renaming temp file to current file name
        cout << "\n\n\tRecord Deleted ..";

    }
    void admin_menu() //  function For Admin to use
    {

        char ch2;
        cout << "\n\n\tADMIN MENU";
        cout << "\n\n\t1.CREATE PRODUCT";
        cout << "\n\n\t2.DISPLAY ALL PRODUCTS";
        cout << "\n\n\t3.QUERY ";
        cout << "\n\n\t4.MODIFY PRODUCT";
        cout << "\n\n\t5.DELETE PRODUCT";
        cout << "\n\n\t6.BACK TO MAIN MENU";
        cout << "\n\n\tPlease Enter Your Choice (1-6) ";
        cin >> ch2;
        switch (ch2) // using switch case statements for selection
        {
        case '1':

            write_product();
            break;
        case '2':
            display_all();
            break;
        case '3':
            int num;

            cout << "\n\n\tPlease Enter The Product No. ";
            cin >> num;
            display_sp(num);
            break;
        case '4':
                modify_product();
                break;
        case '5':
            delete_product();
            break;


        case '6':
            break;
        default:
            cout << "\a";
            admin_menu();
        }
    }

   
Last edited on
Continue 208
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
 int main()
    {

    char a;

    do
    {

    cout << "\n\tMAIN MENU";
    cout << "\n\t1. CUSTOMER";
    cout << "\n\t2. ADMIN";
    cout << "\n\t3. EXIT";
    cout << "\n\tPlease Select Your Option (1-3) ";
    cin >> a;
    switch (a)
    {
    case '1':

    get_data();  //Calling of a function to get load the data
    show_data(); //calling of a function to display data
    select_Items(); //calling of a function select the items from the menu
    calculate(); //calling of a function to calculate the bill

        break;
    case '2':
        admin_menu();
        break;
    case '3':
       exit(0);
    default:
        cout << "\a";
    }
    } while (a != '3');
    }


    void get_data()  // function to get load the data
    {   product::menuitem menu[16];
        menu[1].menu_list = "Biryani    ";
        menu[1].price = 200;
        menu[2].menu_list = "Kababs     ";
        menu[2].price = 100;
        menu[3].menu_list = "Soft Drinks";
        menu[3].price = 100;
        menu[4].menu_list = "Pizza      ";
        menu[4].price = 500;
        menu[5].menu_list = "Zinger Burger";
        menu[5].price = 300;
        menu[6].menu_list = "Beef Pulao";
        menu[6].price = 400;
        menu[7].menu_list = "Cake       ";
        menu[7].price = 500;
        menu[8].menu_list = "Russian Salad";
        menu[8].price = 200;
        menu[9].menu_list = "Pasta      ";
        menu[9].price = 200;
        menu[10].menu_list = "Ice Cream";
        menu[10].price = 50;
        menu[11].menu_list = "Tea       ";
        menu[11].price = 40;
        menu[12].menu_list = "Snacks      ";
        menu[12].price = 50;
        menu[13].menu_list = "Chicken Nuggets";
        menu[13].price = 450;
        menu[14].menu_list = "French Fries";
        menu[14].price = 100;
        menu[15].menu_list = "Coffee       ";
        menu[15].price = 50;
    }
    void show_data() // function to display menu
    {   product::menuitem menu[16];
        cout << "\t\t\t================================="<<endl;
        cout << "\t\t\tFoods offered by the Mess are : " << endl;
        cout << "\t\t\t================================="<<endl;
        cout << "\t" << 1 << "\t" << menu[1].menu_list <<"Biryani    "<< "\t\t\t" << "Rs." << "\t" << menu[1].price << endl;
        cout << "\t" << 2 << "\t" << menu[2].menu_list << "Kababs     "<< "\t\t\t" << "Rs." << "\t" << menu[2].price << endl;
        cout << "\t" << 3 << "\t" << menu[3].menu_list << "Soft Drinks"<<"\t\t\t" << "Rs." << "\t" << menu[3].price << endl;
        cout << "\t" << 4 << "\t" << menu[4].menu_list << "Pizza      "<<"\t\t\t" << "Rs." << "\t" << menu[4].price << endl;
        cout << "\t" << 5 << "\t" << menu[5].menu_list << "Zinger Burger"<<"\t\t\t" << "Rs." << "\t" << menu[5].price << endl;
        cout << "\t" << 6 << "\t" << menu[6].menu_list << "Beef Pulao"<<"\t\t\t" << "Rs." << "\t" << menu[6].price << endl;
        cout << "\t" << 7 << "\t" << menu[7].menu_list << "Cake       "<<"\t\t\t" << "Rs." << "\t" << menu[7].price << endl;
        cout << "\t" << 8 << "\t" << menu[8].menu_list << "Russian Salad"<<"\t\t\t" << "Rs." << "\t" << menu[8].price << endl;
        cout << "\t" << 9 << "\t" << menu[9].menu_list << "Pasta      "<< "\t\t\t" << "Rs." << "\t" << menu[9].price << endl;
        cout << "\t" << 10 << "\t" << menu[10].menu_list <<  "Ice Cream"<<"\t\t\t" << "Rs." << "\t" << menu[10].price << endl;
        cout << "\t" << 11 << "\t" << menu[11].menu_list << "Tea       "<<"\t\t\t" << "Rs." << "\t" << menu[11].price << endl;
        cout << "\t" << 12 << "\t" << menu[12].menu_list << "Snacks      "<<"\t\t\t" << "Rs." << "\t" << menu[12].price << endl;
        cout << "\t" << 13 << "\t" << menu[13].menu_list <<"Chicken Nuggets"<< "\t\t\t" << "Rs." << "\t" << menu[13].price << endl;
        cout << "\t" << 14 << "\t" << menu[14].menu_list <<"French Fries"<< "\t\t\t" << "Rs." << "\t" << menu[14].price << endl;
        cout << "\t" << 15 << "\t" << menu[15].menu_list <<"Coffee       "<< "\t\t\t" << "Rs." << "\t" << menu[15].price << endl;
    }
    

continues
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
137
void select_Items() //function select the items from the menu
    {   product::menuitem menu[16];
        int c[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        int choice, quantity;
        char u;
        do
        {
            cout << endl;
            cout << "Enter your choice : ";
            cin >> choice; //takes the choice from the user to select the item
            cout << "Enter the Quantity : ";
            cin >> quantity;
            switch (choice)
            {
            case 1:
            {
                c[1] = c[1] + quantity;
                cout << "You have Selected :" << menu[1].menu_list << endl;
                break;
            }
            case 2:
            {
                c[2] = c[2] + quantity;
                cout << "You have Selected :" << menu[2].menu_list << endl;
                break;
            }
            case 3:
            {
                c[3] = c[3] + quantity;
                cout << "You have Selected :" << menu[3].menu_list << endl;
                break;
            }
            case 4:
            {
                c[4] = c[4] + quantity;
                cout << "You have Selected :" << menu[4].menu_list << endl;
                break;
            }
            case 5:
            {
                c[5] = c[5] + quantity;
                cout << "You have Selected :" << menu[5].menu_list << endl;
                break;
            }
            case 6:
            {
                c[6] = c[6] + quantity;
                cout << "You have Selected :" << menu[6].menu_list << endl;
                break;
            }
            case 7:
            {
                c[7] = c[7] + quantity;
                cout << "You have Selected :" << menu[7].menu_list << endl;
                break;
            }
            case 8:
            {
                c[8] = c[8] + quantity;
                cout << "You have Selected :" << menu[8].menu_list << endl;
                break;
            }
            case 9:
            {
                c[9] = c[9] + quantity;
                cout << "You have Selected :" << menu[9].menu_list << endl;
                break;
            }
            case 10:
            {
                c[10] = c[10] + quantity;
                cout << "You have Selected :" << menu[10].menu_list << endl;
                break;
            }
            case 11:
            {
                c[11] = c[11] + quantity;
                cout << "You have Selected :" << menu[11].menu_list << endl;
                break;
            }
            case 12:
            {
                c[12] = c[12] + quantity;
                cout << "You have Selected :" << menu[12].menu_list << endl;
                break;
            }
            case 13:
            {
                c[13] = c[13] + quantity;
                cout << "You have Selected :" << menu[13].menu_list << endl;
                break;
            }
            case 14:
            {
                c[14] = c[14] + quantity;
                cout << "You have Selected :" << menu[14].menu_list << endl;
                break;
            }
            case 15:
            {
                c[15] = c[15] + quantity;
                cout << "You have Selected :" << menu[15].menu_list << endl;
                break;
            }
            default:
                cout << "Invalid Input" << endl;
           2 }
            cout << "Do you want to select more items ? (y/n)";
            cin >> u;
        } while (u != 'n');
        cout << endl;
    }
    void calculate()
    {   int c[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        product::menuitem menu[16];
        double total = 0, tax, Total_Amount;
        cout << "\t\t\t================================="<<endl;
        cout << "\t\t\t      Welcome to our Mess" << endl;
        cout << "\t\t\t================================="<<endl;
        cout << endl;
        cout << "\tQuantity \t Meal Items \t\t\t Price"<<endl;
        for (int i = 1; i < 16; i++)
        {
            if (c[i] > 0)
            {
                cout<<"\t" << c[i] << "\t\t" << menu[i].menu_list << "\t\t\t" << "Rs." << menu[i].price << endl;
                total = total + (menu[i].price*c[i]);
            }
        }
        tax = total * 0.17;  //calculate the tax on the total price
        Total_Amount = total + tax;  //calculate the price after adding tax
        cout << endl;
        cout << "\tTax : " << "\t\t\t\t\tRs." << tax << endl;
        cout << "\t_____________________________________________________" << endl;
        cout << "\tTotal Amount  :\t\t\t\t\t " << Total_Amount << endl;
        cout << "\t_____________________________________________________" << endl;
    }
¿why almost all your function have the form void foo();, don't receiving parameters nor returning a value?
¿how do you expect to comunicate between the functions?

¿what's a `product'?
¿what's a `menuitem'?
¿why does a `product' have `menuitem'?

not going to bother reading 400 lines of code, reduce your example.
also, learn about loops.


fp.write((char*)&pr, sizeof(product));
`product' is not POD, that would not work as intended
You are right, I need to change some of the functions' forms. I didn't know that to communicate between the functions you need int(so you could return the value) instead of void. Thank you for telling me that.
The product is for the admin to enter any new Item.
menuItem is a list of items on a menu.

I tried to make struct outside class but it wasn't working. When I added struct in the class, I had to use product::menuitem, so the program can run. Am I doing it wrong?

can you tell me about the last portion?

The Main Problem that these two warnings are coming from Line 26 int c[16]={.....}

The two warning are
warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11|
warning: extended initializer lists only available with -std=c++11 or -std=gnu++11|

As you can see at void get_data(changed into int get_data with return 0), there are multiple values that should be returned, so it can be used in later functions, How can I return multiple values?
Last edited on
Hello ColBosky,

I can not answer everything until I have a chance to put the code together in 1 file.


The two warning are
warning: non-static data member initializes only available with -std=c++11 or -std=gnu++11|
warning: extended initializer lists only available with -std=c++11 or -std=gnu++11|


This tells me that you are trying to use syntax available from C++11 on, but your IDE/compiler is using pre-2011 standards.

You may be able to make a change in the IDE to update to the C++11 standards or you may have to update the IDE/compiler. Since you did not mention what you are using, IDE/compiler, and the version number if you know it. It is hard to say what you will need to do.

As a start to your 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
#include <iostream>
#include<fstream>
#include <stdlib.h>

using namespace std;

void get_data();
void show_data();
void select_Items();
void calculate();

class product // class for product
{
    int pno;
    char name[50];
    float price;

    public:
    struct menuitem  //definition of a struct to store data about resturant
    {
        string menu_list;
        double price;
    };
};  // <--- Was missing.

menuitem menu[16]; //Instance of a struct to store data of 15 resturant items. "menuitem'\" not associated with the class or the struct.
int c[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    
void get_data(); //prototype of a function to loads data about the items in struct
void show_data(); // prototype of a function show the loaded data
void select_Items(); //prototype of a function  to select the items
void calculate(); //prototype of a function to calculate the bill

void create_product() // function to create a product
{
    cout << "\n Enter The Product No. of The Product ";
    cin >> pno;    // <--- Is defined as a private variable of the class. Only accessible through a public function of the class.

    cout << "\n Enter The Name of The Product ";
    cin >> name;   // <--- Is defined as a private variable of the class. Only accessible through a public function of the class.

    cout << "\n Enter The Price of The Product ";
    cin >> price;  // <--- Is defined as a private variable of the class. Only accessible through a public function of the class.

    // <--- All 3 variables need an object of the class and public "set" functions to access the variables.
}

void show_product() // function to display product
{
    cout << "\n The Product No. of the product : " << pno;

    cout << "\n The Name of The Product : ";

    puts(name);

    cout << " Price of the product : " << price;
}

2 things I want you to see here is: some blank lines makes the code easier to read and proper indenting also helps.

First off in the class the class is private by default, so your variables are private and only a public member function can access these variables. I have not gone far enough, but unless you need char name[50]; a :std::string" would be better.

Next try to avoid global variables. Any line of code that follows these definitions can change them and then it is hard to track down where the unwanted change was made. It is better to define these variables in "main" and pass them to the functions that need them. This gives you better control over what can change the variables.

Line 26: You are trying to define a variable without a type. Just because the class is defined before line 26 does not mean that you have access to the variables or functions until you define an object of the class.

In the 2 functions neither has access to the variables or are you thinking of these functions as member functions of the class which would have access to the variables, but you did not set them up as member functions of the class.

Things should work out better if you set up the functions as member functions of the class.

Andy
Thank you so much for your reply.
Yes, I am using CodeBlocks' latest version for window 8.1. It was a group project, therefore, group fellow used visual studio to put the menu and billing. I always try to compact the program by using loops, it becomes neat and saves time.

https://repl.it/@HumzaUddin1/HumzaUddinGroupProject#main.cpp
You can check it when you have the time.

Is string is better than char?

Using global variables. I didn't know it can cause problems. Great advice noted. I will surely avoid global variables.

I used integer data type int c at line 26, Isn't it define? Can you explain this line

Just because the class is defined before line 26 does not mean that you have access to the variables or functions until you define an object of the class.


I don't know how to link functions together, I searched a lot. I probably wasn't search in the right place, because it was giving me the pointers and reference by value. By linking I mean, Using function 1 data in function 2, and using both these functions in function 3 or main function.

I will try to set up functions as member functions of the class for now on.

Again Thank you so much Handy Andy.

Humza
Hello ColBosky,

Sorry for the confusion. After i posted I loaded your code up and found the closing } of the class. I am just use to writing the function outside the class and forget sometimes that some people put the functions inside the class.

DO NOT edit your original post. Changing the code just confuses people when they can see nothing wrong with the code. Put your changes in a reply. Now line 19 is now wrong and line 64 is the closing } of the class making all the functions part of the class.

Usually a "std::string" is better, but using the "fp.write()" to write the whole class to the file you are probably better using the character array because it is a fixed size. Next problem you may find is that the ".write()" may have a problem writing parts of the class to the file. I did not get that far to see what it actually does.

I used integer data type int c at line 26, Isn't it define?
int c[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };. This is a lot of work you should just be able to do int c[16] = {}; unless the compiler complains about the {}s and gives you a warning.

I do not use Code::Blocks that often, but I did find under the menu choice "Settings" look for "Source Formatter". In the window that shows up choose the tab "Style" if it is not already there. All the names that show up are different styles for using the {} and indenting. If you are not familiar with them check each one out. Personally I prefer the "Allman" style because it is easy to read. From what I see most of your code uses the "Allman" style, but I do not know what you did with Code::Blocks and what was done with VS.

Under Settings -> Compiler the window that opens starts with General and there is a box with many choices. The one that is checked, for me, is "Have g++ follow the C++11 ISO C++ language standard [-std=c++11] and it is the second choice. There are others, but C++11 should be the minimum standard.

I don't know how to link functions together
. Linking functions has to do with the returned value of the function and the parameters, i.e., variables sent to the function. A function can have many parameters, but can only return 1 thing. Usually the return of a function is a single variable, but it can return something that holds more information. See http://www.cplusplus.com/doc/tutorial/functions/ Once you understand how functions work it is not that hard.

Also http://www.cplusplus.com/doc/tutorial/namespaces/ is worth reading especially the part about "Namespaces".

Andy
Topic archived. No new replies allowed.