ctypea program

Pages: 123
Based upon Andy's 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
137
138
139
140
141
142
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{
	string exitCheck;
	string
		productname1,
		productname2,
		productname3,
		productname4;
	int
		quantity1 {},
		quantity2 {},
		quantity3 {};
	double
		price1 {},
		price2 {},
		price3 {};
	int qtyItemsTotal {}, count {}, quantity {};
	double price {}, lineTotal {}, priceTotal {};

	cout << fixed << setprecision(2);

	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;
		}

		if (count == 3)
		{
			cout << "Product name: ";
			getline(cin, productname4);

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

		if (count > 2 || (exitCheck == "x" || exitCheck == "X"))
		{
			cout << " - OFFICIAL RECEIPT -\n";
			cout << "Qty\tProduct name\tPrice\n";

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

			cout << "Total\n";
			cout << qtyItemsTotal << " item(s)\t\t" << priceTotal << '\n';

			break;
		}

		if (count == 0)
		{
			cout << "Quantity : ";
			cin >> quantity1;
			quantity = quantity1;
		}

		if (count == 1)
		{
			cout << "Quantity : ";
			cin >> quantity2;
			quantity = quantity2;
		}

		if (count == 2)
		{
			cout << "Quantity : ";
			cin >> quantity3;
			quantity = quantity3;
		}

		if (count == 0)
		{
			cout << "Price : ";
			cin >> price1;
			price = price1;
		}

		if (count == 1)
		{
			cout << "Price : ";
			cin >> price2;
			price = price2;
		}

		if (count == 2)
		{
			cout << "Price : ";
			cin >> price3;
			price = price3;
		}

		// Add extras here

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

		lineTotal = quantity * price;

		qtyItemsTotal += quantity;

		priceTotal += lineTotal;
	} while (++count);

	return 0;
}


giving:


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


NB. I disown this code!

I Came up with something a little different.

1
2
3
4
5
6
7
8
9
10
11
12
do
{
    cout << "\nProduct name (x) to exit: ";
    getline(cin, userInput);

    if (userInput == "x")
        exitCheck = "x";
    else if (count == 0)
    {
        productname1 = userInput;
    }
    // <--- + 2 more else if statements. 


And set up the output as:
1
2
3
4
cout
    << noshowpoint << setprecision(0) << setw(2) << quantity1 << "  "
    << left << setw(13) << productname1
    << right << showpoint << setprecision(2) << setw(7) << price1 * quantity1 << '\n';


Which puts on the screen:

Product name (x) to exit: Bread
Quantity: 3
Price   : 65.75

Product name (x) to exit: Banana
Quantity: 4
Price   : 9.2

Product name (x) to exit: Noodles
Quantity: 5
Price   : 6.4

Product name (x) to exit: x

 - OFFICIAL RECEIPT -
Qty Product name   Price
 3  Bread         197.25
 4  Banana         36.80
 5  Noodles        32.00
Total
12 item(s)        266.05



Andy
Andy,
how did you place them? I'm having a hard time trying to figure out.

I'm currently on seeplus's code see if the prof approves so this whole thing is done, lol.
Thanks again guys.

edit; the program needs to be asking over and over until "x" or "X" is entered. I'll try to hit him up to infinite if's lmao.
Last edited on
Hello seghrein2300,

seghrein2300 wrote:

how did you place them? I'm having a hard time trying to figure out.


Sorry, I do not quite understand what you are referring to with "place them?".

Andy
Andy,

Oh sorry, I meant how you wrote down the code is it connected to seeplus's work?
Hello seghrein2300,

seeplus's code may have had a little influence, but it was more an idea I had Tuesday morning and probably based more on what I have seen here in the past.

The initial concept was easy. It took getting the program finished and to compile along with a lot of testing to get to what I ended up with.

Now that I think about it I would make 1 more change. if (count < 3 && (userInput == "x" || userInput == "X")) and after the last "else if"
1
2
3
4
else
{
    exitCheck = "x";
}

I have just thought of this, so it is not tested yet, but the idea is to keep from trying to enter more than 3 items.

Since I did not plan out the code first it took some extra time to test and adjust until it worked out.

Andy

P.S. Anything more will have to wait until the morning.
Handy Andy,

Ahh okay I see, I'll try to play along with the codes here to see if I can get the right output, my prof hasn't replied yet so...

Thank you again for helping out you all.
seeplus,
ghday is there a way to like loop your code not only limiting to 3 or adding new if line?
yes - but it's not nice. You need to add more variables - productname5, productname6..., quantity4, quantity5..., price4, price5.... etc as needed. The all the if (count == ....) conditions need to be extended and then change line 61 as needed.

Note that the if tests in lines 88 - 128 can be combined into 1 test for each number (quantity & price) rather than as at present one test for quantity and one test for price.

Have fun and good luck!
Hey seeplus,

on your previous code that I just saw, is there a way to remove 'stod()'?
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
#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);
}


edit1; or like find other alternatives for it? cause we haven't been on stoi, stod.
Last edited on
Hello seghrein2300,

I agree with seeplus it is possible, but after 10 I would say the it becomes ridiculous and shortly after that it is ludicrous to continue. I might consider 10 at the most just to show that it can be done.

To start with:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
     constexpr int MAXITEMS{ 3 };  // <--- Added. Based on the number of variables in lines 6 to whatever the end would be.

    string userInput;  // <--- Changed. Removed "exitCheck".

    string
        productname1,
        productname2,
        productname3;
    double
        quantity1{},
        quantity2{},
        quantity3{};
    double
        price1{},
        price2{},
        price3{};

    int qtyItemsTotal{}, count{};  // <--- Changed.
    double quantity{}, price{};    // <--- Changed.
    double priceTotal{};           // <--- Changed. 

Because of the changes I made I included all the variables, but the modt important are lines 5 - 16.

Lets say that you want to up this to handle 100 items. You would have to add 97 lines to each variable (productname?, quantity? and price?). That is a total of 291 lines not counting the rest of the code that needs to be added.

Somewhere between 5 and 10 I would have to consider using a vector or array just to keep the code down.

Sometimes when working on a program there comes a time when you just have to put it down and not think about it for awhile. Having done that I had a revelation when I looked at the do while loop. When I looked at the first lines of the loop it just screamed improvement, So I came up with this:
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
do
{
    cout << "\nProduct name (x) to exit: ";
    getline(cin, userInput);

    if ((userInput[0] == 'x' || userInput[0] == 'X') || count == MAXITEMS)
    {
        cout << "\n - OFFICIAL RECEIPT -\n";
        cout << "Qty Product name   Price\n";

        for (int idx = 0; idx < count; idx++)
        {
            if (idx == 0)
            {
                cout
                    << noshowpoint << setprecision(0) << setw(2) << quantity1 << "  "
                    << left << setw(13) << productname1
                    << right << showpoint << setprecision(2) << setw(7) << price1 * quantity1 << '\n';
            }

            if (idx == 1)
            {
                cout
                    << noshowpoint << setprecision(0) << setw(2) << quantity2 << "  "
                    << left << setw(13) << productname2
                    << right << showpoint << setprecision(2) << setw(7) << price2 * quantity2 << '\n';
            }

            if (idx == 2)
            {
                cout
                    << noshowpoint << setprecision(0) << setw(2) <<quantity3 << "  " 
                    << left << setw(13) << productname3 
                    << right << showpoint << setprecision(2) << setw(7) << price3 * quantity3 << '\n';
            }
        }

        cout << "Total\n";
        cout << setw(2) << qtyItemsTotal << " item(s)       " << setw(7) << priceTotal;

        break;
    }

    while (cout << "Quantity: " && !(cin >> quantity) || quantity < 1)
    {
        if (!cin)
        {
            cerr << "\n     Invalid Entry! Must be a number.\n\n";

            cin.clear();
            cin.ignore(10000, '\n');
        }
        else if (quantity < 1)
        {
            cerr << "\n     Quantity must be at least 1.\n\n";
        }
    }

    while(cout << "Price   : " && !(cin >> price) || price == 0.0)
    {
        if (!cin)
        {
            cerr << "\n     Invalid Entry! Must be a number.\n\n";

            cin.clear();
            cin.ignore(10000, '\n');
        }
        else if (price == 0.0)
        {
            cerr << "\n     Price must be greater than 0.\n\n";
        }
    }

    cin.ignore();  // <--- Moved. Removes the "\n" from the formatted input before the next "getline".

    if (count == 0)
    {
        productname1 = userInput;

        quantity1 = quantity;

        price1 = price;
    }

    if (count == 1)
    {
        productname2 = userInput;

        quantity2 = quantity;

        price2 = price;
    }

    if (count == 2)
    {
        productname3 = userInput;

        quantity3 = quantity;

        price3 = price;
    }
        
    qtyItemsTotal += static_cast<int>(quantity);

    priceTotal += quantity * price;

    count++;
} while (1);

In line 4 you input to "userInput" which you use and later set "productnamr?" to "userInput".

In line 6 your original idea is to check a string against a string. A good idea, but what if "Xxabc" was entered. Now you are checking a string of 5 characters against a string with 1 character. They do not match. The down side to this is that it shows that a "std::string" is just an array. So is using a "std::string" acceptable or should it be considered an array that is not allowed?

I added the last or part, untested though to force the receipt and keep you from trying to enter something that you can not.

The big changes are from line 44 to 101.

The 2 while loops are a good idea since you are using formatted input to enter a number, but if this is more than you can use just take the "cout" and "cin" in the while condition and use just those 2 lines.

The other part of these 2 while loops is that quantity and price are put into a more generic variable to be dealt with in the if statements.

I think the if statements starting at line 76 speak for themselves. The added advantage is the the if statements could be turned into a switch very easy. The switch would be easier to write, maintain and change as needed.

Lines 103 and 105 I changed to better deal with adding to the totals.

This is different than seeplus's code, but the concepts may still be used with his code.

Andy
Hello seghrein2300,

I did some more work on the program, but did not want to say anthing until I had a chance to test it.

In the for loop to print the receipt I came up with this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
for (int idx = 0; idx < count; idx++)
{
    if (idx == 0)
    {
        quantity = quantity1;
        price = price1;
        productname = productname1;
    }
    if (idx == 1)
    {
        quantity = quantity2; price = price2; productname = productname2;
    }
    if (idx == 2) { quantity = quantity3; price = price3; productname = productname3; }

    cout
        << noshowpoint << setprecision(0) << setw(2) << quantity << "  "
        << left << setw(13) << productname
        << right << showpoint << setprecision(2) << setw(7) << price * quantity << '\n';

The difference between the 3 if statements is to show they can be shortened to use less lines.

The "cout" has been changed so that only 1 is needed thus saving many lines of code.

An alternative if you can use a switch:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
for (int idx = 0; idx < count; idx++)
{
    switch (idx)
    {
        case 0: quantity = quantity1; price = price1; productname = productname1; break;
        case 1: quantity = quantity2; price = price2; productname = productname2; break;
        case 2: quantity = quantity3; price = price3; productname = productname3; break;
    }

    cout
        << noshowpoint << setprecision(0) << setw(2) << quantity << "  "
        << left << setw(13) << productname
        << right << showpoint << setprecision(2) << setw(7) << price * quantity << '\n';
}

A switch would also work well for the input section.

This is what the screen looks like:

Product name (x) to exit: Bread
Quantity: 3
Price   : 65.75

Product name (x) to exit: Banana
Quantity: 4
Price   : 9.2

Product name (x) to exit: Noodles
Quantity: 5
Price   : 6.4

Product name (x) to exit: next  // <--- Used to test what happens when you try to enter more than the variables can handle.
                                // Tests forcing the receipt.

 - OFFICIAL RECEIPT -
Qty Product name   Price
 3  Bread         197.25
 4  Banana         36.80
 5  Noodles        32.00
Total
12 item(s)        266.05



Andy
Hey Handy Andy,

Thanks for your work and also seeplus, I appreciate ya'll helping me out in here.

I tried submitting your work but he ain't vibing with limited inputs as his replies tell.
He wants unlimited input until "x, X" is entered, also he will be giving out a clue to us and I am waiting to know. Anyways guys I will update it here the clue that he would tell, I wish this is close to over.

edit 1;
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
#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);
}


this is the code I'm tryna tweak or find alternatives of stod() since it ain't allowed, Dang.
Last edited on
Hello seghrein2300,

He wants unlimited input until "x, X" is entered, also he will be giving out a clue to us and I am waiting to know.


Based on this and what you have said that is not allowed. Two thing come to mind until you learn more.

First you could create a string stream to hold the input, but this may not be allowed.

Second you could write the input to a file then read it back when you print the receipt.

A third alternative would be to gather input and write the entire receipt to a file until "x" is entered. Then if the file needs to be displayed on the screen read the file back line by line and print it to the screen.

Till the clue is revealed we will just have to wait.

Andy
@Andy, string stream and string have already been ruled out, together with anything that even resembles an array. The last option to me, as you said above, is to use a file.

I await the big reveal......
ok, I am new here and did a drive by, but it sure looks to me like the loop runs until you get that x input, via the if (x) break logic.
Hey guys, I'm sorry I have not posted the clue here but it is said and done already. My prof was only tricking us to think that we have code limitations to see if someone attempts to make a code that reads from a file which Handy Andy and seeplus mentioned as the last option/alternative.

But he gave extra credit to some of us attempting it with streams, if/else, loop and etc..

Thank you all for posting and trying to help out in this topic! I've learn a lot obv. This will be a helpful thread in the future.
Last edited on
Topic archived. No new replies allowed.
Pages: 123