C++: Accessing Text File to Organize and Display to User

Write your question here.
I have a basic C++ program trying to access a text file, grab information from it, and then return that information in a organized fashion to the display in the form of a receipt. My code and text file can be seen below. I am having difficulty getting the correct number of spaces in order to align all of the stores on the left, with spaces after the colon. Thanks in advance for the help!
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
  //This is the text file.
/*
 Target, 30000 shirt .60
 Sears, 20000 pants .23
 Walmart, 50000 eggs .50
 Kmart, 300 lettuce .65
 Carrefour, 1100 bowl 2.30
*/

//This is what should be displayed.
/*
 Target:                   30000 shirt @ .60
 Sears:                    20000 pants @ .23
 Walmart:                  50000 eggs @ .50
 Kmart:                    300 lettuce @ .65
 Carrefour:                1100 bowl @ 2.30
*/

#include <fstream>
#include <string>
#include <iostream>
#include <iomanip>
#include <cstdlib>

int main()
{
    std::ifstream inputFile;
    std::string filename;
    int countOfItem;
    std::string item;
    float itemPrice;

    std::cout << "Enter the filename: ";
    std::cin >> filename;
    std::cin.ignore();

    inputFile.open(filename.c_str());

    if(!inputFile)
    {
        std::cout << "This file does not exist." << std::endl;
        return 0;
    }

    while(!inputFile.eof())
    {
        std::string store;
        char firstDelimiter(',');
        double spaces;

        std::getline(inputFile, store, firstDelimiter);

        spaces = 25 - (store.length() + 1);
        std::string numberOfSpaces;
        numberOfSpaces = std::string(spaces, ' ');

        std::cout << std::left << store + ':' << numberOfSpaces;
        store.clear();
    }


    inputFile.close();

    return 0;
}
I've tried to tackle the problem on the following lines, with comments throughout the program. Please read and come back here if something is not clear:

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<fstream>
#include<sstream>
#include<string>
#include<vector>
#include<tuple>
#include<iomanip>

using namespace std;

int main (){
fstream File;
vector<tuple<string,double,string,double>> v;//container to store the data (C++11 syntax);
File.open("F:\\test.txt");
    if(File.is_open()){//doesn't error check file non-opening, something for OP to consider;

       string line;
       static const auto delimiter = ','; //C++11 syntax; 
            while(getline (File, line)){//reads File into the string line;
                stringstream stream(line);//constructs the stringstream object, stream, with the string line;
                string co_name, item_name;
                double item_qty, item_px;
                getline(stream, co_name, delimiter) && // reads stream into co_name upto, but not including, delimiter;
            stream>> item_qty >> item_name >> item_px;  // reads rest of the stream into item_qty, item_name, item_px;

                v.emplace_back(co_name, item_qty, item_name, item_px);//fills vector<tuple<>> with the data;
            }
        }
File.close();

      for(auto& itr : v){//using range-loop (C++11);
            get<0>(itr) += ":"; //adds the colon to each co_name;
        cout<<setw(20)<<left<<get<0>(itr)<<get<1>(itr)<<" "<<get<2>(itr)<<" @ "<<get<3>(itr)<<"\n";
                //prints the vector element by element;
      }
}


Output:
1
2
3
4
5
6
7
8
Target:             30000 shirt @ 0.6
Sears:              20000 pants @ 0.23
Walmart:            50000 eggs @ 0.5
Kmart:              300 lettuce @ 0.65
Carrefour:          1100 bowl @ 2.3

Process returned 0 (0x0)   execution time : 0.027 s
Press any key to continue.
Last edited on
Thank you so much for your help! Everything is perfectly clear now. I was stuck on this for so long!
Topic archived. No new replies allowed.