How to pass a file into a vector and into a private constructor
Jul 10, 2019 at 6:49am UTC
How to pass a file into a vector and into a private constructor, also how to display the elements of the vector if user chose to "show"
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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <cstdlib>
using namespace std;
class expense
{
private :
double c_price;
string c_discription;
public :
expense(double price, string discription)
{
price = c_price;
discription = c_discription;
if (price <= 0.0)
{
c_price = 0.0;
}
}
string toString()
{
stringstream sout;
sout << "price $(" << c_price << ") discription(" << c_discription << ")" ;
return sout.str();
}
};
bool argcCheck(int argc)
{
if (argc == 2)
return true ;
else
return false ;
}
void menu()
{
cout << "spend\nshow\namount >=\nsave file\nhelp\nexit" << endl;
}
int main(int argc, char *argv[])
{
fstream fin;
fin.open("expenses2.txt" , ios::out);
if (!fin)
{
cerr << "Error in opening the file" << endl;
return 1;
}
vector<expense> expenses;
expense temp;
while (fin >> temp.price >> temp.discription)
{
expenses.push_back(temp);
}
string command;
cout << "spend\nshow\namount >=\nsave file\nhelp\nexit" << endl;
cin >> command;
if (command =="show" )
{
}
else if (command == "help" )
{
menu();
}
else if (command == "spend" )
{
}
cout << argc << " " << boolalpha << argcCheck(argc) << endl;
if (argcCheck(argc) == false )
{
cout << "invalid command, please try again" << endl;
}
return 0;
}
Last edited on Jul 10, 2019 at 7:04am UTC
Jul 10, 2019 at 8:06am UTC
You already seem to "
pass a file into a vector " in this:
1 2 3 4 5 6
vector<expense> expenses;
expense temp;
while (fin >> temp.price >> temp.discription)
{
expenses.push_back(temp);
}
Perhaps you could explain the question?
What "private constructor"? Where?
(I think I guess what you mean, but learning to ask a question is a good goal too.)
Jul 10, 2019 at 8:43am UTC
sorry about vague question I had. The first question is how to pass a file into a vector that also store the information in the "private" section of the class expense.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
class expense
{
private :
double c_price;
string c_discription;
public :
expense(double price, string discription)
{
price = c_price;
discription = c_discription;
if (price <= 0.0)
{
c_price = 0.0;
}
}
string toString()
{
stringstream sout;
sout << "price $(" << c_price << ") discription(" << c_discription << ")" ;
return sout.str();
}
};
The second question is how to display the values that I stored inside the vector when the user prompts it?
1 2 3 4 5 6 7 8
string command;
cout << "spend\nshow\namount >=\nsave file\nhelp\nexit" << endl;
cin >> command;
if (command =="show" )
{
}
Sorry for the inconvenience that I gave earlier.
Last edited on Jul 10, 2019 at 8:45am UTC
Jul 10, 2019 at 11:40am UTC
The first question is how to pass a file into a vector that also store the information in the "private" section of the class expense.
Line 55: You use the same name for the vector obeject and the class. Change line 55 e.g. like so:
vector<expense> expenses_vector;
The second question is how to display the values that I stored inside the vector when the user prompts it?
E.g.:
1 2 3 4
for (expense &e : expenses_vector)
{
std::cout << e.toString() << '\n' ;
}
Topic archived. No new replies allowed.