Compiling original cpp for WinOS on MacOS

#1

I’m trying to compile a fairly complex source code (that I did not write) on my Mac. Using Xcode. I have imported several libraries with home brew. The program is supposed to open a window and prompt for user data. I commented out this line in the header: #include <conio.h>. I have #include <curses.h> in the program. The compile time errors are gone, but it will not fully link.

Here are the errors:
image

Maybe something has to replace #include <conio.h>?? It seems to be finding ncurses, but tripping over stdscr and wgetch? Which are not called in the program explicitly.

#2

Hi, both problems seem to be showing up in QSRS::header_read()so it might be usefull if you post that function in order to tell you what to do. cheers

#5

Here is the header_read() code. Thanks for the suggestion roy

#6

Hi, please post code inlined in the message not as a picture, it is easier to read and or copy. (just paste, select what you pasted and press the </> button in the button bar above the text field.)

You can get rid of that call to

getch(); 
exit(1);

as it is just waiting for a key press in order to finish the programs execution.
All the rest seems to be just fine.

#7

Thanks again roy.

clean code paste here:

void QSRS::header_read(void)
{

	int numBytes=300;

    char string_a[300];

	long j=0;

	int kflag;

	double tb=-1.0e+90;

	double dtmin=1.0e+90;
	double dtmax=-dtmin;

	printf("\n");


	while(fgets(string_a,numBytes,pFile[0] )>(void *)0)
	{
		kflag=0;

//		printf("j=%ld.  %s\n",j,string_a);

//***************************************************************


        line_test(kflag,string_a);

//***************************************************************

		if(kflag==1)
		{
			printf("H: %s ",string_a);
		}
		else
		{
			i=number_lines;

			if(strrchr(string_a,',' ) != NULL )
			{
				sscanf(string_a,"%lf, %lf", &t,&aa );
				yin.push_back(aa);
			}
			else
			{
				sscanf(string_a,"%lf %lf", &t,&aa );
                yin.push_back(aa);
			}
            if(number_lines==0)
			{
				t1=t;
			}
			if(t<tb && number_lines>=1)
			{
				printf("\n Error: time values must ascend. \n");


				printf("\n %s \n",string_a);

			    printf("\n t=%11.7e \t tb=%11.7e \n",t,tb);


				printf("\n Press any key to exit  \n");
//		advised to remove, not needed		getch();

//		advised to remove, not needed		exit(1);
			}

			if(number_lines>=1)
			{
				if(dtmin>(t-tb))
				{
					dtmin=(t-tb);
				}
				if(dtmax<(t-tb))
				{
					dtmax=(t-tb);
				}
			}

			tb=t;

			number_lines++;

		}
		j++;

	}type or paste code here
#8

did it work with out those calls?

#9

No, no change with those items commented.

#10

Maybe I need to share the full code…As I mentioned in the first post, the program is intended to prompt the user and it works fine (presumably) in windows. This may be what is still not right for the MacOS. The open_read only contains some of that keyboard input content.

Here’s all of it. I can also write to the program’s author (whose contact info I removed from the comments here. However, he wrote the routine from WinOS and may not have the insight and/or desire to boot it up for Mac)

#include <math.h>
#include <stdio.h>
#include <string>
#include <string.h>
#include <stdlib.h>
#include <curses.h>

//#include <conio.h>
#include <vector>

using namespace std;

class QSRS
{
    private:
        double peak;
        double kurtosis;

    public:
        void files(void);
        void header_read(void);
        void line_test(int &kflag,const std::string & str);
        void initialize_filter_coefficients(void);
        void sdof_response(void);
        void print_response(void);
        void initialize_arrays(void);
        void select_parameters(void);
        void initialize_frequencies();
        void initialize(void);

        vector<float> yin;

        vector<double> a1;
        vector<double> a2;
        vector<double> b1;
        vector<double> b2;
        vector<double> c2;
        vector<double> sm_a1;
        vector<double> sm_a2;
        vector<double> sm_b1;
        vector<double> sm_b2;
        vector<double> sm_b3;
        vector<double> sm_c2;

        vector<double> fn;

        vector<double> tmax;
        vector<double> tmin;
        vector<double> xb;
        vector<double> xbb;
        vector<double> x;
        vector<double> xmax;
        vector<double> xmin;
        vector<double> yb;
        vector<double> ybb;
        vector<double> rd;
        vector<double> rdb;
        vector<double> rdbb;


        double t, t1, tb;

        double alpha;
        double aa;
        double E,K,C,S,Sp;
        double cosd;
        double sind;
        double damp;
        double domegadt;
        double dt;

        double omega, omegad;
        double rdmax,rdmin,rdRMS;
        double RMS;
        double sr;

        double stddev;
        double mean;

        double peak_f,peak_m;

        double y;

        long idamp;
        long last;
        long i,j;
        long iunit;
        long mh;
        long number_lines;
        long numBytes;

        int algor;
        int ire;
        int mflag;
        int ioct;
        int jnum;

        FILE *pFile[4];
        char filename[4][FILENAME_MAX];
};

void QSRS::initialize(void)
{
    mflag=0;
}

const double pi=atan2(0.,-1.);


int main( int argc, char *argv[] )
{

    QSRS ar;

    ar.initialize();

	if(argc > 1 )
	{
			printf("\n\n  argc = %d ", argc );
			printf("\n argv[0] %s ", argv[0] );
			printf("\n argv[1] %s ", argv[1] );

			strcpy (ar.filename[0], argv[1] );

			ar.mflag=1;

	}

	
	printf("\n This program calculates the response of a");
	printf("\n single-degree-of-freedom system subjected");
	printf("\n to base excitation.");

	ar.files();

/******************************** initialize arrays ******************/

    ar.initialize_arrays();

/********************* intialize frequency array *********************/

	ar.select_parameters();

/********************* determine delta t *****************************/

	ar.header_read();

	ar.initialize_frequencies();

/*********************** initialize filter coefficients ***************/

    ar.initialize_filter_coefficients();

/********************** response calculation *******************/

    ar.sdof_response();

    ar.print_response();



   printf("\n Press any key to exit.\n");

   getch();

   exit(1);
}
void QSRS::print_response(void)
{


   strcpy(filename[1], "srs1.grp");
   strcpy(filename[3], "srs2.grp");

   pFile[1]=fopen(filename[1],"w");
   pFile[3]=fopen(filename[3],"w");

   peak_m=0.;
   peak_f=0.;


	for(j=0; j<jnum; ++j)
	{

		xmin[j]=fabs(xmin[j]);

        fprintf(pFile[3],"%10.5e \t %10.5e \t %10.5e \n",fn[j],xmax[j],xmin[j]);

		if(xmax[j]<fabs(xmin[j]))
		{
			xmax[j]=fabs(xmin[j]);
		}

        fprintf(pFile[1],"%10.5e \t %10.5e \n",fn[j],xmax[j]);
//         printf("%10.5e \t %10.5e \n",fn[j],xmax[j]);

		if(xmax[j]>peak_m)
		{
			peak_m=xmax[j];
			peak_f=fn[j];
		}
   }

   fclose(pFile[1]);
   fclose(pFile[3]);

   printf ( "\n\n Absolute peak =  %8.4g G at  %8.4g Hz \n",peak_m,peak_f);
   printf ( "\n\n Output files: \n" );

   printf ( "\n srs.grp  - natural frequency (Hz) " );
   printf ( "\n             and peak absolute acceleration (G) " );

   printf ( "\n srs2.grp - natural frequency (Hz)  " );
   printf ( "\n             and peak positive acceleration (G)  ");
   printf ( "\n             and peak negative acceleration (G)  \n");

   printf ( "\n Please call the *.grp files into your own graphics program. " );
   printf ( "\n Note that the data files are tab delimited. \n");

}

void QSRS::files(void)
{

	/******************************** open files *************************/

	if(mflag==0)
	{
   		printf( "\n\n The base input file must contain two columns: \n");
		printf( " time(sec)  accel(G)    \n");

		printf( "\n Input filename \n");
		scanf("%s",filename[0]);

	}

    pFile[0] = fopen(filename[0], "rb");

	while(pFile[0] == NULL )
	{

		printf("\n Failed to open file: %s \n", filename[0]);

		printf("\n Please enter the input filename: \n");

		scanf("%s",filename[0]);

		pFile[0] = fopen(filename[0], "rb");

	}

	printf(" File: %s opened. \n", filename[0]);


}

void QSRS::header_read(void)
{

	int numBytes=300;

    char string_a[300];

	long j=0;

	int kflag;

	double tb=-1.0e+90;

	double dtmin=1.0e+90;
	double dtmax=-dtmin;

	printf("\n");


	while(fgets(string_a,numBytes,pFile[0] )>(void *)0)
	{
		kflag=0;

//		printf("j=%ld.  %s\n",j,string_a);

//***************************************************************


        line_test(kflag,string_a);

//***************************************************************

		if(kflag==1)
		{
			printf("H: %s ",string_a);
		}
		else
		{
			i=number_lines;

			if(strrchr(string_a,',' ) != NULL )
			{
				sscanf(string_a,"%lf, %lf", &t,&aa );
				yin.push_back(aa);
			}
			else
			{
				sscanf(string_a,"%lf %lf", &t,&aa );
                yin.push_back(aa);
			}
            if(number_lines==0)
			{
				t1=t;
			}
			if(t<tb && number_lines>=1)
			{
				printf("\n Error: time values must ascend. \n");


				printf("\n %s \n",string_a);

			    printf("\n t=%11.7e \t tb=%11.7e \n",t,tb);


				printf("\n Press any key to exit  \n");
//		advised to remove, not needed		getch();

//		advised to remove, not needed		exit(1);
			}

			if(number_lines>=1)
			{
				if(dtmin>(t-tb))
				{
					dtmin=(t-tb);
				}
				if(dtmax<(t-tb))
				{
					dtmax=(t-tb);
				}
			}

			tb=t;

			number_lines++;

		}
		j++;

	}
	rewind(pFile[0]);

	dt=(t-t1)/double(number_lines-1);

	sr=1./dt;

	printf("\n Time span: %10.5g to %10.5g sec \n",t1,t);

	printf("\n dtmin= %11.6g sec	   ",dtmin);
	printf("\n dt   = %11.6g sec	   ",dt);
	printf("\n dtmax= %11.6g sec	 \n",dtmax);

	printf("\n srmin= %11.6g samples/sec	   ",1./dtmax);
	printf("\n sr   = %11.6g samples/sec	   ",sr);
	printf("\n srmax= %11.6g samples/sec	 \n",1./dtmin);


    if( dtmin < 0.99 *dtmax)
	{

	   int nsr;



	   printf("\n *** Time Step Warning ***   \n");

       printf("\n dtmax = %8.4g   dtmin = %8.4g \n",dtmax,dtmin);

	   printf("\n The time step must be constant. \n");

       printf("\n Enter new sample rate?  1=yes  2=no \n");

	   scanf("%d",&nsr);



	   if(nsr==1)
       {

			printf("\n\n Assume constant sample rate. Enter sample rate (samples/sec) \n");

			scanf("%lf",&sr);

			dt=1./sr;


			printf("\n\n sr   = %11.6g samples/sec	   ",sr);

			printf("\n Nyquist Freq = %lf Hz \n",sr/2.);

			printf("\n dt   = %11.6g sec	   \n",dt);

	   }
	   else
	   {
			printf("\n Press any key to exit \n");

			getch();

			exit(1);

	   }
	}

	last=number_lines;

	printf("\n t1=%9.5g sec \n",t1);
    printf("\n\n number of input points = %ld \n",last);
	printf("\n\n  damp=%g \n",damp);

}
void QSRS::line_test(int &kflag,const std::string & str)
{

//  detect header lines

//		printf("j=%ld.  %s\n",j,string);

		if (std::string::npos != str.find_first_of("Ee"))
		{
		    std::size_t f1=str.find("e+");
		    std::size_t f2=str.find("e-");
		    std::size_t f3=str.find("E+");
		    std::size_t f4=str.find("E-");

		    if(f1==std::string::npos && f2==std::string::npos && f3==std::string::npos && f4==std::string::npos)
		    {
		        kflag=1;
		    }
		}

        if (std::string::npos != str.find_first_of("ABCDFGHIJKLMNOPQRSTUVWXYZabcdfghijklmnopqrstuvwxyz"))
        {
             kflag=1;
        }
        if (std::string::npos != str.find_first_of("!@#$%^&*/<>?()=;"))
        {
             kflag=1;
        }
        if (std::string::npos == str.find_first_of("0123456789"))
        {
             kflag=1;
        }
}
void QSRS::initialize_arrays(void)
{
   number_lines=0;

   numBytes=300;

   rdmax=0.;
   rdmin=0.;
   RMS=0.;
   rdRMS=0.;
}
void QSRS::sdof_response()
{
   long iend;
   double yy;


//   printf("\n***  jnum=%ld  last=%ld***\n",jnum,last);

   for(long j=0;j<jnum;j++)
   {

        if(ire==1)
        {
            iend = last + long(2./(dt*fn[j]));  // primary+2*residual
        }
        else
        {
            iend = last;  // primary
        }

        i=0;

        while( i < (iend-1)  )
        {

            if(i<last)
            {
                yy=yin[i];
            }
            else
            {
                yy=0.;
            }

            if(algor==1)
            {
               x[j]=     (  a1[j]* xb[j]
                          +a2[j]*xbb[j]
			              +b1[j]*yy
		                  +b2[j]*yb[j] );
            }
            else
            {
               x[j]=      (   sm_a1[j]* xb[j]
                            +sm_a2[j]*xbb[j]
			                +sm_b1[j]*yy
		                    +sm_b2[j]*yb[j]
		                    +sm_b3[j]*ybb[j] );
            }

/*
            rd[j]=       ( a1[j]* rdb[j]
			           +a2[j]*rdbb[j]
		               +c2[j]*yb[j] );

            rd[j]*=alpha;
*/

            if( fabs(x[j])>1.0e+12)
            {
                printf("\n x break \n");
                break;
            }
/*
            if(fabs(rd[j])>1.0e+12)
            {
                printf("\n rd break \n");
                break;
            }
*/
//            printf(" %ld %ld %ld %8.4g %8.4g  \n",i,iend,j,x[j],xmax[j]);

            if(x[j] > xmax[j])
	        {
	            xmax[j]=x[j];
	        }
            if(x[j] < xmin[j])
	        {
	            xmin[j]=x[j];
	        }

             xbb[j]=xb[j];
              xb[j]= x[j];


//            rdbb[j]=rdb[j];
//	         rdb[j]= rd[j];

             ybb[j]=yb[j];
	         yb[j]=yy;

 //           yin[i]=x[j];
           i++;
        }

   }

}
void QSRS::select_parameters()
{
    printf("\n\n Select alogrithm: \n");

	printf("\n 1=Kelly-Richman  2=Smallwood \n");
    scanf("%d",&algor);

	printf("\n\n Enter starting frequency (Hz) \n");
	scanf("%lf",&aa);
	fn.push_back(aa);

	printf("\n\n Select octave spacing:  \n");
	printf("   1=1/3   2=1/6   3=1/12   4=1/24 \n");
    scanf("%d",&ioct);


   	printf("\n Enter damping format. \n 1= damping ratio   2= Q \n");
	scanf("%ld",&idamp);

	if(idamp == 1 )
	{
	  printf("\n Enter damping ratio (typically 0.05) ");
      scanf("%lf",&damp);
	}
	else
    {
	  printf("\n Enter amplification factor Q (typically 10) ");
      scanf("%lf",&damp);
	  damp = 1./(2.*damp);
	}

    printf("\n Include residual?   ");
    printf("\n   1=yes  2=no \n");
	scanf("%d",&ire);


//	printf("\n Enter output relative displacement unit. \n 1= inch   2= mm \n");

//	scanf("%ld",&iunit);

    iunit=1;

	if(iunit==1)
	{
		alpha=386.;
	}
	else
	{
		alpha=9810.;
	}

}
void QSRS::initialize_filter_coefficients()
{
    for(int j=0;j<jnum;j++)
    {

      omega=2.*pi*fn[j];
      omegad=omega*sqrt(1.-pow(damp,2));
      cosd=cos(omegad*dt);
      sind=sin(omegad*dt);
      domegadt=damp*omega*dt;
      a1.push_back( 2.*exp(-domegadt)*cosd );
      a2.push_back( -exp(-2.*domegadt) );
      b1.push_back( 2.*domegadt );

      aa=( (omega/omegad)*(1.-2.*pow(damp,2))*sind -2.*damp*cosd );

      b2.push_back( aa*omega*dt*exp(-domegadt) );

	  c2.push_back( -(dt/omegad)*exp(-domegadt)*sind );


	  if(algor==2)
      {
        E=exp(-damp*omega*dt);
        K=omegad*dt;
        C=E*cos(K);
        S=E*sin(K);

        Sp=S/K;

        sm_a1.push_back( 2*C );
        sm_a2.push_back( -pow(E,2) );

        sm_b1.push_back( 1.-Sp );
        sm_b2.push_back( 2.*(Sp-C) );
        sm_b3.push_back( pow(E,2)-Sp );

//        printf(" %5.3g %5.3g %5.3g %5.3g %5.3g \n",sm_a1[j],sm_a2[j],sm_b1[j],sm_b2[j],sm_b3[j]);

      }

    }
}

void QSRS::initialize_frequencies()
{
    double bw;

    double scc = 0.0;

   if(ioct==1)
   {
      scc=1./3.;
   }
   if(ioct==2)
   {
      scc=1./6.;
   }
   if(ioct==3)
   {
      scc=1./12.;
   }
   if(ioct==4)
   {
      scc=1./24.;
   }


   jnum=0;

   j=1;

   while(1)
   {

        fn.push_back(fn[0]*pow(2.,j*scc));

         j++;
         jnum++;

		if( fn.back() > sr/8.)break;

   }

   for(j=0;j<jnum;j++)
   {
       xmax.push_back(0.);
       xmin.push_back(0.);
       tmax.push_back(0.);
       tmin.push_back(0.);

		 x.push_back(0.);
		xb.push_back(0.);
       xbb.push_back(0.);
	    yb.push_back(0.);
       ybb.push_back(0.);
        rd.push_back(0.);
       rdb.push_back(0.);
      rdbb.push_back(0.);

   }

}

#11

Well, gosh. The issue is now solved. It turns out the code’s author had a newer version in Python. Thanks Roy!

1 Like