Reading text input from .lbf files

Hiya there everyone, I'm very new to C++ and am trying to figure things out as I go along. I have code for a program I have already made where it reads text input from an lbf file "level1.lbf", and outputs the text for that to two separate variables, "height" and "width". I would now like some help with adapting this program so that I can ask the user which file they would like to load e.g.
1. level1.lbf
2. level2.lbf
3. level3.lbf
and based on their selection, find the data from the given file. (I already understand how to print / read a user's input). Upon selecting level3, they will receive an error telling them that it does not exist as there is no such file. I have planned out some basic Pseudo here:

Print “Which file would you like to read?
1. level1.lbf
2. level2.lbf
3. level3.lbf”
Read user input
check for successful load of file

if (file is found)
{
Print output of file
}
else
{
Print "Cannot read file"
}

if (user inputs 3)
{
Print "Error: level3.lbf could not be loaded!"
}
END

I just need help with changing my code below so that it is searching for the file selected by the user. I don't want this done for me, more of just a basic explanation of how I can achieve this. Thanks for any help in advance! :)



[code]
#include "stdafx.h"
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
int height, width;
fstream myReadFile;
myReadFile.open("level1.lbf", fstream::in);

if (myReadFile)
{
myReadFile >> height;
myReadFile >> width;

cout << "Height is " << height << endl;
cout << "Width is " << width << endl;

}

else
{
cout << "cannot read file";
}


myReadFile.close();

system("pause>null");
return 0;
}
Input an int according to your menu (so it should be 1, 2 or 3).

Use a switch statement testing this integer to open either level1.lbf, level2.lbf or level3.lbf according to whether it is 1, 2 or 3.

You can find information about switch as the last control structure in
http://www.cplusplus.com/doc/tutorial/control/
Hi lastchance. I've tried my best to implement this correctly but it is not working. Below is my code with the switch statement in place:

/*Week_4_Practical_2
*Author Alex Stroud
* 19 October 2016
* Program to read data from a range of text files
*/


#include "stdafx.h"
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
int height, width, userInput; // declared variables for both height and width
fstream myReadFile; // fstream is used to both read and write a file
myReadFile.open("level1.lbf", fstream::in);
myReadFile.open("level2.lbf", fstream::in);

cout << "Which file would you like to read?" << endl;
cout << "1. level1.lbf" << endl;
cout << "2. level2.lbf" << endl;
cout << "3. level3.lbf" << endl;
cin >> userInput;

switch (userInput)
{
case '1':
myReadFile >> height;
myReadFile >> width;

cout << "Height is " << height << endl;
cout << "Width is " << width << endl;
break;
case '2':
myReadFile >> height;
myReadFile >> width;

cout << "Height is " << height << endl;
cout << "Width is " << width << endl;
break;
case '3':
cout << "Error: level3.lbf could not be loaded!" << endl;
break;
default:
cout << "Cannot read file" << endl;
break;
}




myReadFile.close();

system("pause>null");
return 0;
}

With it the way it currently is, it is skipping over the cases and going straight to the default displaying "cannot read file." I'm not sure if this is because of the fact I am trying to use two files or not, but I can't figure out how to make it read the input correctly.
You could list all *.lbf files and have the user choose from that list.

To open your file you could do something like this

1
2
3
4
5
string FileName;
ifstream InFile;
cout << "Enter File Name: ";
cin >> FileName;
InFile.open(FileName.c_str());
I want to keep it the way it is with just having to select either 1, 2, or 3, and for the switch statement to work. I just wanna know what's wrong with it currently. Thanks.
Look at this snippet:
1
2
3
fstream myReadFile; // fstream is used to both read and write a file
myReadFile.open("level1.lbf", fstream::in);
myReadFile.open("level2.lbf", fstream::in);

First your comment is incorrect, you're trying to open 2 different files for input. But remember you can only open one file per stream.

Please use code tags when posting code!
Okay, I understand then that I need to use more than one stream. But how do I apply the opening of the files to two separate streams? Would I have another stream like "fstream myReadFile2" or something? Sorry for sounding really incompetent, but I really don't understand much about what I'm doing here without it being explained.
Kinassassinable:

The order of operations should be:
- write your menu
- get the integer from the user
- switch block
{
- all you need do within the switch block is OPEN the relevant file
}
- proceed with myReadFile as before


In your last code snippet, as jlb points out, you tried to attach two files to the same stream and they weren't within the switch{} block.
If you're careful you can reuse a stream for multiple files.

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
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    int height, width, userInput; // declared variables for both height and width
    ifstream myReadFile; // Notice using ifstream, not fstream, because an ifstream has fewer failure modes.

    cout << "Which file would you like to read?" << endl;
    cout << "1. level1.lbf" << endl;
    cout << "2. level2.lbf" << endl;
    cout << "3. level3.lbf" << endl;
    cin >> userInput;


    switch(userInput)
    {
        case '1':
            myReadFile.open("level1.lbf");
            if(!myReadFile)
            { // Always check to see that the file opens correctly!
                std::cerr << "Failed to open the level1.lbf file."
                break;
            }
            myReadFile >> height;
            myReadFile >> width;

            cout << "Height is " << height << endl;
            cout << "Width is " << width << endl;
            break;
        case '2':
            myReadFile.open("level2.lbf");
            // Don't forget to check that the file opens properly.
            myReadFile >> height;
            myReadFile >> width;

            cout << "Height is " << height << endl;
            cout << "Width is " << width << endl;
            break;
        case '3':
            cout << "Error: level3.lbf could not be loaded!" << endl;
            break;

        default:
            cout << "Cannot read file" << endl;
            break;
    }

    // myReadFile.close(); // This is not needed here, the file will automatically close when the program finishes.

    system("pause>null");
    return 0;
}


By the way your switch statement won't operate the way you expect because there is a difference between the a char constant '1' and the number 1. Your userInput variable is an int so you need to use integers in the case statements.

If you plan on using this code inside a loop you will need to make sure you always close the file before the break statement.

Topic archived. No new replies allowed.