switch with user input


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  i am supposed to use a switch statement to open 3 txt files.

i have txt;
txt1.txt
txt2.txt
txt3.txt

so for a switch to get user input.
//file number is the number the user entered either 1,2 or 3.

switch(fileNumber)
   case '1':


   case '2':

   default:

how do i make it open the correct txt based on the user entering the 1,2 or 3 number.
closed account (Dy7SLyTq)
case 1:
file.open("file1.txt");
break;
case 2:
file.open("file2.txt");
break;
//etc etc
Or you could generalise it a bit more:

1
2
3
4
5
6
7
8
9
std::vector filenames;

filenames.push_back("file1.txt");
filenames.push_back("file2.txt");

// Read user selection into i
// ...
// Open file
file.open(filenames[i]);


Reduces the repeated code.
Last edited on
Thanks guys
Topic archived. No new replies allowed.