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.