huge program

closed account (1vf9z8AR)
Incomplete and wrong output.(Delete loading from main and then run if you find it irritating)

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
 #include<iostream>
#include<chrono>
#include<thread>
#include<string.h>
#include<stdlib.h>
#include<fstream>
using namespace std;
class user
{
private:
   string name;
   string pass;
public:
        void getdata()
        {
                int flag=0;
                do
                {
                cout<<"Enter username: ";std::getline(std::cin,name);
                cout<<endl;
                cout<<"Enter password: ";std::getline(std::cin,pass);
                cout<<endl;
                 if((name=="myname")&&(pass=="tiger"))
                        {
                                cout<<"\n\nLogin successful"<<endl;
                                flag=1;
                        }
                else
                        cout<<"\n\nInvalid username or password\n\nHint:Your username and password is myname and tiger"<<endl<<endl;
        }while(flag==0);
        }
};
void loading()
{
               for(int i=0;i<5;i++)
        {
                cout<<"\t\t\tLoading 10%"<<endl;
                std::this_thread::sleep_for(std::chrono::seconds(1));
                cout<<"\t\t\tLoading 30%"<<endl;
                std::this_thread::sleep_for(std::chrono::seconds(1));
                cout<<"\t\t\tLoading 60%"<<endl;
                std::this_thread::sleep_for(std::chrono::seconds(1));
                cout<<"\t\t\tLoading 90%"<<endl;
                std::this_thread::sleep_for(std::chrono::seconds(1));
                cout<<"\t\t\tLoading 100%"<<endl;
                std::this_thread::sleep_for(std::chrono::seconds(1));
                break;
        }
}
float category(char items[80],int &no,float &price)
{
        ofstream w1;
w1.open("file.dat");
        int option,n;
        char ch,choice;
        do
        {
        cout<<"Enter 1 for Fruits and Vegetables\n2 for Beverages\n3 for Household needs"<<endl;
        cin>>option;
        ////////////////////////////
        if(option==1)
        {
                char fv[80][80]={"","onion","potato","banana"};
                float fvp[5]={0,50,15,7};
                cout<<"Enter 1  for onion(Rs 50 per kg)\n2 for potato(Rs 15 per kg)\n3 for banana(Rs 7 per unit)";cin>>choice;
                cout<<endl;
                items=fv[choice];
                cout<<"Enter number required";cin>>n;
                cout<<endl;
                no+=n;
                price+=(n*fvp[choice]);
                w1<<items<<": "<<n<<"\n";
        }
               if(option==2)
        {
                char bv[80][80]={"","Tea","Coffee","Juice"};
                float bvp[5]={0,160,400,175};
                cout<<"Enter 1 for tea(Rs 160 per 500 gm)\n2 for coffee(Rs 400 per 500 gm)\n3for juice(Rs 175 per 500ml)";cin>>choice;
                cout<<endl;
                items=bv[choice];
                cout<<"Enter number required";cin>>n;
                cout<<endl;
                no+=n;
               price=n*bvp[choice];
               w1<<items<<":"<<n<<"\n";
        }
              if(option==3)
        {
                char hn[80][80]={"","Detergent","Dishwashers","Air freshener"};
                float hnp[5]={0,160,105,43};
                cout<<"Enter 1 for detergent(Rs 160 per 1.5 kg)\n2 for dishwasher(Rs 105 per 200 gm)\n3for juice(Rs 43 per 50gm)";cin>>choice;
                cout<<endl;
                items=hn[choice];
                cout<<"Enter number required";cin>>n;
                cout<<endl;
                no+=n;
               price=n*hnp[choice];
               w1<<items<<":"<<n<<"\n";
        }
        cout<<"Do you want to continue?(y/n)";cin>>ch;
        cout<<endl;
        }while(ch=='y');
        w1.close();
        return 0;
}
class cart
{
private:
        char items[80];
         int no=0;
        float price=0;
public:
        void getdata()
        {
               category(items,no,price);
               ofstream w1;
               w1.open("file.dat");
               w1<<"Total price="<<price<<"\n";
               w1<<"Total items="<<no<<"\n";
                w1.close();
        }
        void outdata()
        {
                cout<<"\n\nProceeding to checkout"<<endl;
        }
};
int main()
{
        user s1;
        cout<<"###########################################################################################################################"<<endl<<endl;
        cout<<"                                        Welcome To 100% Supermarket                                             "<<endl;
        cout<<"###########################################################################################################################"<<endl<<endl;
        cout<<"Enter your username and password"<<endl<<endl;
        s1.getdata();
        loading();
        system("cls");
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        cart c1;
        c1.getdata();
        c1.outdata();
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        string line;
        ifstream r1;
        r1.open("file.dat");
        cout<<"Your shopping details are as follows:--"<<endl;
        while(getline(r1,line))
                cout<<line<<endl;
        r1.close();
        return 0;
}
I don't see a question.
closed account (1vf9z8AR)
what i am saying is that i write to the file but when read the same file the output is wrong and incomplete
Hello suyashsing234,

When I compiled the program here I received five warnings about the use of "choice" as a subscript. Since the subscript need a variable of "unsigned int" or "int" and "choice" is defined as a "char" part of your problem could come from this as the integral number needed for the subscript is returning a much higher number s a "char" than it would as an "int".

When you enter a "1" into a "char" it is stored as decimal 49 not the 1 that you entered. This affects how the array is accessed, so if element 49 of the array has not received any information yet and the array was not initialized when it was defined the results are unknown and unpredictable.

That is what I see for now until I can load the program in my IDE and see what is happening.

Hope that helps,

Andy
closed account (1vf9z8AR)
i found something like that on the internet but cant figure a workaround. :(
How about "don't use char for choice"?
Hello suyashsing234,

As helios said changing "char choice" to "int choice" made a big difference.

I believe that there are still problems with the program. Still working on that.

Hope that helps,

Andy
I’ve temporary removed your function “loading()” and the statement “system("cls");” because they made the code harder to read and to understand while running. If you fancy them, just restore them into your code. The following is a working version, but I have a question, if I may: since you use std::strings inside one of your class, why do you go back to C-style char arrays inside the other? It seems they can only make your life harder, for what I can see. Whenever, that’s up to 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
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
#include <chrono>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <string>

class user
{
private:
    std::string name;
    std::string pass;
public:
    void getdata()
    {
        int flag = 0;
        do {
            std::cout << "Enter username: "; std::getline(std::cin, name);
            std::cout << "\nEnter password: "; std::getline(std::cin, pass);
            if(name == "myname" && pass == "tiger")
            {
                std::cout << "\n\nLogin successful\n";
                flag = 1;
            }
            else {
                std::cout << "\n\nInvalid username or password\n\nHint:Your "
                             "username and password is myname and tiger\n\n";
            }
        } while(flag == 0);
    }
};

class cart
{
private:
    char items[80] {};
    int no = 0;
    double price = 0;
public:
    void getdata()
    {
        category();
        std::ofstream w1("file.dat", std::ios_base::app);
        w1 << "Total price = " << price << '\n';
        w1 << "Total items = " << no << '\n';
        w1.close();
    }

    void outdata() { std::cout << "\n\nProceeding to checkout\n"; }

    double category()
    {
        std::ofstream w1("file.dat");
        char ch {};
        do {
            std::cout << "\nEnter:\n1) for Fruits and Vegetables\n2) for Beverages"
                         "\n3) for Household needs\n>>> ";
            int option {};
            std::cin >> option;
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            ////////////////////////////
            switch(option) {
            case 1:
            {
                char fv[80][80] = { "onion", "potato", "banana"};
                double fvp[5] = { 50, 15, 7 };
                std::cout << "\nEnter:\n1) for onion (Rs 50 per kg)\n2) for "
                             "potato (Rs 15 per kg)\n3) for bananas (Rs 7 per "
                             "unit)\n>>> ";
                int choice {};
                std::cin >> choice;
                std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
                std::strncpy(items, fv[choice-1], 80);
                std::cout << "Enter number required: ";
                int n {};
                std::cin >> n;
                std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
                no += n;
                price += n * fvp[choice-1];
                w1 << items << ": " << n << '\n';
            }
                break;
            case 2:
            {
                char bv[80][80] = { "Tea", "Coffee", "Juice" };
                double bvp[5] = { 160, 400, 175 };
                std::cout << "\nEnter:\n1) for tea (Rs 160 per 500 gm)\n2) for "
                             "coffee (Rs 400 per 500 gm)\n3) for juice (Rs "
                             "175 per 500ml)\n>>> ";
                int choice {};
                std::cin >> choice;
                std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
                std::strncpy(items, bv[choice-1], 80);
                std::cout << "Enter number required: ";
                int n {};
                std::cin >> n;
                std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
                no += n;
                price = n * bvp[choice-1];
                w1 << items << ": " << n << '\n';
            }
                break;
            case 3:
            {
                char hn[80][80] = { "Detergent", "Dishwashers", "Air freshener" };
                double hnp[5] = {160, 105, 43 };
                std::cout << "\nEnter:\n1) for detergent (Rs 160 per 1.5 kg)"
                             "\n2) for dishwasher (Rs 105 per 200 gm)\n3) for "
                             "juice (Rs 43 per 50gm)\n>>> ";
                int choice {};
                std::cin >> choice;
                std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
                std::strncpy(items, hn[choice-1], 80);
                std::cout << "Enter number required: ";
                int n {};
                std::cin >> n;
                std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
                no += n;
                price = n * hnp[choice-1];
                w1 << items << ": " << n << '\n';
            }
                break;
            }
            std::cout << "\nDo you want to continue (y/n)? ";
            std::cin >> ch;
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        } while(ch == 'y');
        w1.close();
        return 0;
    }
};

void waitForEnter();

int main()
{
    user s1;
    std::cout <<   "############################################################"
              << "\n##             Welcome To 100% Supermarket                ##\n"
              <<   "############################################################"
              << "\nEnter your username and password\n\n";
    s1.getdata();

    cart c1;
    c1.getdata();
    c1.outdata();

    std::string line;
    std::ifstream r1("file.dat");
    std::cout << "Your shopping details are as follows:--\n";
    while(getline(r1,line)) { std::cout << line << '\n'; }
    r1.close();
    waitForEnter();
    return 0;
}

void waitForEnter()
{
    std::cout << "\nPress ENTER to continue...\n";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}

closed account (1vf9z8AR)
ok i use string array now
closed account (1vf9z8AR)
Here is my changed code but now only the net price and total number of items are getting displayed instead of all information.
The output should contain all the information of the purchase before showing the net price and total number of items.

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
#include<iostream>
#include<chrono>
#include<thread>
#include<string.h>
#include<stdlib.h>
#include<fstream>
using namespace std;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class user
{
private:
   string name;
   string pass;
public:
        void getdata()
        {
                int flag=0;
                do
                {
                cout<<"Enter username: ";std::getline(std::cin,name);
                cout<<endl;
                cout<<"Enter password: ";std::getline(std::cin,pass);
                cout<<endl;
                 if((name=="myname")&&(pass=="tiger"))
                        {
                                cout<<"\n\nLogin successful"<<endl;
                                flag=1;
                        }
                else
                        cout<<"\n\nInvalid username or password\n\nHint:Your username and password is myname and tiger"<<endl<<endl;
        }while(flag==0);
        }
};
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void loading()
{
               for(int i=0;i<5;i++)
        {
                cout<<"\t\t\tLoading 10%"<<endl;
                std::this_thread::sleep_for(std::chrono::seconds(1));
                cout<<"\t\t\tLoading 30%"<<endl;
                std::this_thread::sleep_for(std::chrono::seconds(1));
                cout<<"\t\t\tLoading 60%"<<endl;
                std::this_thread::sleep_for(std::chrono::seconds(1));
                cout<<"\t\t\tLoading 90%"<<endl;
                std::this_thread::sleep_for(std::chrono::seconds(1));
                cout<<"\t\t\tLoading 100%"<<endl;
                std::this_thread::sleep_for(std::chrono::seconds(1));
                break;
        }
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class cart
{
private:
        string items;
         int no=0;
        float price=0;
public:
        void getdata()
        {
               category();
               ofstream w1;
               w1.open("file.dat");
               w1<<"Total price="<<price<<"\n";
               w1<<"Total items="<<no<<"\n";
                w1.close();
        }
        void outdata()
        {
                cout<<"\n\nProceeding to checkout"<<endl;
        }
        void category()
                {
        ofstream w1;
w1.open("file.dat");
        int option,n,choice;
        char ch;
        do
        {
        cout<<"Enter 1 for Fruits and Vegetables\n2 for Beverages\n3 for Household needs"<<endl;
        cin>>option;
        ////////////////////////////
        if(option==1)
        {
                string fv[5]={"","onion","potato","banana"};
                float fvp[5]={0,50,15,7};
                cout<<"Enter 1  for onion(Rs 50 per kg)\n2 for potato(Rs 15 per kg)\n3 for banana(Rs 7 per unit)";cin>>choice;
                cout<<endl;
                items=fv[choice];
                cout<<"Enter number required";cin>>n;
                cout<<endl;
                no+=n;
                price+=(n*fvp[choice]);
                w1<<items<<": "<<n<<"\n";
        }
               if(option==2)
        {
                string bv[5]={"","Tea","Coffee","Juice"};
                float bvp[5]={0,160,400,175};
                cout<<"Enter 1 for tea(Rs 160 per 500 gm)\n2 for coffee(Rs 400 per 500 gm)\n3for juice(Rs 175 per 500ml)";cin>>choice;
                cout<<endl;
                items=bv[choice];
                cout<<"Enter number required";cin>>n;
                cout<<endl;
                no+=n;
               price=n*bvp[choice];
               w1<<items<<":"<<n<<"\n";
        }
              if(option==3)
        {
                string hn[5]={"","Detergent","Dishwashers","Air freshener"};
                float hnp[5]={0,160,105,43};
                cout<<"Enter 1 for detergent(Rs 160 per 1.5 kg)\n2 for dishwasher(Rs 105 per 200 gm)\n3for juice(Rs 43 per 50gm)";cin>>choice;
                cout<<endl;
                items=hn[choice];
                cout<<"Enter number required";cin>>n;
                cout<<endl;
                no+=n;
               price=n*hnp[choice];
               w1<<items<<":"<<n<<"\n";
        }
        cout<<"Do you want to continue?(y/n)";cin>>ch;
        cout<<endl;
        }while(ch=='y');
        w1.close();
        }
};
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int main()
{
        user s1;
        cout<<"###########################################################################################################################"<<endl<<endl;
        cout<<"                                        Welcome To 100% Supermarket                                             "<<endl;
        cout<<"###########################################################################################################################"<<endl<<endl;
        cout<<"Enter your username and password"<<endl<<endl;
        s1.getdata();
        loading();
        system("cls");
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        cart c1;
        c1.getdata();
        c1.outdata();
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        string line;
        ifstream r1;
        r1.open("file.dat");
        cout<<"Your shopping details are as follows:--"<<endl;
        while(getline(r1,line))
                cout<<line<<endl;
        r1.close();
        return 0;
}
only the net price and total number of items are getting displayed
That issue is due only to how you open your output file.
If you open a file in output with std::ios_base::openmode set to std::ios_base::out , the content of the file is destroyed and overwritten. When you want to preserve the previously written text, you need to set std::ios_base::openmode to std::ios_base::app .

Solving that issue will improve your output, but will leave unresolved a lot of questions about your code logic: I’d rather focus on them.
Topic archived. No new replies allowed.