Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't compile under UNIX...

Status
Not open for further replies.

CSpannos

IS-IT--Management
Mar 21, 2001
32
US
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.)



----------------------------------------------------------
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<<&quot;\n&quot;<<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<<&quot;***  ERROR :: Invalid registers, &quot;<<operand1<<&quot; & &quot;<<operand2<<endl
          <<&quot;              Please enter registers between 0 - 10&quot;
          <<endl<<endl;
     }
     else
     {
      //error message for first register being invalid
      if ((operand1<0)||(operand1>10))
      {
       cout<<&quot;***  ERROR :: Invalid register, &quot;<<operand1<<endl
           <<&quot;              Please enter a register between 0 - 10&quot;
           <<endl<<endl;
      }
      else
      {
       //error message for second register being invalid
       if ((operand2<0)||(operand2>10))
       {
        cout<<&quot;***  ERROR :: Invalid register, &quot;<<operand2<<endl
            <<&quot;              Please enter a register between 0 - 10&quot;
            <<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<<&quot;***  ERROR :: Invalid register, &quot;<<operand1<<endl
          <<&quot;              Please enter a register between 0 - 10&quot;
          <<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<<&quot;***  ERROR :: Invalid command. Please enter one of the &quot;
          <<&quot;following:&quot;<<endl
          <<&quot;              0 - add&quot;<<endl
          <<&quot;              1 - multiply&quot;<<endl
          <<&quot;              2 - subtract&quot;<<endl
          <<&quot;              3 - divide&quot;<<endl
          <<&quot;              4 - modulus&quot;<<endl
          <<&quot;              5 - store&quot;<<endl
          <<&quot;              6 - print register contents&quot;<<endl
          <<&quot;              7 - print all registers&quot;<<endl
          <<&quot;              8 - re-initialize all registers&quot;<<endl<<endl;
     }
    }
   }
   DoOperation(registers,operation,operand1,operand2,stop);
  }
 }
 else
 {
  //error message if input file could not be opened
  cout<<&quot;***  ERROR :: Could not open file, registers.inp&quot;<<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<<&quot;***  ERROR :: Register &quot;<<oprnd2<<&quot; is initialized to zero.&quot;<<endl
       <<&quot;              Cannot divide by zero. Operation not performed.&quot;<<endl
       <<endl;
  }
  else
  {
   switch (oper)
   {
    case 0: regs[oprnd1]=regs[oprnd1]+regs[oprnd2];
            cout<<&quot;OPERATION 0 - Add Two Register Values To One Another&quot;<<endl;
            opt='+';
            break;
    case 1: regs[oprnd1]=regs[oprnd1]*regs[oprnd2];
            cout<<&quot;OPERATION 1 - Multiply Two Register Values With One Another&quot;
                <<endl;
            opt='*';
            break;
    case 2: regs[oprnd1]=regs[oprnd1]-regs[oprnd2];
            cout<<&quot;OPERATION 2 - Subtract One Register Value From Another&quot;
                <<endl;
            opt='-';
            break;
    case 3: regs[oprnd1]=regs[oprnd1]/regs[oprnd2];
            cout<<&quot;OPERATION 3 - Divide One Register Value By Another&quot;<<endl;
            opt='/';
            break;
    case 4: regs[oprnd1]=regs[oprnd1]%regs[oprnd2];
            cout<<&quot;OPERATION 4 - Modulus Determination Of One Register Value On&quot;
                <<&quot; Another&quot;<<endl;
            opt='%';
            break;
   }
   cout<<&quot;              Register &quot;<<oprnd1<<&quot; &quot;<<opt<<&quot; Register &quot;<<oprnd2
       <<&quot; = &quot;<<regs[oprnd1]<<endl<<endl;
  }
 }
 else
 {
  if ((oper==5)&&(stp==0))
  {
   regs[oprnd1]=oprnd2;
   cout<<&quot;OPERATION 5 - Store a Value to a Specified Register&quot;<<endl
       <<&quot;              A value of &quot;<<oprnd2<<&quot; has been stored to Register &quot;
       <<oprnd1<<endl<<endl;
  }
  else
  {
   if ((oper==6)&&(stp==0))
   {
    cout<<&quot;OPERATION 6 - Print Specified Register Value&quot;<<endl
        <<&quot;              Register  &quot;<<oprnd1<<&quot; ==> &quot;<<regs[oprnd1]<<endl<<endl;
   }
   else
   {
    if (oper==7)
    {
     cout<<&quot;OPERATION 7 - Print All Register Values&quot;<<endl<<endl
         <<&quot;              REGISTER            VALUE&quot;<<endl
         <<&quot;              =========================&quot;<<endl
         <<&quot;                 0        ==>      &quot;<<regs[0]<<endl
         <<&quot;                 1        ==>      &quot;<<regs[1]<<endl
         <<&quot;                 2        ==>      &quot;<<regs[2]<<endl
         <<&quot;                 3        ==>      &quot;<<regs[3]<<endl
         <<&quot;                 4        ==>      &quot;<<regs[4]<<endl
         <<&quot;                 5        ==>      &quot;<<regs[5]<<endl
         <<&quot;                 6        ==>      &quot;<<regs[6]<<endl
         <<&quot;                 7        ==>      &quot;<<regs[7]<<endl
         <<&quot;                 8        ==>      &quot;<<regs[8]<<endl
         <<&quot;                 9        ==>      &quot;<<regs[9]<<endl
         <<&quot;                10        ==>      &quot;<<regs[10]<<endl<<endl;
    }
    else
    {
     if (oper==8)
     {
      for (oprnd1=0; oprnd1<10; oprnd1++)
      {
       regs[oprnd1]=0;
      }
      cout<<&quot;OPERATION 8 - Re-initialize All Registers&quot;<<endl
          <<&quot;              All registers have been re-initialized to 0.&quot;<<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<<&quot;***  ERROR :: Unable to complete last operation.&quot;<<endl
           <<&quot;              No data left in input file to do so.&quot;<<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<<&quot;***  ERROR :: Unable to complete last operation.&quot;<<endl
            <<&quot;              Incomplete data to do so.&quot;<<endl
            <<endl;
       }
      }
     }
    }
   }
  }
 }
 return;
} //end of DoOperation
 
 
//clears the screen (part of conio.h)
void ClearScreen()
{
 printf(&quot;%c[2J%c[%i;%iH&quot;,27,27,1,1);
 cout<<endl<<endl;
} //end of ClearScreen
 
 
//used to hault termination of program
void Pause()
{
 int exit=0;
 
 cout<<endl;
 cout<<&quot;Enter any value to exit:  &quot;;
 cin>>exit;
} //end of Pause
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top