`Hello and thanks for reading my post. I am trying to develop a program that will select a file for opening based on user input. I did have this code working at one point but no longer. I am a beginner at C++ so I am trying to learn from my mistakes but I need a little help at this point. I would appreciate any positive input.

#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>
#include <string>
#include <stdlib.h>
using namespace std;
//**********************************************************************************
//********** SWITCH CASE MODULE  ***************************************************
//**********************************************************************************
int main ()
{
    int input;
    const char *x;
    cout<< "What is your Heating problem?\n"<<endl;
    cout<<"1. No Heat\n";
    cout<<"2. Not Enough Heat\n";
    cout<<"3. Water Leak\n";
    cout<<"4. Noise\n";
    cout<<"5. Smell\n";
    cout<<"6. Exit\n";
    cout<<"Selection:";
    cin>> input;
    system("CLS");
    switch (input)//Main Selection Menu
    {
    case 1://No Heat Menu
            cout<<"What type of fuel do you have?\n"<<endl;
            cout<<"1. Oil\n";
            cout<<"2. Gas\n";
            cout<<"3. Electric\n";
            cout<<"Selection:";
            cin>>input;
            system("CLS");
        switch (input)//No Heat // Fuel Type
            {
        case 1://Oil
            x = "Data.csv";
            break;
        case 2://Gas
            x = "Data1.csv";
            break;
        case 3://Electric
            x = "Data2.csv";
            break;
            }
    case 2://Not Enough Heat
            cout<<"What type of Heating system do you have?\n"<<endl;
            cout<<"1. Steam\n";
            cout<<"2. Hot Water\n";
            cout<<"3. Hot Air\n";
            cout<<"Selection:";
            cin>>input;
            system("CLS");
        switch (input)//Not Enough Heat // System Type
            {
        case 1://Steam
            x = "data3.csv";
            break;
        case 2://Hot Water
            x = "data4.csv";
            break;
        case 3://Hot Air
            x = "data5.csv";
            break;
            }
    case 3://Water Leak
            cout<<"What type of Heating system do you have?\n"<<endl;
            cout<<"1. Steam\n";
            cout<<"2. Hot Water\n";
            cout<<"Selection:";
            cin>>input;
            system("CLS");
        switch (input)//Water Leak // System Type
            {
        case 1://Steam
            x = "data6.csv";
            break;
        case 2://Hot Water
            x = "data7.csv";
            break;
            }
    case 4://Noise
            cout<<"What type of Heating system do you have?\n"<<endl;
            cout<<"1. Steam\n";
            cout<<"2. Hot Water\n";
            cout<<"3. Hot Air\n";
            cout<<"Selection:";
            cin>>input;
            system("CLS");
        switch (input)//Noise // System Type
            {
        case 1://Steam
            x = "data8.csv";
            break;
        case 2://Hot Water
            x = "data9.csv";
            break;
        case 3://Hot Air
            x = "data10.csv";
            break;
            }
    case 5://Smell
            cout<<"What type of Smell do you have?\n"<<endl;
            cout<<"1. Gas\n";
            cout<<"2. Oil\n";
            cout<<"3. Burning\n";
            cout<<"Selection:";
            cin>>input;
            system("CLS");
        switch (input)//Smell / Type
            {
        case 1://Gas
            x = "data11.csv";
            break;
        case 2://Oil
            x = "data12.csv";
            break;
        case 3://Burning
            x = "data13.csv";
            break;
            }
    case 6:
        cout<<"Program has terminated!\n";
        break;
    default:
        cout<<"Error, bad input, quitting\n";
        break;
        }
  string line;
  ifstream myfile(x);
  if (myfile.is_open())
    {
    while  ( myfile.good() )
    {
      getline (myfile,line);
      cout << line << endl;
    }
    myfile.close();
    }
  else cout << "Unable to open file, check code or file path\n";
  return 0;

}

`

What errors are you getting?

You need another break; statment at the end of each outer switch block, before the next case statement, as in (from lines 46-48):

            }
            break;
    case 2://Not Enough Heat
            cout<<"What type of Heating system do you have?\n"<<endl;

plus breaks for the others.

cases 1, 2, 3, 4 and 5 need a break at the end of them before the start of the next case.

Awesome, thanks for your help. I have appended the code and got it working. I have one question, I was reading somewhere in a forum that using switch case to do file handling was unstable. Has anyone experienced this problem ? Thanks

I was reading somewhere in a forum that using switch case to do file handling was unstable.

That's nonsensical. Nothing about a switch or "file handling" in general discourages stability. It's certainly possible to write brittle code, but that's a problem with the programmer, not the constructs being used.

So I'd say that whoever wrote what you were reading either meant something else, or was quite confused.

Thankyou, now I know I am on the right track...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.