User data file

I need some help on this one. Generally I am suppose to ask the user for a file name and then open that file and it will have a list of ints and doubles for example:

41 98.25 101.1 1975 10.1
44 96.12 98.2 1966 6.2
46 92.58 97.3 1942 9.3
50 88.51 88.6 1937 12.5
51 90.21 91.9 1912 15.3
52 82.02 88.8 1998 7.9
0 0.0 0.0 0 0.0

but I cannot figure out how in the world to get the file to get open.. or how to retrieve it this is how my code looks like:

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
86
87
88
89
90
91
92
#include <iostream>
#include <fstream>
#include <math.h>
#include <string>
#include <cstdlib>
using namespace std;

int main()
{
	const int MAX = 100;
	int dataEntry, day[MAX], menuSelection, desiredDay, selectedField, dayOne, dayTwo,
		decade;
	double highTemp[MAX], recordHigh[MAX], humidityPercent[MAX], yearRecord[MAX],
		heatIndex, Adjustment, minValue, maxValue, averageValue, valueRange,
		sum, numDifference, largestDiff;
	int ix = 0;
	int count = 0;
	string filename;
	ifstream infile;
	
	cout << endl << "Weather Station Records of Temperatures and Humidity" 
		<< endl << endl;

	do
	{
		cout << "Please choose from the following" << endl;
		cout << "(1) Enter data through keyboard" << endl;
		cout << "(2) Enter data through a data file" << endl;
		cout << "Please enter your selection now: ";
		cin >> dataEntry;

		if (dataEntry <= 0 || dataEntry > 2)
		{
			cout << endl << "Please enter one or two" << endl << endl;
		}
	}
	while (dataEntry <= 0 || dataEntry > 2);

	if (dataEntry == 1)
	{
		do
		{
	        cout << endl << "Enter the five data fields. "
	            "When you are done enter 0 for the day." << endl;

			cout << endl << "Enter the day " << endl;
			cout << "(Counting from the beginning of the year for example: "
				"February 2 is 33): ";
			cin >> day[ix];

			count = count + 1;

			if (day[ix] == 0) break;
			{
				cout << endl << "Enter the high temperature (degrees Fahrenheit): ";
				cin >> highTemp[ix];

				cout << endl << "Enter the record high temperature "
					"(degrees Fahrenheit): ";
				cin >> recordHigh[ix];

				cout << endl << "Enter the year the record high temperature" 
					" was recorded: ";
				cin >> yearRecord[ix];

				cout << endl << "Enter the relative humidity (in percent): ";
				cin >> humidityPercent[ix];

				++ix;
			}
		}
		while (ix < MAX);
	}
	else 
	{
		cout << endl << "Please enter the name of the file you wish to open: ";
		cin >> filename;

		infile.open(filename.c_str());
		
		if (infile.fail())
		{
			cout << endl << "The file named " << filename
				<< " was not successfully opened"
				exit(1);
		}
		else
		{
			cout << "\nThe file was successfully opened." << endl;
		}
	}


the user has the option of hand typing in the data or just loading it through a data file, but as I said before no idea how to get it open.

I have tried to load a file into my program but no avail. Does the file have to be in a certain folder in order to actually open it?

Thanks in advance
you need to ask the user for a full path and not just a filename.

This way, the program is going to look in the current folder which is generally the folder from where you are running the program.

I have a suggestion for you -
Hide complex details from the user like data file etc and make the program user friendly. User likes an application which questions less and do more. right ?

User will not be interested if you write to datafile, database or network drive or anywhere else. He will be more interested in writing and then retrieving it when required. So, you are free to apply any logic you want but the user should just know that he will write some data and it will get saved without fail.

For example, if Oracle starts to ask you in which data file you want to create a specific table, what would you do ?
Cool thank you I will definitely look into making it easier for the user.. i know my professor has told me this before too lol
Topic archived. No new replies allowed.