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! :)
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;
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.
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.
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.
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.
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.
#include <iostream>
#include <fstream>
usingnamespace 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.