Switch statement issue

76 views Asked by At

I've made some modifications and now 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. How can I fix this?

//Random-Access Files Program
//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;

         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');
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;
     }
}    
1

There are 1 answers

0
Talha Çolakoğlu On

First of all, you defined "choice" as char, therefore you should use it in case part like this:

case '1':

or you should define "choice" as int. (Remember '1' different than 1)

Second of all, you should use escape character in file name when you try to open files.

char* filename = "C:\\bla\\bla.txt";

or (without escape character)

char* filename = "C:/bla/bla.txt";