Asking user to select a file and outputting the results

Need help on one tiny problem. I have asked the user regarding which file they want. If they press a, b, or c. How do contruct a code in such a way that it opens only that file?

For instance, if a user asks file2.txt to be open. my program will only read data from file2.txt file.

I have started it partially, but cant seem to know how to move further 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
  
	//********************************************************************START OF CODE**********************************************************************************************

	//Declare Variable for names, two test grades, counter for number students, average of test, average of all assignments and tests, letter grade, etc...
	string firstname;
	string lastname;
	double testgrade1;
	double testgrade2;
	double assign1;
	double assign2;
	double assign3;
	int counter;
	counter=0;
	double testaverage;
	double assignaverage;
	double totalpoints;
	//Getting the sum of the entire class in the loop
	double sum=0;
	double numericgrade;
	//Set minimum to 100 to find the minimum value
	double minimum=100;
	
	//Declare and initilize maximum to find the maximum value of the student
	double maximum=0;

	//Declare average to find the class average
	double average;
	
	
	
	
	//Ask the user which file they want to read from
	cout<<"Which file would you like to read data from: (A, B, C)";

	//Declare the variable where user will type in a character
	char answer;
	cout<<"\n\t\t\tA.) File1.txt";
	cout<<"\n\t\t\tB.) File2.txt";
	cout<<"\n\t\t\tC.) File3.txt";
	cout<<"\n\n\t\t\tFile: ";
	
	cin>>answer;
	switch (answer)
	{
		case 'A':
			cout<<"\n\n\tFile1.txt opening";
			break;
		case 'B':
			cout<<"\n\n\tFile2.txt opening";
			break;
		case 'C':
			cout<<"\n\n\tFile3.txt opening";
			break;
		default:
			cout<<"\n\n\tInvalid Input!";
	}

	// Open file2.txt 
	
	
	ifstream fin;
	fin.open("File1.txt");

	
	//In case, file doesnt open. Print out an error message

	if (!fin)
		{
			cout<<"Cannot open the input file" <<"\nProgram Terminates";
			
			//This return1 will exit the program due to error
			return 1;
	
		}
	
	//**************************************************CONSOLE OPEN**************************************************************************************************************

	//Outputing data names that will be outputted on the screen. For instance, name, grades, letter grade
		cout<< " Student Statistics: " << endl <<endl<<endl;
		cout<< "Name\t" << "\tTest" << "\t    Assignments" << "\t      Points" <<"\t  Numeric " <<"\tLetter ";
		cout<< endl << "\t\t Avg" << "\t        Avg"<< "\t\t\t    Grd" << "\t\t Grd ";
	

		//Enter in first name and last name in the filetxt
		fin>>firstname >>lastname;
		fin>>testgrade1 >>testgrade2;
		fin>> assign1 >>assign2>>assign3;

	




	//Going into the while loop.
		while (fin)
		{
			
			//Print out the first and last name of the student
			cout<<left;
			cout<< "\n\n" << setw(11)<< firstname+ " " +lastname;

			//Calculate the testaverage. Round it to two decimal places, with trailing zeroes
			testaverage=(testgrade1+testgrade2)/2;
			cout<<setprecision(2) <<fixed<<showpoint;
			cout<<right<<setw(10) <<testaverage;

			//Calculate the assignment average of each student
			assignaverage=(assign1+assign2+assign3)/3;
			cout<< setw(15);
			cout<<assignaverage;

			//Calculate the total points accumalated
			totalpoints= testgrade1+testgrade2+assign1+assign2+assign3;
			cout<< setw(16) <<totalpoints;

			//Calculate the the numericgrade. Add up assignments and test, then divide by 5 to find the numericavg grade
			numericgrade= (testgrade1+testgrade2+assign1+assign2+assign3)/5;
			cout<< setw(12) <<numericgrade;

			
			
			//you will use the if and else statement to calculate the letter grade

			
			//If number is higher then 90. Student gets an A
			if(numericgrade>=90)
				cout<<setprecision(15)<<"\t  A";
			
			//If number is higher then 80. Students gets a B
			else if (numericgrade>=80)
				cout<<setprecision(15)<<"\t  B";

			//If number is higher then 70. Student gets a C
			else if (numericgrade>=70)
				cout<<setprecision(15)<<"\t  C";

			//Any lower then 70, gets an F
			else 
			{
				cout<<setprecision(15)<<"\t  F";
			}
			
		//Minimum is declared as 100. If minimum is greater then numeric grade, then numeric grrade will become minimum
		//For instance, numeric grade is 89 and minimum is 100. Minimum is greater then 89. 89 becomes the minimum. 
		//During the next loop, 89 is stored in mininum.

			if(minimum>numericgrade)
				minimum=numericgrade;


		//Maximum is declared at zero. If maxium is less then numeric grade then maxium will become numeric grade.
			if (maximum<numericgrade)
				maximum=numericgrade;

			//Prevent the loop from becoming an infinite loop. Enter in names, scores, etc...
			fin>>firstname >>lastname;
			fin>> testgrade1 >>testgrade2;
			fin>> assign1 >>assign2>>assign3;

			//Sum is used in this loop to find the total sum of numeric grades
			sum=sum+numericgrade;
			
			//Add counter++ to determine the total number of students
			counter++;

			
				
		}
		
		
		

		//Find the average of students
		average=sum/counter;

		//If the file is an empty set, I will set counter=0 and other values to zero.
			if (counter==0)
			{
			average=0;
			minimum=0;
			maximum=0;
			
			
			
			}
			
		//Overall Class Statistics output

		//Display Class Statistic on console
		cout<<"\n\n\n\tClass Statistics ";

		//Display The minimum grade out of the whole class and have all the values right manipulated
		cout<<right;

		//Display number of students in class and the calculation of total students
		cout<<"\n\n\t\tNumber: "<< setw(7)<<counter;

		


	

		cout<<"\n\n\t\tMinimum: " <<setw(6)<<setprecision(2)<<minimum;

		//Display the maximum grade out of the whole class
		cout<<"\n\n\t\tMaximum: " <<setw(6)<<setprecision(2)<<maximum;

		
		cout<<"\n\n\t\tAverage: " <<setprecision(2)<<setw(6)<<average;


		//Close the file
		fin.close();

	_getch();
	return 0;
}
Don't want to overwhelm you guys with my code. I have shortened a quite so you will be able to see it well.

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
//Ask the user which file they want to read from
	cout<<"Which file would you like to read data from: (A, B, C)";

	//Declare the variable where user will type in a character
	char answer;
	cout<<"\n\t\t\tA.) File1.txt";
	cout<<"\n\t\t\tB.) File2.txt";
	cout<<"\n\t\t\tC.) File3.txt";
	cout<<"\n\n\t\t\tFile: ";
	
	cin>>answer;
	switch (answer)
	{
		case 'A':
			cout<<"\n\n\tFile1.txt opening";
			break;
		case 'B':
			cout<<"\n\n\tFile2.txt opening";
			break;
		case 'C':
			cout<<"\n\n\tFile3.txt opening";
			break;
		default:
			cout<<"\n\n\tInvalid Input!";
	}

	// Open file2.txt 
	
	
	ifstream fin;
	fin.open("File1.txt");

	
	//In case, file doesnt open. Print out an error message

	if (!fin)
		{
			cout<<"Cannot open the input file" <<"\nProgram Terminates";
			
			//This return1 will exit the program due to error
			return 1;
	
		}
	


When you call fin.open, pass it some variable whose value you can change at execution time.
NOTE: You will have to do some error handling for the case where input is invalid!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
    cin>>answer;
    string fileToOpen;
    switch (answer)
    {
    case 'A':
        fileToOpen = "File1.txt";
        break;
    case 'B':
        fileToOpen = "File2.txt";
        break;
    case 'C':
        fileToOpen = "File3.txt";
        break;
    default:
        cout<<"\n\n\tInvalid Input!";
    }

    cout<<"\n\n\t" << fileToOpen << " opening";
    ifstream fin;
    fin.open(fileToOpen);
Last edited on
Thank you booardley! I have got this problem resolved :) Suppose, I want the user to enter in one of the files. Once they have opened that, I want them to return to this loop and the program to ask again which file they want to open. How can I make a loop like that?
I have tried to use a do while loop.

I have a "do" prior to my code...and while in the end.


sentinel='a';
do
{
allstatements

}

while(!sentinel);


This method is not working at all. I did try using another variable like "try++" and then did work, but only when I have the while setup like this while(try<100);
anyone
try this:

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
    char openAnotherFile;
    
    do {
        cin>>answer;
        string fileToOpen;
        
        switch (answer)
        {
        case 'A':
            fileToOpen = "File1.txt";
            break;
        case 'B':
            fileToOpen = "File2.txt";
            break;
        case 'C':
            fileToOpen = "File3.txt";
            break;
        default:
            cout<<"\n\n\tInvalid Input!";
        }

        cout<<"\n\n\t" << fileToOpen << " opening";
        ifstream fin;
        fin.open(fileToOpen);
       
        //******** Prompt if the user want to open another file
        cout << "\nDo you want to open another file ? ( y/n ): ";
        cin >> openAnotherFile;
    
    } while ( openAnotherFile =='y' || openAnotherFile == 'Y' );
Thank you for your response shadow fiend. I have tried your solution out too and similar to my approach I'm getting a little logical error in my program. All of my files consist of the same topic.

File.txt1 consists of 5 students with grades and other data

file.txt2 conists of 15 students with grades and other data

file.txt3 consists of no data

My problem when using your loop and my similar loop is that when it does ask me whether I want to open another file. I press yes, however I get a totally different number of students.

For instance. When I open file.1 txt

number of students should be 5

when the program asks me for another file, and I type in file 2. Instead of getting 15 from the number of students I get 20 which I believe it added from file1.
anyone please :( I have been working on this problem for nearly 1 hour however cant find a valid solution.
This assignment has been submitted to my teacher; however , anyone out there that can tell me how can I use a do while loop to ask the user for another file.

I did try the previous method recommended by fellow c++ members but In my code when I implement that, my calculations from the first file are added to second.

For instance, when file one closes, I woukd have 15 students . Program asks the user for another file they want and suppose they want file2. File 2 opens and displays all the results. In file 2 there are onky 5 students but on my console it's displaying 20. From my perspective, and I'm highly sure of it, the data is being added or being included in file2 too...
knock knock?
can you show your updated code ?
Topic archived. No new replies allowed.