Issue with switch statement

I'm having an issue with my switch statement. The functions are supposed to read random access files. When I run the program, it displays the menu and when I choose an option it only outputs the first option I choose. If I choose another option, it doesn't output the letters.

The file itself just contains the alphabet and my assignment is to read certain positions of the 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
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
//This program reads a file from beg to end, end to beg, beg to 4th position,
//8th to 15th position, end to 21st position, and 22nd position to the end

#include <iostream>
#include <fstream>

using namespace std;

void begToEnd(fstream &, char);
void endToBeg(fstream &, char);
void begTo4th(fstream &, char);
void eighthTo15th(fstream &, char);
void endTo21st(fstream &, char);
void Twenty2ndToEnd(fstream &, char);

int main()
{
    char letters;   //holds the character
    int choice;
    
    fstream file("alphabet.txt", ios::in | ios::binary);  //This opens the file
    
    do
    {
         cout << "Enter 1 to read from beginning to end" << endl
         << "Enter 2 to read from end to beginning" << endl
         << "Enter 3 to read from beginning to 4th position" << endl
         << "Enter 4 to read from 8th to 15th position" << endl
         << "Enter 5 to read from end to 21st position" << endl
         << "Enter 6 to read from 22nd position to the end" << endl
         << "Choice: ";
         
         cin >> choice;
         
         switch(choice)
         {
         case 1:
              begToEnd(file, letters);
              break;
         case 2:
              endToBeg(file, letters);
              break;
         case 3:
              begTo4th(file, letters);
              break;
         case 4:
              eighthTo15th(file, letters);
              break;
         case 5:
              endTo21st(file, letters);
              break;
         case 6:
              Twenty2ndToEnd(file, letters);
              break;
         }
         
         cout << endl;
         system("pause");
    
         file.close(); //Closes the file
}

while(choice != 'N' && choice != 'n');
cout << "Would you like to continue? Y/N: ";
cin >> choice;

return 0;    

}
void begToEnd(fstream &in, char letter)
{    
     in.seekg(0L, ios::beg);  //Displays beginning to end
     for(int i = 0; i < 25; i++)
     {
          in.get(letter);
          cout <<"Beginning to end: " << letter << endl;
     }
}

void endToBeg(fstream &in, char letter)
{
     in.seekg(0, ios::end);  //Displays end to beginning
     int size = in.tellg();
     for (int i=1; i <= size; i++)
     {
         in.seekg(-i, ios::end);
         letter=in.get();
         cout << "End to beginning: " << letter << endl;
     }
}

void begTo4th(fstream &in, char letter)
{
     in.seekg(0L, ios::beg);  //Displays beginning to 4th position
     for(int i = 0; i < 4; i++)
     {
         in.get(letter);
         cout << "Beginning to the 4th letter: " << letter << endl;
     }
}

void eighthTo15th(fstream &in, char letter)
{
     in.seekg(7L, ios::beg);  //Displays 8th to 15th position
     for(int i = 0; i < 7; i++)
     {
     in.get(letter);
     cout << "8th to 15th letter: " << letter << endl;
     }
}

void endTo21st(fstream &in, char letter)
{
     in.seekg(0, ios::end);  //Displays end to 21st position
     int size = in.tellg();
     for (int i=1; i <= 5; i++)
     {
         in.seekg(-i, ios::end);
         letter=in.get();
         cout << "End to 21st: " << letter << endl;
     }
}

void Twenty2ndToEnd(fstream &in, char letter)
{
     in.seekg(21L, ios::beg);  //Displays the 22nd position to the end
     for(int i = 0; i < 5; i++)
     {
         in.get(letter);
         cout << "22nd to end: " << letter << endl;
     }
}
First off, why is choice an int? I think you meant for it to be a char.... :)

Secondly, ask them if they want to continue inside of the do-while loop. Place it between cout << endl; and system("pause"); Be prepared for people to get all upset because you used a system command, as well. Now see what happens.
Last edited on
I had 'choice' as a char at first but it wouldn't call the function. It only calls the function as an int.

If I ask if they want to continue within the loop, it goes on a never-ending loop and doesn't stop. :'(
Also, why are you using choice for the number selection AND their decision to continue?

This is a simplified version of your program (obviously), but it is working as intended. Take it for what it is

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
#include <iostream>
using namespace std;
int main()
{
	int choice = 0;
	char decision = ' ';

	do
	{
		cout << "Enter 1 to read from beginning to end" << endl
			<< "Enter 2 to read from end to beginning" << endl
			<< "Enter 3 to read from beginning to 4th position" << endl
			<< "Enter 4 to read from 8th to 15th position" << endl
			<< "Enter 5 to read from end to 21st position" << endl
			<< "Enter 6 to read from 22nd position to the end" << endl
			<< "Choice: ";

		cin >> choice;


		cout << endl;
		cout << "Would you like to continue? Y/N: ";
		cin >> decision;

	} while (decision != 'N' && decision != 'n');
	return(0);
}
Last edited on
void begToEnd(fstream &in, char letter) is wrong.
you should declaim like this void begToEnd(fstream &in, char& letter).
otherwise, the variable letter in function is temporary, it has no relation with
"letter" outside.

or you can declaim like this:
char begToEnd(fstream &in)

call the function in your main() like leter = begToEnd(file)

i wish it could help.
Thanks! All suggestions helped! Also, I figured out why I couldn't keep switching, I had to close the file outside of the do-while loop.
Topic archived. No new replies allowed.