0

Here we have a code that opens a filestream from a notepad but it doesn't open the notepad file. I have also attached the folder of the program itself so you can check it as your reference. please help us. Thank you very much!

Please help. hre

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <cctype>
#include <cmath>
using namespace std;

void display();
void input();
void equate();
void calculate();
void lineChecker();

double num1=0;
double num2=0;
double ans=0;
char in1;
char in2;
string line;
bool error=false;
char operate;

void main()
{
	ifstream inFile;
	inFile.open("Input.txt");
	
		
		
		cout<<"opened"<<endl;
		system("pause");
}
void lineChecker()
{	if(line[0]=='num1'||line[0]=='num2')
		equate();
	else if(line[0]=='c')
	{
		if(line[1]=='o')
		{
			if(line[2]=='u'&&line[3]=='t')
				display();
			else
			{
				error=true;
				main();
			}
		}

		else if(line[1]=='i'&&line[2]=='n')
		{
			input();
		}
		else
		{
			error=true;
			main();
		}
	}
	else
	{
		error=true;
		main();
	}
}

void input()
{
	if(line[3]=='>'&&line[4]=='>'&&line[line.size()-1]==';')
	{
		if(line[5]=='num1')
		{
			cout<<"num1:";
			cin>>in1;
			if(isalpha(in1))
			{
				error=true;
				main();
			}
			num1=atoi(&in1);
		}

		else if(line[5]=='num2')
		{
			cout<<"num2:";
			cin>>in2;
			if(isalpha(in2))
			{
				error=true;
				main();
			}
			num2=atoi(&in2);
		}
		else if(line[5]=='c'&&line[6]=='a'&&line[7]=='l'&&line[8]=='c'&&line[9]=='u'&&line[10]=='l'&&line[11]=='a'&&line[12]=='t'&&line[13]=='e')
		{
			cout<<"calculate:";
			cin>>operate;
			calculate();
		}
	else
	{
		error=true;
		main();
	}
	}
}



void equate()
{
	char t;
	if(line[0]=='num1'&&line[1]=='='&&line[3]==';')
	{
		t=line[2];
		if(!isdigit(t))
		{
			error=true;
			main();
		}
		num1=atoi(&line[2]);
	}
	else if(line[0]=='num2'&&line[1]=='='&&line[3]==';')
	{
		t=line[2];
		if(!isdigit(t))
		{
			error=true;
			main();
		}
		num2=atoi(&line[2]);
	}
	else
	{
		error=true;
		main();
	}
}

void display()
{
	int w;
	w=7;
	if(line[4]=='<'&&line[5]=='<'&&line[line.size()-1]==';')
	{
		if(line[6]=='"')
		{
			if(line[line.size()-2]=='"')
			{
				while(line[w]!='"')
				{
					cout<<line[w];
					w++;
				}
			}
			else if(line[line.size()-7]=='<'&&line[line.size()-6]=='<'&&line[line.size()-5]=='e'&&line[line.size()-4]=='n'&&line[line.size()-3]=='d'&&line[line.size()-2]=='l')
			{
				while(line[w]!='"')
				{
					cout<<line[w];
					w++;
				}
				cout<<"\n";
			}
			else
			{
				error=true;
				main();
			}
		}
	
		else if(line[6]=='num1')
		{
			if(line[7]==line[line.size()-1])
				cout<<num1;
			else if(line[7]=='<')
			{
				if(line[8]=='<'&&line[9]=='e'&&line[10]=='n'&&line[11]=='d'&&line[12]=='l')
					cout<<"num1:"<<num1<<endl;
				else
				{
					error=true;
					main();
				}
			}
			else
			{
				error=true;
				main();
			}
		}
		else if(line[6]=='num2')
		{
			if(line[7]==line[line.size()-1])
				cout<<num2;
			else if(line[7]=='<')
			{
				if(line[8]=='<'&&line[9]=='e'&&line[10]=='n'&&line[11]=='d'&&line[12]=='l')
					cout<<"num2:"<<num2<<endl;
				else
				{
					error=true;
					main();
				}
			}
			else
			{
				error=true;
				main();
			}
		}
		else
		{
			error=true;
			main();
		}
	}
	else
	{
		error=true;
		main();
	}
}

void calculate()
{
	if(operate=='+')
		ans=num1+num2;
	else if(operate=='-')
		ans=num1-num2;
	else if(operate=='*')
		ans=num1-num2;
	else if(operate=='/')
	{
		if(num2!=0)
			ans=num1/num2;
		else
		{
			cout<<"Cannot divide by zero"<<endl;
			error=true;
			main();
		}
	}
	else
	{
		error=true;
		main();
	}
}
2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by VernonDozier
0
void main()
{
	ifstream inFile;
	inFile.open("Input.txt");
	
		
		
		cout<<"opened"<<endl;
		system("pause");
}

This is fine. The rest of the code you posted is irrelevant. No function is ever called. How do you know it doesn't open? There is no attempt to read from it and there is no check to see whether it actually opened successfully. I would expect this program to display "opened" regardless of whether the file actually opened.


>> Here we have a code that opens a filestream from a notepad but it doesn't open the notepad file.

I assume you mean that you created the file called "Input.txt" with the text editor "Notepad" provided with Windows? If so, the program you created the text file with is irrelevant. All that matters is that the file exists, it's on a path that the program can find it, and you have the correct permissions to open it for reading it. And again, the code doesn't do anything with the file so whether you can open it is irrelevant. What precisely is the problem?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.