I tried doing it but every time I look down "- OFFICIAL RECEIPT - " I am getting confused. How can it read what I'm gonna input next on a loop? because it only read the last inputs I will enter. It closes/end with an 'x' or 'X'
Here is my attempt source code that is not even close to half.
There is not enough code to duplicate the problem or see what is going on.
Line 5 is the only variable that is left after the do/while loop.
Line 7 is created each time you enter the loop and destroyed each time the loop ends, meaning the closing }.
Line 10 the variable is "sum", but sum of what? And at the moment "quantity" just feels like the wrong variable to add.
The code you are missing is what prints each line of the receipt.
The do/while loop looks like it is useful, but I do not see where you are storing the the value of the variables "productname", "quantity" and "price" outside of the loop.
#include <cctype> // <--- For "std::tolower() and std::toupper()" + others.
#include <iostream>
#include <iomanip>
#include <string>
usingnamespace std;
int main()
{
string productname;
double quantity = 0, price = 0, qprice{}, flist{}; // <--- "qprice" is never used here and "flist"'s value never changes.
double subTotal{}; // <--- Maybe this would be a better name.
do
{
cout << "\nProduct name: ";
cin >> productname;
subTotal += quantity; // <--- Maybe you meant subTotal += quantity * price;
if (std::tolower(productname[0]) == 'x' /*|| productname == "X"*/)
{
cout << "\n - OFFICIAL RECEIPT -\n";
cout << "Qty Product name Price\n";
cout << flist;
break;
}
cout << "Quantity: ";
cin >> quantity;
cout << "Price: ";
cin >> price;
} while (1);
std::cout
<< quantity << " "
<< productname << " "
<< price << '\n';
return 0; // <--- Not required, but makes a good break point.
}
And this is the output I get:
Product name: 1
Quantity: 2
Price: 1
Product name: 2
Quantity: 4
Price: 2
Product name: x
- OFFICIAL RECEIPT -
Qty Product name Price
04 x 2
^ This number is from inside the loop from the if statement. It is "flist" who's value has never changed.
The 4 x and 2 are the last values of the variables "quantity", "productname" and "price". Not what you want.
Line 20 seams better suited to come after line 34.
Your program is a little bit more than halfway there but you need to rethink what it is you are trying to do.
pseudo-code organizes your thinking and planning before you start typing code. ( where have you heard that before )
The way I read it is:
1. LOOP:
2. If product name is not x/X continue, otherwise get out of loop
3. get details of name price and quantity of a product
4. process the current receipt total
5. go back to LOOP
Now, you can see from this you need somewhere to store the data because you can't write out the receipt until you have everything including the total. The way it is you are left to print the header and mixing it up with the input data.
You need arrays, at least. And to keep it simple maybe allow for 5 items maximum.
thanks ya'll for helping me understand the logic. but I am limited not to use arrays sadly. I tried tweaking my code based on what you guys sent. here it is.
As we go up (not down) in complexity perhaps use <vectors>'s.
I'm now just as interested as you are to see how this will run out if arrays are excluded.
Aside from stringstreams, displaying the (formatted) data on another terminal instance, using a GUI platform with textbox storage of some sort, or saving the data to a file and retrieving it for displaying the receipt, I can't think of a simpler way than C-style arrays. But I am open to whatever ...
BTW while(10), like while(1) just keeps the loop going around because 1, 10 or whatever number you put in their is always true.
Also, having the while at the start has the small advantage you can bail out before any data beyond a bail out 'x' is input.
Good luck with it. I look forward to the array alternatives :)
BTW If you include the heading all of that above applies. If you don't include the heading then use <iomanip> functionality to format/present the processed data. Even then mixing input with formatted output to the console/terminal becomes interesting unless you use something like nCurses software.
is it possible that cin.ignore() and cin.getline() can fix my output at -OFFICIAL RECEIPT-?
This tutorial example shows how the two fit together.
I'm not sure what 'fix my output' means. They won't fix the storage problem that arrays sor other containers solve. If I were you I would have a talk to your teacher or tutor on this and also the array ban.
If you have a productname that is made up of two or more words then you would use getline, instead of cin. cin.ignore(...) is explained in the tutorial.
As far as the operation of the while loop I suggest you read up on while loops and practice and test your ideas. They can be tricky so a simple test inside the while condition is the best way to go. Putting the if x stuff inside the while only makes it more complicated than it needs to be.
If you need to process/display which has previously been entered, then some sort of storage is required to hold the data. This would often be an array (if the number of items is known) or a vector.
but I am limited not to use arrays sadly
Does this also apply to using a vector - which would be the usual container used? If you aren't allowed to use a vector, can you use a file? what about dynamic memory? Assuming this is an exercise, what is your teacher expecting you to use?
Those questions plus others have already been raised and we are waiting answers from the teacher/tutor.
Of course, along the lines of a creative use of stringstream there might be a solution using getline(). The problem is, however, that tokenization, parsing or whatever would be a challenge way beyond the beginner level at an introductory course.
It sounds like this is a school assignment. The first thing you should have done is pass along the instructions that you were given. This way everyone will know what you have to do without asking questions about what you can use.
I have come up with this code using a string stream, but you may not be able to use it:
#include <cctype> // <--- For "std::tolower() and std::toupper()" + others.
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
usingnamespace std;
int main()
{
std::stringstream outStream;
string productname;
double quantity = 0, price = 0, qprice{}, flist{}; // <--- "qprice" is never used here and "flist"'s value never changes.
double subTotal{}, lineTotal{}; // <--- Maybe this would be a better name.
std::cout << std::fixed << std::showpoint;
do
{
cout << "\nProduct name (x) to end: ";
cin >> productname;
if (std::tolower(productname[0]) == 'x' /*|| productname == "X"*/)
{
break;
}
cout << "Quantity: ";
cin >> quantity;
cout << "Price: ";
cin >> price;
lineTotal = quantity * price;
subTotal += lineTotal; // <--- Maybe you mean subTotal += quantity * price;
outStream
<< std::setprecision(0) << std::setw(2) << quantity << " "
<< std::left << std::setw(13) << productname
<< std::right << std::setprecision(2) << std::setw(6) << lineTotal << '\n';
} while (1);
cout << "\n - OFFICIAL RECEIPT -\n";
cout << "Qty Product name Price\n";
//cout << flist;
std::cout << outStream.str();
std::cout
<< std::string(25, '-')
<< '\n' << std::string(18, ' ') << std::setprecision(2) << std::setw(6) << subTotal << '\n';
return 0; // <--- Not required, but makes a good break point.
}
The only problem I am having is with line 43. It does not print ".00" and I am not sure why. I am not that familiar with creating a string stream this way so any input is appreciated.
If you can not use a string stream the only other option I can think of is to use a file.
I hope this is at least something that will help you grt to the right direction.
Product name (x) to end: Apples
Quantity: 1
Price: 1.56
Product name (x) to end: Oranges
Quantity: 2
Price: 1
Product name (x) to end: x
- OFFICIAL RECEIPT -
Qty Product name Price
1 Apples 1.6
2 Oranges 2
-------------------------
Sub Total 3.56
Product name [CR to end]: apple
Quantity: 2
Price: 4
Product name [CR to end]: pear
Quantity: 3
Price: 10
Product name [CR to end]:
2 apple 4
3 pear 10