File IO with depending on switch case.

I am new to programming. What I would like to do is once the choice is made and the user knows how much they have saved, that goes into a .txt file stamped with the date and time. My question is does the ofstream go inside the switch case or outside?

Also, is there a more efficient way to store the variables for lunch places rather then keep declaring them over and over? This is not a homework assignment this is for me.

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
  #include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>


using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	double McDonalds = 7.55;
	double Wawa = 6.89;
	double Moe = 8.99;
	double Jersey_Mikes = 9.90;
	double Panera = 11.00;
	int choiceoflunch;

	cout << "MyLunchMoney is an application which helps you identify\n how much money you are spending on lunch\n\n";
	cout << "Where would have you ate today if you did not pack your lunch?\n";
	cout << "Please select from the list\n";
	cout << " 1.McDonalds\n 2.Wawa \n 3.Moes\n 4.Jersey Mikes\n 5.Panera\n";
	cin >> choiceoflunch;




	switch (choiceoflunch)
	{
	case 1:
	choiceoflunch = McDonalds;
	cout << "You saved $" <<McDonalds << " today by packing your lunch\n";
	break;

	case 2:
	choiceoflunch = Wawa;
	cout << "You saved $" <<Wawa<< " today by packing your lunch\n";
	break;

	case 3:
	choiceoflunch = Moe;
	cout << "You saved $" <<Moe<< " today by packing your lunch\n";
	break;

	case 4:
	choiceoflunch = Jersey_Mikes;
	cout << "You saved $" <<Jersey_Mikes<< " today by packing your lunch\n";
	break;

	case 5:
	choiceoflunch = Panera;
	cout<< "You saved $" <<Panera << "today by packing your lunch\n";
	break;
    }

	system("PAUSE");
	return 0;
}

I would put the ofstream after the switch. What's the purpose of choiceoflunch = McDonalds; and the others? Maybe you have a reason for it later, but with the code you've posted here I don't see any reason to do it. Also, choiceoflunch is an int, but the prices are doubles. You are losing everything after the decimal by doing this.
This is a good example of where data is better than code. What if you decide you would have eaten at Wendy's instead? You'd have to change a whole lot of code. Here's a version where the restaurant names and prices are stored in a separate text file:

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

using namespace std;

class Restaurant {
public:
    double cost;
    string name;
};

istream &operator>>(istream &is, Restaurant &r)
{
    is >> r.cost;
    getline(is, r.name);
    return is;
}

bool readRestaurants(vector<Restaurant> &restaurants, istream &is)
{
    Restaurant tmp;
    restaurants.clear();

    is >> tmp;
    while (!is.eof() && is.good()) {
	restaurants.push_back(tmp);
	is >> tmp;
    }
    return is.eof();
}

int main(int argc, char *argv[])
{
    int choiceoflunch;
    vector<Restaurant> restaurants;
    ifstream rs("restaurants.txt");

    if (!readRestaurants(restaurants,rs)) {
	cerr<< "Can't read restaurants\n";
	return 1;
    }
    rs.close();

    cout << "MyLunchMoney is an application which helps you identify\n how much money you are spending on lunch\n\n";
    cout << "Where would have you ate today if you did not pack your lunch?\n";
    cout << "Please select from the list\n";
    for (int i=0; i<restaurants.size(); ++i) {
	cout << i+1 << ". " << restaurants[i].name << '\n';
    }
    cout << "? ";
    cout.flush();
    cin >> choiceoflunch;
    --choiceoflunch;		// convert to zero based offset

    if (choiceoflunch < 0 || choiceoflunch >= restaurants.size()) {
	cerr << choiceoflunch << " is not a valid choice.\n";
	return 1;
    }

    cout << "You saved $" <<restaurants[choiceoflunch].cost << " today by packing your lunch.\n";

    // system("PAUSE");
    return 0;
}


And restaurant.txt is:
1
2
3
4
5
7.55 McDonalds
6.89 Wawa
8.99 Moe
9.90 Jersey_Mikes
11.00 Panera


To add a new restaurant, just add it to restaurant.txt.
Thank you everyone for your responses. I planned on putting them in a file as seen in the example above. I am just working my way to it.

As for choiceoflunch = I can assume that does nothing code wise, I will remove it and continue to build of this program.

Thank you for your time guys it means a lot. I am a newbie.
Topic archived. No new replies allowed.