I am trying to read a file from fstream in a switch statement but something in the code seems to be stopping this part of my code to be read by the compiler. Please some one let me know what am I doing wrong. Its not the text file thats having problem, for sure.

#include "stdafx.h"
#include <iostream>
#include<fstream>
#include<iomanip>
#include<stdlib.h>
using namespace std;

int main()
{
	char option = '\0', user;
	

	cout<< setiosflags(ios::left)<< "\t \t \t UH Visitors Parking "<<endl;

	cout<< setiosflags(ios::left)<< "_______________________________________________________________"<<endl;
	
	cout<<"\n"<<setiosflags(ios::right)<< "\t Help \t Car \t MotorCycle \t SeniorCitizen \t Quit";

	cout<<"\n"<<setiosflags(ios::left)<<"_______________________________________________________________"<<endl;
	
	cout<<"Please select an option from above."<<endl;
	cin>> option;

	switch(option)
	{
		case 'H':
		//case 'h':
			cout<<"Hello."<<endl;
			ifstream input("Help.txt");
			if(input.fail())
			{
				cout<<"File not found."<<endl;
			}

			//if( user = '\n')
				exit(0);
			break;
	}



	

	return 0;
}

Also I had another question.
My assignment asks me, when the user presses enter or so after reading the file, the screen should get clear and display the above menu again. But I wasn't sure how would I clear the screen. I see that I can display the menu by using a loop but not sure about clearing screen. Any ideas for this will be appreciated.

Thanks.

I don't see a problem at all. The only reason could be (assuming that the file is very much present in the same directory where you are executing this code) is that you might entering 'h' instead of 'H'. Are you not getting the 'Hello.' on the screen either?

The problem is not with case of h because intially I had both cases of h in my swith statement but commented one out to figure out the problem switch statement.
And yes my file is exactly in the same folder.
I wrote the hello line to check and see if the compiler is executing the switch statement. And it is executing the hello line but not the switch statement.

This is totally weird.

if its printing 'Hello.' then it has executed the switch statement..

I know that but then why is it not opening the file. Is there something wrong in linking the file. I mean there has to be something because of which it is not working as it should.

How do you know its not opening the file? is it printing 'File not found' ?? Why dont you try and read something from the file and confirm?

Well, if the program is opening the file then I am suppose to be able to see all the text in the file. I mean there is some data on that file. The whole purpose here is that through fstream I shoud be able to open that file and be able to read it.
But on compiling, all I see is the menu, and then it says "press any key to continue."

Its not showing "failed command" but it is also not opening the file.


Ok, I tried something. I moved my .txt file from the same folder to another and then tried executing the program. It showed the failed statement( as expected). But then when I save this .txt file in the same it is not doing what it is supposte to ( i.e. read from the file).

Thanks.

There's nothing wrong with the program. You're succesfully opening it.
To have the effect you want, you first need to get the data from the file then write it back to the screen.

Oh, O.k. I understand that. But could you show me how can I show that data on the screen.

Thanks.

An example:

#include <iostream>

std::ifstream openFile("filename.txt");
std::cout << "::::::::::::::::::::::::::::FILECONTENTS::::::::::::::::::::::::::::" << std::endl;
char getFile;
while (openFile.get(getFile)) //Read file character by character(including new lines)
     std::cout << getFile; //Printing the chars back to screen
std::cout << "::::::::::::::::::::::::::::::::END:::::::::::::::::::::::::::::::::" << std::endl;
openFile.close();

Thanks.

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.