We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,524 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Trying to do file handling with nested switch case

`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;

}

`

5
Contributors
6
Replies
21 Hours
Discussion Span
3 Months Ago
Last Updated
21
Views
Tonyi1
Newbie Poster
3 posts since Feb 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

What errors are you getting?

M4trixSh4d0w
Light Poster
47 posts since Feb 2013
Reputation Points: 13
Solved Threads: 6
Skill Endorsements: 0

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.

rubberman
Posting Maven
2,587 posts since Mar 2010
Reputation Points: 365
Solved Threads: 308
Skill Endorsements: 52

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

NathanOliver
Posting Virtuoso
1,516 posts since Apr 2009
Reputation Points: 281
Solved Threads: 278
Skill Endorsements: 3

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

Tonyi1
Newbie Poster
3 posts since Feb 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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.

deceptikon
Challenge Accepted
Administrator
3,460 posts since Jan 2012
Reputation Points: 822
Solved Threads: 474
Skill Endorsements: 57

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

Tonyi1
Newbie Poster
3 posts since Feb 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0812 seconds using 2.81MB