A problem + need explaining

Hey guys , I was practicing some of the things that I've learnt " I know there's a better way to do such things but I'm only practicing "

here's the 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
  #include<iostream>
#include<iomanip>
#include<string>
using namespace std;

class store{
public:

	store(int n){
		number = n;
		ptrname = new string[n];
		ptrprice = new double[n];

	}

	void setname(){
		cout << "Enter the name of the products then press enter and enter the second product's item and so on\n";

		for (int i = 0; i < number; i++){
			getline(cin, ptrname[i]);

		}


	}
	void setprice(){
		cout << "Please enter the price for the first item and then press enter and so on\n";

		for (int i = 0; i < number; i++){
			cin >> price;
			ptrprice[i] = price;
		}
	}
	void settotal(){
		for (int i = 0; i < number; i++){
			total += ptrprice[i];
		}
	}
	void getinfo(){
		cout << "Product's name: " << setw(15) << "Product's price:\n";

		for (int i = 0; i < number; i++){
			cout << ptrname[i] << setw(25) << ptrprice[i] << endl;
		}
		cout << "Total = " << total;


	}

	void deleteptr(){
		delete ptrname;
		delete ptrprice;
	}

private:
	double total = 0;
	int number;
	double price;
	string *ptrname;
	double *ptrprice;




};

int main(){

	int x;
	cout << "Welcome to your grocery shopping calculator\n ";
	cout << "PLEASE ENTER: how many items you have bought" << endl;
	cin >> x;

	store cal(x);

	cal.setname();
	cal.setprice();
	cal.settotal();
	cal.getinfo();
	cal.deleteptr();


	cin.get();
	cin.get();
	return 0;


}


in the function " setname " when for example I put 1 , it skips and goes to the second function , so if I put 2 it skips the first one and let me initialize the second one and so on.

the second problem is in deleteptr function , when I try to delete it it just loads and sometimes crashes , how am I suppose to delete it ? " the program runs fine and gives my the output I need but it just loads stuff and in the other compiler it crashes after giving me what I want "
in the function " setname " when for example I put 1 , it skips and goes to the second function , so if I put 2 it skips the first one and let me initialize the second one and so on.


The >> operator leaves the trailing whitespace (in this case, the \n newline character) in the cin buffer. So when you do a getline, it will think there was an empty line input.

If you want to do a getline() after a >>.... you'll need to purge all whitespace from the buffer first. You can do this with the ignore() command:

1
2
3
4
5
6
int x;
string y;

cin >> x;  // <- leaves the newline in the buffer
cin.ignore(1000,'\n');  // <- wipes the contents of the buffer up to the next newline
getline(cin, y);  // <- gets a new line 



the second problem is in deleteptr function , when I try to delete it it just loads and sometimes crashes , how am I suppose to delete it ?


You're using array new (new[]) so you should be using array delete (delete[]). That is... lines 51 and 52 should actually be this:

1
2
		delete[] ptrname;  //<- array delete[]!
		delete[] ptrprice;


That would cause a leak, but I don't think it would normally cause a crash. But I don't see any other issues right now. Try that out and see if it fixes the problem.
Thanks man ^^ , about the second question I don't know how did I forget that :D

the first question:

it worked now , but when I put:

cin.ignore(1000,'\n');

out side of setname function , it solves it for me , but after that wouldn't the cin buffer keeps some chars and screw up the setprice function ?
Last edited on
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
/* IF YOU ARE GOING TO STUDY, YOU HAVE TO MAKE IT CLEANER AS POSSIBLE! */

#include<iostream>
#include<iomanip>
#include<string>
#include<vector> //It's an array, but thousand times better and faster, for all.
using namespace std;
//Identation is a good pratice.

class store
{
    //Always add members first.
    private:
        /*
        I changed some variable names for readability, that's very important,
        and some of "signed" and "unsigned" comparisons.
        */

        double total; //Here, you don't need, but if you want, remove the zero and add the propriety in the constructor
        /*- warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 -*/
        unsigned number; //unsigned-int, you wouldn't use -0 numbers.
        double price;
        vector<string> products_name; //The best pratice, vectors are very good!
        vector<double> products_price; //They are resize-able

    public:
            store(int n) : total(0) //Initializing the variable!
            {
                number = n;
                products_name.resize(n);
                products_price.resize(n);
            }

        void setname()
        {
            cout << "Enter the name of the products:\n\n"; //I've edited for clarifying
            for (unsigned i = 0; i < number; i++)
                {
                    std::cout << "Name " << i+1 <<": "; //Note the +1, because the user will not know that an array starts with 0
                    getline(cin , products_name[i]);
                }
        }
        void setprice()
        {
            cout << "Please enter the price for items:\n"; //I've edited for clarifying
            for (unsigned i = 0; i < number; i++)
                {
                    std::cout <<"$ "; //Only the dollar simbol...
                    cin >> price;
                    if(cin.fail())
                    {
                        //Exceptions are good.
                        throw -1;
                    }
                    cin.sync(); //That was missing. That's the "error-fixer". It just clean the stream
                    products_price[i] = price;
                    std::cout << std::endl;
                }
        }
        void settotal()
        {
            for (unsigned i = 0; i < number; i++)
            {
                total += products_price[i];
            }
        }
        void getinfo(){
            cout << "Product's name: " << setw(15) << "Product's price:\n";

            for (unsigned i = 0; i < number; i++){
                cout << products_name[i] << setw(25) << products_price[i] << endl;
            }
            cout << "Total = " << total;


        }
        //You don't need deleteptr anymore! Vectors have destructors!
};

int main()
{
    try
	{
        unsigned x;
        cout << "Welcome to your grocery shopping calculator\n";
        cout << "PLEASE ENTER: how many items you have bought: ";
        cin >> x;
        if(cin.fail())
        {
            throw -2;
        }
        cin.sync(); //Sync...

        store cal(x);
        cal.setname();
        cal.setprice();
        cal.settotal();
        cal.getinfo();
        cin.get();
        cin.get();
        return 0;
	}
	catch(int except) //not only int, BUT we ARE using int-s, so...
	{
	    if(except == -3)
        {
            std::cout << "You entered an invalid number!";
	    }
        cout << "You entered an invalid number!";
        return except;
	}
}

/*
That program can be better, but I'll need more time ;)
*/
Last edited on
^

thanks man , but I don't think vectors and dynamic arrays are that different since both are dynamic , maybe using vector would be easy , but how about using the built in array ? that one in the "array" header.
They are almost equal, but vectors have too much more methods and are resize-able, a real important propriety. In this case, you can use normal arrays.
Last edited on
std::vector<> is a resizeable sequence; we can insert items into it to increase its size, and remove items from it to reduce its size.

std::array<> is a fixed size array, with the size being a constant known at compile-time. It is a thin wrapper over a C89-style array.

@iQChange


I mean dynamic array I don't think it's that different from vector since you can give the size of the dynamic array at run time just like vectors :D

thank you ^^

@JLBorges

thanks buddy ^^

but we're talking about dynamic array here which it's size could be insert at run time instead of compile time.
> but we're talking about dynamic array here

std::vector<> is the dynamically sized C++ array.
http://www.mochima.com/tutorials/vectors.html
Thanks, all of you.

You guys helped a lot ^^
Topic archived. No new replies allowed.