Write your question here.
So Im super confused and can't seem to figure this out. Im trying to figure out how to get information from a file ( which I figured out) and assign them to variables in order to format them into a receipt. the data file looks as follows
Bread
2.49
Y
Milk
1.89
N
Eggs, dozen
0.97
N
Apples
4.75
Y
Bananas
1.69
Y
Peanut Butter
2.49
Y
my code is as follows (I'm stuck where the while loop is)ps I'm on a mac using CLion
[code]
Put the code you need help with here.
[code]
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main() {
cout << "Hello, Welcome to StuffMart" << endl;
cout << endl;
string input;
cout << "Would you like to input your own items? [yes/no]" << endl;
cin >> input;
// how do save data such as items and price and tax code being that it is an undetermined amount of items
if (input == "yes") { cout << "Please enter item 1 ";}
else if (input == "no") { cout << "Would you like to use a data file? [yes/no]" << endl; }
cin >> input;
if (input == "yes") {
ifstream Fin;
string Item;
float price;
char Taxcode;
const float tax= .85;
cout << endl;
cout << endl;
cout << endl;
cout << "Thank you for shopping at StuffMart" << std:: endl;
cout << left << setw (21) << "Item" << "Unit Price" << " Tax" << endl;
cout << setfill ('-') << setw (35) << "-" << endl;
// Set decimal place.
cout << fixed << setprecision (2);
// how do i use the data from the file or store the values of an undetermined amount of data/ items
Fin.open("HW3_Data.txt");
while (!Fin.eof()) {
getline(Fin, Item);
} else { }
// how do i do a while loop to make it start the process over again
} } else if ( input == "no") { cout << "Would you like to input your own items? [yes/no]" << endl;}
return 0;
}
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
int main()
{
std::ifstream file( "HW3_Data.txt" ) ;
std::string item ;
double price ;
char tax_code ;
// file >> std::ws extracts and discards leading white space characters from the input stream
// this is required because after the formatted input file >> price >> tax_code
// a new line character would be remaining in the input buffer
// we want to throw that new line away and get to the next line to read the name of the item
while( std::getline( file >> std::ws, item ) && !item.empty() && file >> price >> tax_code )
{
std::cout << "item: " << std::quoted(item)
<< " price: " << price
<< " tax code: '" << tax_code << "'\n" ;
}
}
while i was waiting for a response i got this for the code
code : #include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main() {
cout << "Hello, Welcome to StuffMart" << endl;
cout << endl;
string input;
cout << "Would you like to input your own items? [yes/no]" << endl;
cin >> input;
// how do save data such as items and price and tax code being that it is an undetermined amount of items
if (input == "yes") { cout << "Please enter item 1 ";}
else if (input == "no") { cout << "Would you like to use a data file? [yes/no]" << endl; }
cin >> input;
if (input == "yes") {
ifstream Fin;
string Item;
float price;
char Taxcode;
const float tax= .85;
cout << endl;
cout << endl;
cout << endl;
cout << "Thank you for shopping at StuffMart" << std:: endl;
cout << left << setw (21) << "Item" << "Unit Price" << " Tax" << endl;
cout << setfill ('-') << setw (35) << "-" << endl;
// Set decimal place.
cout << fixed << setprecision (2);
cout << setfill(' ');
// how do i use the data from the file or store the values of an undetermined amount of data/ items
Fin.open("HW3_Data.txt");
while (!Fin.eof()) {
getline (Fin, Item); Fin.clear (), Fin >> price, Fin.clear (), Fin >> Taxcode;
cout << left << setw(21) << Item << "$" << right << setw(9) << price << " " << Taxcode << endl;
getline (Fin, Item);
if (Item != "") {
} else { }
// how do i do a while loop to make it start the process over again
} } else if ( input == "no") { cout << "Would you like to input your own items? [yes/no]" << endl;}
return 0;
}
it gives me the correct variables in the correct spot (sort of) but the formatting is all out of place?
output:
Hello, Welcome to StuffMart
Would you like to input your own items? [yes/no]
no
Would you like to use a data file? [yes/no]
yes
Thank you for shopping at StuffMart
Item Unit Price Tax
-----------------------------------
Bread
$ 2.49 Y
Milk
$ 1.89 N
Eggs, dozen
$ 0.97 N
Apples
$ 4.75 Y
Bananas
$ 1.69 Y
Peanut Butter
$ 2.49 Y
$ 2.49 Y
it should have all the items on the same line as the price and tax code. I need to know how to get the items on the same line. also where is the extra line at the bottom coming from? how do i get rid of it? ps you can't tell from the post but the receipt is item then new line skips 9 spaces (should skip more) and dollar sign and price and tax code)