ctypea program

Pages: 123
Nov 9, 2020 at 1:07pm
This is really, really, yucky code -

I know right if only I didn't have limitations to do this I could've been done asking you guys.. dang.
Nov 9, 2020 at 1:25pm
If I try reamoving 'outStream' I am stuck with this code, with a very complicated
- OFFICIAL RECEIPT -.

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
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
    string productname;
    int subTotal{};
    double quantity = 0, price = 0, qprice{}, flist{};
    double lineTotal{}, priceTotal{}; 
    do{
        lineTotal = quantity * price;
        subTotal += quantity;
        priceTotal += lineTotal;
        cout << "Product name: ";
        cin >> productname;
        if ( productname == "x" || productname == "X"){
        cout << "- OFFICIAL RECEIPT -\n";
        cout << "Qty Product name   Price\n";
        cout<< setprecision(0) << setw(2) << quantity << "  ";
        cout<< left << setw(13) << productname;
        cout<< right << setprecision(4) << setw(6) << price << '\n';
        cout << "Total\n";
        cout <<  subTotal << " item(s)" << "      " << setprecision(2) << "    " << priceTotal;
            break;
                        }
        cout << "Quantity: ";
        cin >> quantity;
        cout << "Price   : ";
        cin >> price;
        } while (1);
    return 0;
}
Nov 9, 2020 at 1:33pm
I've updated my previous code to produce the receipt in the required format:

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
#include <cstdlib>
#include <string>
#include <iostream>
#include <iomanip>

int main()
{
	struct Item {
		int quant {};
		double price {};
	};

	size_t used {};
	char* data {nullptr};

	for (std::string name; (std::cout << "\nProduct name [CR to end]: ") && std::getline(std::cin, name) && !name.empty(); ) {
		Item item;

		std::cout << "Quantity: ";
		std::cin >> item.quant;

		std::cout << "Price: ";
		std::cin >> item.price;

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

		const size_t old {used};

		used += sizeof(Item) + name.size() + 1;
		data = (char*)std::realloc(data, used);
		memcpy(data + old, &item, sizeof(Item));
		memcpy(data + old + sizeof(Item), name.c_str(), name.size() + 1);
	}

	std::cout << '\n';

	int noItems {};
	double totalprice {};

	std::cout << "- OFFICIAL RECEIPT -\n";
	std::cout << std::left << std::setw(5) << "Qty" << std::setw(15) << "Product name" << "Price\n";

	for (char* ptr = data, *endptr = data + used; ptr < endptr; ) {
		const std::string name(ptr + sizeof(Item));
		Item item;

		memcpy(&item, ptr, sizeof(Item));
		std::cout << std::left << std::setw(5) << item.quant << std::setw(15) << name << std::fixed << std::setprecision(2) << item.price * item.quant << '\n';
		noItems += item.quant;
		totalprice += item.price * item.quant;

		ptr += sizeof(Item) + name.size() + 1;
	}

	std::cout << "\nTotal\n" << noItems << " item(s) " << totalprice << '\n';

	std::free(data);
}




Product name [CR to end]: Bread
Quantity: 3
Price: 65.75

Product name [CR to end]: Banana
Quantity: 4
Price: 9.2

Product name [CR to end]: Noodles
Quantity: 5
Price: 6.4

Product name [CR to end]:

- OFFICIAL RECEIPT -
Qty  Product name   Price
3    Bread          197.25
4    Banana         36.80
5    Noodles        32.00

Total
12 item(s) 266.05



PS I've had an idea........
Last edited on Nov 9, 2020 at 1:45pm
Nov 9, 2020 at 1:43pm
I've updated my previous code to produce the receipt in the required format:


Thanks for this, I'm still trying to tweak Handy Andy's code to make it work without using stringstream.. cause it's really close to what I'm tryna reach.. Thank ya'll guys for helping.
Nov 9, 2020 at 1:44pm
So if you can't adopt a stringstream just produce the output as a string.
You can even do away with <iomanip> if you use the string manipulation functions length() etc.

PS Your
1
2
3
4
5
6
do
{

if(...)

}while(1)

is not at all good given the limitations imposed on the rest of it.

And here's your big chance to use getline, clear and ignore. (Enter "French fries" as a product into your current code :)

Nov 9, 2020 at 1:47pm
OK. rather than using ofstream which you can't, you can use std::string. Consider:

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
include <string>
#include <iostream>
#include <iomanip>

int main()
{
	std::string receipt;
	int noItems {};
	double totalprice {};

	for (std::string name; (std::cout << "\nProduct name [CR to end]: ") && std::getline(std::cin, name) && !name.empty(); ) {
		int quant {};
		double price {};

		std::cout << "Quantity: ";
		std::cin >> quant;

		std::cout << "Price: ";
		std::cin >> price;

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

		receipt += std::to_string(quant) + "  " + name + "  " + std::to_string(quant * price) + '\n';
		noItems += quant;
		totalprice += quant * price;
	}

	std::cout << "\n- OFFICIAL RECEIPT -\n";
	std::cout << "Qty  " << "Product name" << "Price\n";
	std::cout << receipt;
	std::cout << "\nTotal\n" << noItems << " item(s) " << totalprice << '\n';
}



Product name [CR to end]: Bread
Quantity: 3
Price: 65.75

Product name [CR to end]: Banana
Quantity: 4
Price: 9.2

Product name [CR to end]: Noodles
Quantity: 5
Price: 6.4

Product name [CR to end]:

- OFFICIAL RECEIPT -
Qty  Product namePrice
3  Bread  197.250000
4  Banana  36.800000
5  Noodles  32.000000

Total
12 item(s) 266.05


The output needs formatting a bit - but the idea seems OK.
Nov 9, 2020 at 2:49pm
OK. rather than using ofstream which you can't, you can use std::string. Consider:


Thnx for this, but program should be like this and etcetc like xX to get receipt..;

https://imgur.com/a/GuOM0Fd
Last edited on Nov 9, 2020 at 2:49pm
Nov 9, 2020 at 3:28pm
OK. As per the image:

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
#include <string>
#include <iostream>
#include <iomanip>

int main()
{
	std::string receipt;
	int noItems {};
	double totalprice {};

	for (std::string name; (std::cout << "Product name  : ") && std::getline(std::cin, name) && !name.empty() && (name != "x" && name != "X"); ) {
		std::string squant, sprice;

		std::cout << "Quantity  : ";
		std::cin >> squant;

		std::cout << "Price     : ";
		std::cin >> sprice;

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

		const int quant = std::stoi(squant);

		noItems += quant;
		totalprice +=  quant * std::stod(sprice);

		squant += std::string(5 - squant.size(), ' ');
		name += std::string(15 - name.size(), ' ');

		receipt += squant + name + sprice + '\n';
	}

	std::cout << "- OFFICIAL RECEIPT -\n";
	std::cout << "Qty  " << std::left << std::setw(15) << "Product name" << "Price\n";
	std::cout << receipt;
	std::cout << "Total\n" << std::left << std::setw(5) << noItems << std::setw(15) << "item(s)" << totalprice << '\n';
}



Product name  : Bread
Quantity  : 3
Price     : 65.75
Product name  : Banana
Quantity  : 4
Price     : 9.20
Product name  : Noodles
Quantity  : 5
Price     : 6.40
Product name  : x
- OFFICIAL RECEIPT -
Qty  Product name   Price
3    Bread          65.75
4    Banana         9.20
5    Noodles        6.40
Total
12   item(s)        266.05


Note no input checking for bad/wrong data.
Last edited on Nov 9, 2020 at 3:29pm
Nov 9, 2020 at 3:39pm
OK. As per the image:

I tried submitting with std::cout in it and professor/teacher ain't having it... dang.. but this one is the most perf one as per image, Thank you so much!
Nov 9, 2020 at 3:50pm
Ok. With the std:: removed - maybe he wants you to use using..

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

int main()
{
	string receipt;
	int noItems {};
	double totalprice {};

	for (string name; (cout << "Product name  : ") && getline(cin, name) && !name.empty() && (name != "x" && name != "X"); ) {
		string squant, sprice;

		cout << "Quantity  : ";
		cin >> squant;

		cout << "Price     : ";
		cin >> sprice;

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

		const int quant = stoi(squant);

		noItems += quant;
		totalprice += quant * stod(sprice);

		squant += string(5 - squant.size(), ' ');
		name += string(15 - name.size(), ' ');

		receipt += squant + name + sprice + '\n';
	}

	cout << "- OFFICIAL RECEIPT -\n";
	cout << "Qty  " << left << setw(15) << "Product name" << "Price\n";
	cout << receipt;
	cout << "Total\n" << left << setw(5) << noItems << setw(15) << "item(s)" << totalprice << '\n';
}


If your teacher still doesn't like this - what are they looking for???????
Nov 10, 2020 at 2:34am
If your teacher still doesn't like this - what are they looking for???????

I tried using your code, he clearly just said I'm trying to use "arrays"? my man ain't messing around.
Nov 10, 2020 at 3:19am
is there like a way to delete 'stringstream outStream' and still get same results?
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
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
    stringstream outStream;
    string productname;
    int subTotal{};
    double quantity = 0, price = 0, flist{};
    double lineTotal{}, priceTotal{};
    cout << fixed << showpoint;
    do{
        cout << "Product name: ";
        cin >> productname;
        if ( productname == "x" || productname == "X"){
            break;
            }
        cout << "Quantity: ";
        cin >> quantity;
        cout << "Price   : ";
        cin >> price;
        lineTotal = quantity * price;
        subTotal += quantity;
        priceTotal += lineTotal;
        outStream
            << setprecision(4) << setw(2) << quantity << "  "
            << left << setw(13) << productname
            << right << setprecision(4) << setw(6) << price << '\n';
    } while (1);
    cout << "- OFFICIAL RECEIPT -\n";
    cout << "Qty Product name   Price\n";
    cout << outStream.str();
    cout << "Total\n";
    cout <<  " " << subTotal << " item(s)" << "      " << setprecision(2) << "    " << priceTotal;
    return 0;
}



Product name: Bread
Quantity: 3
Price  : 65.75
Product name: Banana
Quantity: 4
Price  : 9.20
Product name: Noodles
Quantity: 5
Price  : 6.40
Product name: x
 - OFFICIAL RECEIPT -
Qty Product name   Price
3     Bread         65.75
4     Banana       9.2
5     Noodles      6.4
Total
12 item(s)         266.05
Last edited on Nov 10, 2020 at 3:36am
Nov 10, 2020 at 10:06am
You can't use string or stringstream to accumulate the receipt. You can't use array/vector (or memory used as an array). What's left to hold the receipt? A file? what have you covered in your class - as he's expecting this to be done using existing knowledge?

What about a queue - or is that also classed as an array?
Last edited on Nov 10, 2020 at 10:11am
Nov 10, 2020 at 1:42pm
seeplus,

I think he's letting us try to accomplish it with loops and if/else which is I think impossible and very complicated. By now I'm happy with what ya'll guys gave since it's delivering what was shown, it's just the limitations. Lmao.
Nov 10, 2020 at 1:52pm
The requirements of some 'teacher's never cease to amaze. If there's some particular technique/aspect of C++ he wants used, then he should indicate this!
Nov 10, 2020 at 2:00pm
I know right, we've been struggling. Lol. But I have this Idk if this make any sense at this point.
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
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
    string cInputNames = "", cOutput = "";
    double quantity, price, sum = 0, qprice, sum1 = 0;
    int x = 1;
    x = 1;
    do{
        cout << "Product name: ";
        cin >> cInputNames;
        cOutput+= cInputNames + '\n' + '\t';
        x++;
        if (cInputNames == "x" || cInputNames == "X"){
        cout<<" - OFFICIAL RECEIPT -\n";
        cout<<"Qty Product name   Price\n";
        cout<< quantity << "\t" << cOutput << endl;
        cout<<"Total\n";
        cout<< sum1 << " item(s)         " << sum;
        break;
        }
        cout << "Quantity: ";
        cin >> quantity;
        cout << "Price   : ";
        cin >> price;
        qprice = quantity * price;
        sum += qprice;
        sum1 += quantity;
    } while (1);
}


Product name: Bread
Quantity: 3
Price   : 65.75
Product name: Banana
Quantity: 4
Price   : 9.20
Product name: Noodles
Quantity: 5
Price   : 6.40
Product name: x
 - OFFICIAL RECEIPT -
Qty Product name    Price
5     Bread
      Banana
      Noodles
      x


My only prob is idk how I will display the quantity vertically reading it one by one cause it only reads the last one and, idk how I will place the Price. ps; and it displays "x".
Last edited on Nov 10, 2020 at 2:03pm
Nov 10, 2020 at 6:16pm
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
include <iostream>
#include <string>
using namespace std;

int main() {
	string cInputNames, cOutput, cquantity, cprice;
	double quantity = 0, sum = 0, sum1 = 0;

	do {
		cout << "Product name: ";
		cin >> cInputNames;

		if (cInputNames == "x" || cInputNames == "X") {
			cout << " - OFFICIAL RECEIPT -\n";
			cout << "Qty\tProduct name\tPrice\n";
			cout << cOutput;
			cout << "Total\n";
			cout << sum1 << " item(s)\t\t" << sum;
			break;
		}

		cout << "Quantity: ";
		cin >> cquantity;

		cout << "Price   : ";
		cin >> cprice;

		cOutput += cquantity + '\t' + cInputNames + "\t\t" + cprice + '\n';
		quantity = stod(cquantity);
		sum += quantity * stod(cprice);
		sum1 += quantity;
	} while (1);
}


but this is very similar to my previous version using string.


Product name: Bread
Quantity: 3
Price   : 65.75
Product name: Banana
Quantity: 4
Price   : 9.20
Product name: Noodles
Quantity: 5
Price   : 6.40
Product name: x
 - OFFICIAL RECEIPT -
Qty     Product name    Price
3       Bread           65.75
4       Banana          9.20
5       Noodles         6.40
Total
12 item(s)              266.05

Nov 10, 2020 at 6:28pm
Hello seghrein2300,

I think I have figured this out. The following code has not been completely changed, compiled or tested, but I think the concept should work.

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
#include <iostream>
#include <iomanip>
#include <string>

#include <fstream>

using namespace std;

int main()
{
    string exitCheck;
    string
        productname1,
        productname2,
        productname3;
    double
        quantity1,
        quantity2,
        quantity3;
    double
        price1,
        price2,
        price3;
    int qtyItemsTotal{}, count{};
    //double quantity = 0, price = 0, qprice{}, flist{};  // <--- These variables should no longer be needed.
    double lineTotal{}, priceTotal{};  // <--- Maybe this would be a better name.

    cout << fixed << setprecision(2); // <--- May or may not be needed.

    do
    {
        if (count == 0)
        {
            cout << "Product name: ";
            getline(cin, productname1);

            exitCheck = productname1;
        }
        if (count == 1)
        {
            cout << "Product name: ";
            getline(cin, productname2);

            exitCheck = productname2;
        }
        if (count == 2)
        {
            cout << "Product name: ";
            getline(cin, productname3);

            exitCheck = productname3;
        }
        // <--- Add as many as you want.

        if (exitCheck == "x" || exitCheck == "X")
        {
            cout << "\n - OFFICIAL RECEIPT -\n";
            cout << "Qty Product name   Price\n";

            for (int idx = 0; idx < count; idx++)
            {
                if (idx == 0)
                {
                    cout << quantity1 << "  " << productname1 << price1 * quantity1 << '\n';
                }
                if (idx == 1)
                {
                    cout << quantity2 << "  " << productname2 << price2 * quantity2 << '\n';
                }
                if (idx == 2)
                {
                    cout << quantity3 << "  " << productname3 << price3 * quantity3 << '\n';
                }
            }

            cout << "Total\n";
            cout << sum1 << " item(s)         " << sum;

            break;
        }

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

        lineTotal = quantity * price;

        qtyItemsTotal += quantity;

        priceTotal += lineTotal;
   } while (1);

    return 0;
}

See what you think.

Andy
Nov 10, 2020 at 6:35pm
@Andy

Looking at it, that approach may well be what the 'teacher' is looking for. IMO it's so bad I would never have thought about it. Talk about teaching 'not' how to do things. I sometimes despair how C++ is being taught.....
Nov 10, 2020 at 6:59pm
@seeplus,

I agree.

My concept is sound, but the code still need work.

This works for 2 items, but what happens when it needs to be increased to handle 100 items.

Andy
Pages: 123