Getline while loop

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

if (Item != "") {
// Change fill.
std::cout << std::setfill(' ');
// format receipt. Display Items and Price
std::cout << std::left << std::setw(21) << Item << "$" << std::right << std::setw(9) << price;
std::cout << " " << Taxcode << std::endl;

} 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;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#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" ;
    }
}


More information: http://www.cplusplus.com/forum/general/69685/#msg372532
Last edited on
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)


Last edited on
> where is the extra line at the bottom coming from?

This (checking for input failure before the attempted input) is fundamentally unsound: while (!Fin.eof()) {

Something like this perhaps (assuming that you aren't familiar with writing functions):

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

int main()
{
    const std::string file_name = "HW3_Data.txt" ;

    bool quit = false ;
    std::cout << std::fixed << std::setprecision(2) ;

    while( !quit )
    {
        bool input_from_file = false ;

        std::cout << "\n\n----------------------------\nHello, Welcome to StuffMart"
                  << "\n-------------------------------\n\n"
                  << "Enter yes if you would like to input your own items: " ;
        std::string input ;
        std::cin >> input;
        if( input != "yes" )
        {
            std::cout << "Enter yes if you would like to use a data file (any thing else to quit): " ;
            std::cin >> input;
            if( input != "yes" ) quit = true ;
            else input_from_file = true ;
        }

        std::string item ;
        double price ;
        char tax_code ;

        if( input_from_file )
        {
            // print a header
            std::cout << std::setw(15) << "item" << std::setw(10) << "price($)"
                      << std::setw(10) << "tax_code" << '\n'
                      << std::string( 35, '_' ) << '\n' ;

            std::ifstream file(file_name) ; // open the file for input
            while( std::getline( file >> std::ws, item ) && !item.empty() && file >> price >> tax_code )
            {
                std::cout << std::setw(15) << item << std::setw(10) << price
                          << std::setw(6) << tax_code << '\n' ;
            }
        }


        else if( !quit )// input from stdin
        {
            // open the file for output in append mode
            // (to add the entered items to the end of the file)
            std::ofstream file( file_name, std::ios::app ) ;

            int cnt = 0 ; // number of items
            while( std::cin.ignore( 1000, '\n' ) && // throw the new line away
                   std::cout << "\nitem #" << ++cnt << "? (enter an empty line to end input): " &&
                   std::getline( std::cin, item ) && !item.empty() &&
                   std::cout << "price? " && std::cin >> price &&
                   std::cout << "tax code (Y/N)? " && std::cin >> tax_code
                 )
            {
                // display the item on stdout
                std::cout << std::setw(15) << item << std::setw(10) << "$ " << price
                          << std::setw(6) << ( tax_code == 'y' ? 'Y' : 'N' ) << '\n' ;
                // write the information to the file
                file << item << '\n' << price << '\n' << ( tax_code == 'y' ? 'Y' : 'N' ) << '\n' ;
            }
        }

        std::cout << "\n\nEnter yes to run the program once again: " ;
        std::cin >> input ;
        quit = input != "yes" ;
    }

    std::cout << "Thank you for shopping at StuffMart\n" ;
}
Topic archived. No new replies allowed.