Hey,
I'm trying to write a little program for a class at school. I've never had to compile a program under UNIX before. We use SunOS at school. I wrote the following code at home under Windows using Borland C++ Builder 5, and it works perfectly. I was just hoping if someone could tell me what I'm missing to get this to compile and run under UNIX. I used the command "g++ filename" to compile it under UNIX, getting no errors. I ran "a.out" and got nothing - no output. Any help would be appreciated.
Thanks!
(Oh yeah, what headers can be called in order to be able to clear the screen, clrscr(), and to utilize the sleep() function. I use conio.h and dos.h to do so under WIndows, but those headers are not found in UNIX.)
----------------------------------------------------------
I'm trying to write a little program for a class at school. I've never had to compile a program under UNIX before. We use SunOS at school. I wrote the following code at home under Windows using Borland C++ Builder 5, and it works perfectly. I was just hoping if someone could tell me what I'm missing to get this to compile and run under UNIX. I used the command "g++ filename" to compile it under UNIX, getting no errors. I ran "a.out" and got nothing - no output. Any help would be appreciated.
Thanks!
(Oh yeah, what headers can be called in order to be able to clear the screen, clrscr(), and to utilize the sleep() function. I use conio.h and dos.h to do so under WIndows, but those headers are not found in UNIX.)
----------------------------------------------------------
Code:
//header file includes
#include <iostream.h>
#include <fstream.h>
#include <stdio.h>
const int REG=11;
void DoOperation(int[REG],int,int,int,int);
void ClearScreen();
void Pause();
void main()
{
ifstream inData;
int registers[REG]={0};
int operation=0,operand1=0,operand2=0,stop=0;
ClearScreen();
cout<<"\n"<<endl;
//test for existence of data file
if (!inData.fail())
{
//test for end of file
while (inData.peek()!=EOF)
{
stop=0;
inData>>operation;
//checks if operations 0-5 are to be performed - only two operands are input
if ((operation==0)||(operation==1)||(operation==2)||(operation==3)||
(operation==4)||(operation==5))
{
inData>>operand1>>operand2;
//checks for invalid registers
//if first register is invalid, keep looking for valid first register first
//if second register is invalid, start looking for first register again
while ((((operand1<0)||(operand1>10))&&(stop==0))||(((operand2<0)||
(operand2>10))&&(stop==0)))
{
//error message for both registers being invalid
if (((operand1<0)||(operand1>10))&&((operand2<0)||(operand2>10)))
{
cout<<"*** ERROR :: Invalid registers, "<<operand1<<" & "<<operand2<<endl
<<" Please enter registers between 0 - 10"
<<endl<<endl;
}
else
{
//error message for first register being invalid
if ((operand1<0)||(operand1>10))
{
cout<<"*** ERROR :: Invalid register, "<<operand1<<endl
<<" Please enter a register between 0 - 10"
<<endl<<endl;
}
else
{
//error message for second register being invalid
if ((operand2<0)||(operand2>10))
{
cout<<"*** ERROR :: Invalid register, "<<operand2<<endl
<<" Please enter a register between 0 - 10"
<<endl<<endl;
}
}
}
//first register is invalid &
//second register is valid
if (((operand1<0)||(operand1>10))&&((operand2>=0)&&(operand2<=10)))
{
operand1=operand2;
inData>>operand2;
}
else
{
//first register is valid &
//second register is invalid
if (((operand1>=0)&&(operand1<=10))&&((operand2<0)||(operand2>10)))
{
inData>>operand1>>operand2;
}
else
{
//both registers are invalid
if (((operand1<0)||(operand1>10))&&((operand2<0)||(operand2>10)))
{
inData>>operand1>>operand2;
}
}
}
//checks if there is no more data for the current operation
//1 = End of File has been reached
//2 = End of Line has been reached
if (inData.peek()==EOF)
{
stop=1;
}
else
{
if ((inData.peek()=='\n')&&(((operand1<0)||(operand1>10))&&((operand2<0)||
(operand2>10))))
{
stop=2;
}
}
}
}
else
{
//checks if operation 6 is to be performed - only one operand is input
if (operation==6)
{
inData>>operand1;
operand2=0;
//checks for invalid registers
while (((operand1<0)||(operand1>10))&&(stop==0))
{
cout<<"*** ERROR :: Invalid register, "<<operand1<<endl
<<" Please enter a register between 0 - 10"
<<endl<<endl;
inData>>operand1;
//checks if there is no more data for the current operation
//1 = End of File has been reached
//2 = End of Line has been reached
if (inData.peek()==EOF)
{
stop=1;
}
else
{
if ((inData.peek()=='\n')&&((operand1<0)||(operand1>10)))
{
stop=2;
}
}
}
}
else
{
//checks if operations 7-8 are to be performed - no operands are input
if ((operation==7)||(operation==8))
{
operand1=0;
operand2=0;
}
else
{
//error message for invalid operations
cout<<"*** ERROR :: Invalid command. Please enter one of the "
<<"following:"<<endl
<<" 0 - add"<<endl
<<" 1 - multiply"<<endl
<<" 2 - subtract"<<endl
<<" 3 - divide"<<endl
<<" 4 - modulus"<<endl
<<" 5 - store"<<endl
<<" 6 - print register contents"<<endl
<<" 7 - print all registers"<<endl
<<" 8 - re-initialize all registers"<<endl<<endl;
}
}
}
DoOperation(registers,operation,operand1,operand2,stop);
}
}
else
{
//error message if input file could not be opened
cout<<"*** ERROR :: Could not open file, registers.inp"<<endl<<endl;
}
Pause();
} //end of main
// Carries out operation function
void DoOperation(int regs[REG],int oper,int oprnd1,int oprnd2,int stp)
{
char opt;
if (((oper==0)||(oper==1)||(oper==2)||(oper==3)||(oper==4))&&(stp==0))
{
//checks for division by zero
if (((oper==3)||(oper==4))&&(regs[oprnd2]==0))
{
cout<<"*** ERROR :: Register "<<oprnd2<<" is initialized to zero."<<endl
<<" Cannot divide by zero. Operation not performed."<<endl
<<endl;
}
else
{
switch (oper)
{
case 0: regs[oprnd1]=regs[oprnd1]+regs[oprnd2];
cout<<"OPERATION 0 - Add Two Register Values To One Another"<<endl;
opt='+';
break;
case 1: regs[oprnd1]=regs[oprnd1]*regs[oprnd2];
cout<<"OPERATION 1 - Multiply Two Register Values With One Another"
<<endl;
opt='*';
break;
case 2: regs[oprnd1]=regs[oprnd1]-regs[oprnd2];
cout<<"OPERATION 2 - Subtract One Register Value From Another"
<<endl;
opt='-';
break;
case 3: regs[oprnd1]=regs[oprnd1]/regs[oprnd2];
cout<<"OPERATION 3 - Divide One Register Value By Another"<<endl;
opt='/';
break;
case 4: regs[oprnd1]=regs[oprnd1]%regs[oprnd2];
cout<<"OPERATION 4 - Modulus Determination Of One Register Value On"
<<" Another"<<endl;
opt='%';
break;
}
cout<<" Register "<<oprnd1<<" "<<opt<<" Register "<<oprnd2
<<" = "<<regs[oprnd1]<<endl<<endl;
}
}
else
{
if ((oper==5)&&(stp==0))
{
regs[oprnd1]=oprnd2;
cout<<"OPERATION 5 - Store a Value to a Specified Register"<<endl
<<" A value of "<<oprnd2<<" has been stored to Register "
<<oprnd1<<endl<<endl;
}
else
{
if ((oper==6)&&(stp==0))
{
cout<<"OPERATION 6 - Print Specified Register Value"<<endl
<<" Register "<<oprnd1<<" ==> "<<regs[oprnd1]<<endl<<endl;
}
else
{
if (oper==7)
{
cout<<"OPERATION 7 - Print All Register Values"<<endl<<endl
<<" REGISTER VALUE"<<endl
<<" ========================="<<endl
<<" 0 ==> "<<regs[0]<<endl
<<" 1 ==> "<<regs[1]<<endl
<<" 2 ==> "<<regs[2]<<endl
<<" 3 ==> "<<regs[3]<<endl
<<" 4 ==> "<<regs[4]<<endl
<<" 5 ==> "<<regs[5]<<endl
<<" 6 ==> "<<regs[6]<<endl
<<" 7 ==> "<<regs[7]<<endl
<<" 8 ==> "<<regs[8]<<endl
<<" 9 ==> "<<regs[9]<<endl
<<" 10 ==> "<<regs[10]<<endl<<endl;
}
else
{
if (oper==8)
{
for (oprnd1=0; oprnd1<10; oprnd1++)
{
regs[oprnd1]=0;
}
cout<<"OPERATION 8 - Re-initialize All Registers"<<endl
<<" All registers have been re-initialized to 0."<<endl
<<endl;
}
else
{
//error message if there is no more data in file for the current operation
if (((oper==0)||(oper==1)||(oper==2)||(oper==3)||(oper==4)||(oper==5)||
(oper==6))&&(stp==1))
{
cout<<"*** ERROR :: Unable to complete last operation."<<endl
<<" No data left in input file to do so."<<endl
<<endl;
}
else
{
//error message if there is no more data for the current operation
if (((oper==0)||(oper==1)||(oper==2)||(oper==3)||(oper==4)||(oper==5)||
(oper==6))&&(stp==2))
{
cout<<"*** ERROR :: Unable to complete last operation."<<endl
<<" Incomplete data to do so."<<endl
<<endl;
}
}
}
}
}
}
}
return;
} //end of DoOperation
//clears the screen (part of conio.h)
void ClearScreen()
{
printf("%c[2J%c[%i;%iH",27,27,1,1);
cout<<endl<<endl;
} //end of ClearScreen
//used to hault termination of program
void Pause()
{
int exit=0;
cout<<endl;
cout<<"Enter any value to exit: ";
cin>>exit;
} //end of Pause