help with vectors

how to print out the qualified items in a vector if a user decides to search for it?
example:amount>= 100
200.20 Monthly electric, water and gas
1200 Rent
150.50 Weekly movie, music, concert and entertainment
400 Friday restaurant and dining out
175.75 Home repair and improvement
333 Yearly Tax and insurance
160 Gift and charitable contributions
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
  #include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cstdlib>
#include <iomanip>
#include <algorithm>
using namespace std;

class expense
{
public:
	double c_price;
	string c_description;
};
int main(int argc, char* argv[])
{
	ifstream fin;
	fin.open("expenses2.txt");
	if (!fin)
	{
		cerr << "Error in opening the file" << endl;
		return 1;
	}
	vector<expense> expense_vector;
	expense temp;
	while (fin >> temp.c_price)
	{
		getline(fin, temp.c_description);
		expense_vector.push_back(temp);
	}

	string command;
	cout << "spend\nshow\namount >=\nsave file\nhelp\nexit\n" << endl;
	cin >> command;
else if (command == "amount >=")
	{
		double item = 0;
		cout << "please enter the minimum price" << endl;
		cin >> item;
		if (find(expense_vector.begin(), expense_vector.end(), item) != expense_vector.end())
		{
			
		}
		else
		{
			cout << "no price was qualified" << endl;
		}

	}
Last edited on
Hello mcclit,

Quick observations on your program.

First you use an input file. Post the complete input file so everyone will be using the same information and not have to guess about what might be in this file.

The class puts the variables in "public" this defeats the purpose of the class and you might as well use a struct.

Your "else if" statement on line 36 has no matching if statement because you define variables and have a while loop in-between.

Watch your indentation it helps.

It may have been a copy problem, but you missed the closing } of "main".

The if/else starting at line 41 I believe should be a for loop that checks the amount and prints out what is acceptable.

Once I fix the errors and can get the code to compile and run I will know more.

Hope that helps,

Andy
find(...) on line 41 isn't the right thing for your purpose since it returns only one element. You need a [range based] loop like:
1
2
3
4
5
6
7
for(const expense &e : expense_vector)
{
  if(e.c_price >= item)
  {
    ...
  }
}
Hello mcclit,

After getting the program to run this is what I saw:

Your program starts with:

spend
show
amount >=
save file
help
exit


This is asking much of the user to enter the correct choice. You will find that a user will break your program without even trying when they enter something like "Show" or SHOW" because the had "Caps Lock" turned on and you did not account for this. Not to mention the spelling mistakes that can happen. This is to much to ask of the user.

I think you will find this better:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int choice;

std::cout << std::fixed << std::showpoint << std::setprecision(2);

cout
	<< "\n 1. Spend"
	<< "\n 2. Show"
	<< "\n 3. Amount >="
	<< "\n 4. Save file"
	<< "\n 5. Help"
	<< "\n 6. Exit\n"
	<< "  Enter choice: ";

cin >> choice;

 1. Spend
 2. Show
 3. Amount >=
 4. Save file
 5. Help
 6. Exit
  Enter choice:

I do not know what you can use, but I followed the input here with a "switch" which calls the necessary functions. If you are not up to functions you can put the code in the case statements. And if you are not familiar with a switch you can use if/else if statements with the final else statement to catch anything that is invalid.

It would also be a good idea to follow the menu choice with some kind of verification to know that only a valid choice was entered. This can also be handled in the "default' case section or the final "else" of the "if/else" if statements.

For now I created two functions for the menu choices. One "Show()" and the other "Report()" although this should have a better name to describe what it does.

In the functions I started with the same idea that coder777 has suggested. If you are not familiar with or able to use a range base for loop a regular for loop will work.

Just for fun I added a sort routine, but that will take some work to set up to use the third parameter of "std::sort".

After getting the program to run I realized what the "else if" on line 36 is for. By its self you can not have just an "else if" you would need at least:
1
2
3
4
5
6
if (choice == 1)
    ;
else if(...)
{
    // ...
}

The alternative with the switch is that you can leave out the case statements that you are not ready for yet.

Hope that helps,

Andy
Thank you so much Andy, I sorry that i could not get to you in time because I fell asleep in the desk(lol). Your suggestions helped me greatly and I am sorry for the inconvenience. Thank you.
Hello mcclit,

You are welcome. No inconvenience as long as you learned something.

Andy
Topic archived. No new replies allowed.