Reading Data from a file with a selection option

earlier last week I posted a question about file reading. I really didn't get the responds I was looking for. So after reading a bunch of info on for loops and while loops, I finally understand how to use them. below is my example of code, I hope it helps someone as someone has heledp me. I'm sure it can be tweaked but its a start. good luck!

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
ifstream inputfile;
	int number;
	string names;
	int min_num;
	int max_num;

	

	// open file
	

	cout << " This program displays a list of names from a txt.file" << endl;  // greeting 
	cout << " " << endl;
	

	inputfile.open ("lineUp.txt");


	// get the filename from user 
		cout << " enter the start number: number has to start with the number one" << endl;
		cin >>  min_num;
		cout << " enter the end number: any number from 1 - 35 " << endl;
		cin >> max_num;

		// validation
			

			if (inputfile.is_open())
				{
				cout << "File opened\n" << endl;
				}
			else
				{
				cout << ("File Failed to open") << endl;
				return 0;
				}

			// opens and displays nmaes
				
				
				
			
				while (inputfile >> names)
					{
				

			

							for(number = min_num; number <= max_num; number++)
							{
								if (number >= 36  ){
								cout << " Error: 1 - 35 only, try again" << endl;
								return 0;}
								if (number < 0 ){
								cout << " Error: 1 - 35 only:  try again" << endl;
								return 0;}

								inputfile >> names;
								
								inputfile;
								cout << number  << "\t" << (names) << endl;
						
							}

						break;
					}
    
    // Close the output file
    inputfile.close();
	return 0;

}
Last edited on
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

string maxRain (double arr[], string months[], int n);
string minRain (double arr[], string months[], int n);
double totRain(double arr[],int n);
double avgRain(double arr[],int n);
string showList (double arr[], string months[], int n);
void sortRain (double arr[], string months[], int n);

int main ()
{
    double totRainfall=0.0;
    double avgRainfall=0.0;
    string minRainfallMonth;
    string maxRainfallMonth;
	string listofmonths;
   

    const int n=12;
    string months[n]={"January","February","March","April","May","June","July","August","September","October","November","December"};
    double totals[n];

    for (int i=0;i<n; i++)
    {
       
        cout << "Enter rainfall for " << months[i]  << ": ";
        cin >> totals[i];

        if (totals[i] <0 )
        {
            cout << "invalid data (negative rainfall) -- retry";
            cout << "Enter rainfall for " << months[i] << ": ";
            cin >> totals[i];
        }


    }
	
		totRainfall = totRain(totals, n);
        maxRainfallMonth= maxRain(totals, months, n);
        minRainfallMonth= minRain(totals, months, n);
        avgRainfall = avgRain(totals, n);
	
		//cout << " " << endl;
    	cout << "Total rainfall: " << totRainfall;
    	cout << "\nAverage rainfall: " << avgRainfall;
    	cout << "\nLeast rainfall in: " << minRainfallMonth;
    	cout << "\nMost rainfall in: " <<maxRainfallMonth;
		cout << " " << endl;
		
		sortRain (totals, months, n);
		listofmonths =showList(totals, months, n);
		
	
   
    //cout << "\n\n";
    //system("pause");
}

double avgRain(double arr[],int n)
{
    double tot=0.0;
    for (int i=0; i<n; i++)
        tot+=arr[i];

    return tot/n;
}

double totRain(double arr[],int n)
{
    double tot=0.0;
    for (int i=0; i<n; i++)
        tot+=arr[i];

    return tot;
}


string maxRain(double arr[], string months[], int n)
{
    double max=arr[0];
    int idx=0;
    for (int i=0; i<n; i++)
        if (arr[i]>max)
        {
                max=arr[i];
                idx=i;
        }

    return months[idx];
}


string minRain(double arr[], string months[], int n)
{
    double min=arr[0];
    int idx=0;
    for (int i=0; i<n; i++)
        if (arr[i]<min)
        {
                min=arr[i];
                idx=i;
        }

    return months[idx];
}
void sortRain(double arr[], string month[], int SIZE)
{
   int i;
   int j;

   int min;

for (i = 0; i < SIZE - 1; i++)
{
    min = i; // The intial subscript or the first element.

    for (j = i + 1; j < SIZE; j++)
    {
        if (arr[j] > arr[min])  /* if this element is greater,
                                     then it is the new minimum */
        {
            min = j;
        }
    }   

    // swap both variables at the same times
    double tempDouble = arr[i];
    arr[i] = arr[min];
	arr[min] = tempDouble;

    string tempString = month[i];
    month[i] = month[min];
    month[min] = tempString;
}
 

//for(i=0; i<=11; i++)
// {
//    /* Display the amount of rainfall per month from highest to
//       lowest */
//
//	 cout << "\n" << month[i] << "\t" << arr[i] << endl;
// }
}

string showList (double arr[], string months[], int n)
{
	double max=arr[0];
    int idx=0;
	int count = 0;
	//cout<< "\n";
	for (int count = 0; count < n; count++)
	{
		cout<< arr[count] << " inches " << "in "<< months[count];
		cout << endl;

	}
	
	return months[idx];
}
Topic archived. No new replies allowed.