We are having problem with our code regarding file streaming. here is our code:

//Nava
//Maganito
//Ocampo
//Lazo
//Manata
//CS133/BC2/PROJ

#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()
{
	if(error==true)
	{
		cout<<"Error"<<endl;
		exit(1);
	}
	ifstream codes("wow.txt");
	if (codes.is_open())
	{
		while(codes.good())
		{
			getline(codes,line);
			if(line=="\0")
				cout<<"File is blank"<<endl;
			lineChecker();
		}
		codes.close();
	}
	else
	{
		cout<<"CANNOT OPEN TEXT FILE"<<endl;
		cout<<"Please check the filename..."<<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();
	}
}

Everytime we run our code, the highlighted lines in red is displayed, please help.

Recommended Answers

All 4 Replies

I would think the obvious question would be, is your text file in the location it needs to be for your executable to find it?

We are having problem with our code regarding file streaming.

No, you are having problems with your code because you are doing very bad things: void main() -- MAIN is and always has been an int function. See this. exit(1); -- is there something wrong with a simple return(1) ? system("Pause"); -- see this main(); -- Never NEVER call main(). It is an entry point to your program and should not be used as a recursive function! main(); -- and again main(); -- and again

in void input() -- main(); -- 3 times

in void equate() -- main(); -- 3 times

in void display() -- main(); -- 7 times

etc...

In these functions, a simple return should be used, and when the function returns, test the error code you set.

No, your are having problems with your code because you are doing very bad things: void main() -- MAIN is and always has been an int function. See this. exit(1); -- is there something wrong with a simple return(1) ? system("Pause"); -- see this main(); -- Never NEVER call main(). It is an entry point to your program and should not be used as a recursive function! main(); -- and again main(); -- and again

in void input() -- main(); -- 3 times

in void equate() -- main(); -- 3 times

in void display() -- main(); -- 7 times

etc...

In these functions, a simple return should be used, and when the function returns, test the error code you set.

can you show to us in the code itself what you are trying to say sir? I mean, I don't understand what's wrong with using void main() we've been using it with our other programs and its never been a problem...please help, thank you very much!

can you show to us in the code itself what you are trying to say sir?

Everything I mentioned is self-explanitory. I don't need to show you in the code because the code already is wrong, and if you change the code as I explained, it will be right.

I mean, I don't understand what's wrong with using void main() we've been using it with our other programs and its never been a problem...please help, thank you very much!

That's what the link is for. To explain so I don't have to. Read it.

Just because it works for you on your compiler doesn't make it correct. Running a red light can be done, too. But eventually you'll get caught. Then you can tell the officer "but nothing bad happened all the other times I ran the light". That is if you're not dead...

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.