C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28645
Number of posts: 94661

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Floating point error in turbo c++ Posted by vivekn on 2 Feb 2009 at 9:29 AM
I wrote this code for a school project,since i use vista and dos doesn't work properly on it ,i normally use the emulator DOSBOX.The problem is this program runs well on DOSBOX but not on the normal DOS platform. It gives an error
 Floating point error:domain Abnormal program termination


Here's the code,its on text file encryption.My school requires me to use the ancient "turbo c++ 3.0"

#include<string.h>
#include<stdio.h>
#include<fstream.h>
#include<conio.h>
#include<math.h>

 int strtoken(char k[]) {
    int N = strlen(k);
    int sum = 0   ;

            for (int q=0;q<N;q++)
                sum+= (int) k[q] ;

    int tok = sum%255;

  return tok;
 }

int strtoken2(char k1[]) {

   int N1 = strlen(k1);
   int sum1 = 0,t=0;
        for (int q1=0;q1<N1;q1++)
        {
            t = (int) k1[q1];
            t=pow(2,t);
            sum1+= (t%255) ;
            }

  int tok = sum1%255;

  return tok;
}


 int main() {
     clrscr();
     int choice,num;
     char ifname[20],ofname[20],Key[20],ch;

start:
			  clrscr();
    cout<<"\n------------------------------------------------------------------------------\n";
	cout<<"Texcrypt 1.0.\n";
	cout<<"\n";
	cout<<"------------------------------------------------------------------------------\n\n";
	cout<<"\t\t\t*-------------------*\n"<<"\t\t\t| 1) Encrypt. |\n"<<"\t\t\t| 2) Decrypt. |\n"<<"\t\t\t| 3) Help.    |\n\t\t\t| 4) Exit.    |\n"<<"\t\t\t*-------------------*\n";
	cout<<"\nEnter your choice: ";

	cin>>choice;




if(choice==3) {
 clrscr();

cout<<" Instructions \n ------------- \n\n\n 1.Choose whether to encrypt or decrypt a file ";
cout<<"\n 2.Enter the input and output filenames (if the file is in same directory as texcrypt ,if not,then enter the full path of the file)";
cout<<"\n 3.Enter the key to encrypt/decrypt the file with(your file will be encrypted on the basis of the key entered).";
cout<<"\n 4.The output file will be your encrypted/decrypted file";
cout<<"\n\n\n\nPress any key to return ...... ";

getch();

goto start;






}






else if(choice==4)
	  return 0;
	      char text[3][20] = {"","encrypted","decrypted"};

	clrscr();
		cout<<"Enter input filename : ";
		gets(ifname);
		fflush(stdin);
		cout<<"\n";
		int i;

		ifstream infile("");
		ofstream outfile("");
		   infile.open(ifname);

		if(!infile) {          // if file is not found
		cout<<"Error:File not found";
		goto end;
		}

		cout<<"Enter output filename : ";gets(ofname);fflush(stdin);
		cout<<"\n";







		   outfile.open(ofname);


		       switch(choice) {

                        case 1:cout<<"Enter key: ";

                                for(i=0;;i++)
                                {


                                        Key[i]=getch();


				    if (Key[i] == '\r')
					break;
		       cout<<"*";
                                    }
                                Key[i] = '\0';

// I think the error is somewhere overhere
			    int token= strtoken(Key);
			    int token2= strtoken2(Key);

			  fflush(stdin);
			      cout<<"\n";

				     while (infile) {

				      while(infile){

					infile.get(ch);
					if(!infile){
					  cout<<"\nThe file was successfully encrypted.";
					  getch();
					  goto end;
					  }

					ch=char((ch+token-(token2))%255);

					outfile.put(ch);
					   }
					   }

			    break;


						  case 2:
				cout<<"Enter key: ";

				for(i=0;;i++){
				    Key[i]=getch();


				    if (Key[i] == '\r')
					break;
					cout<<"*";

						}
				Key[i] = '\0';
				cout<<"\n";
				     fflush(stdin);
                                     int dtoken=strtoken(Key);
                                     int dtoken2=strtoken2(Key);

                                     while(infile) {

                                     while(infile) {

                                          infile.get(ch);
                                          if(!infile)
                                          {
                                                cout<<"\nThe file was successfully decrypted.";
                                                getch();
                                                goto end;
                                                }
                                                ch=char(((ch)-dtoken+(dtoken2))%255);
                                                outfile.put(ch);

                                           }
                                           }
                                    break;
								 }
								//end switch
								end:
								 infile.close();
								 outfile.close();

								 cout<<"\n\n\nPress any key to return ......... "          ;

								 getch();
								 goto start;

								   return 0;
								   }

Report
Re: Floating point error in turbo c++ Posted by Lundin on 3 Feb 2009 at 1:22 AM
I don't know what is causing the error, but I can give you some feedback on the code.


: Here's the code,its on text file encryption.My school requires me
: to use the ancient "turbo c++ 3.0"

That is a poor school then. You should download a free, modern compiler and use it on the side, so that you get chance to learn real standard C++.


:
: #include<string.h>
: #include<stdio.h>
: #include<fstream.h>
: #include<conio.h>
: #include<math.h>

 These header declarations aren't valid in C++. This is non-standard code that only works in TC.


 
:             t=pow(2,t);

I suspect this is what makes the error appear, since you aren't using float numbers elsewhere in the code. As a work-around, you could code an integer version of that function, it is quite simple to do.

Here is one you can use:

(change uint32 to unsigned long and uint16 to unsigned int)

uint32 gpfunc_intpow (uint16 base, uint16 exp)
{
  uint32 sum;

  if(exp == 0)                                   /* special case: x times 0 */
  {
    sum = 1;
  }
  else
  {
    uint16 i;

    sum = base;

    for(i=1; i<exp; i++)
    {
      sum *= base;
    }
  }

  return sum;
}



: goto start;

You should avoid using goto, it is frowned upon by a majority of the programming world, and can be replaced with loops, that are easier to read.




: 		fflush(stdin);

This is not standard C nor standard C++ and will not work on standard compilers. You can only fflush stdout, never stdin.

Report
Re: Floating point error in turbo c++ Posted by vivekn on 9 Feb 2009 at 9:05 AM
thanks,i made some tweaks to get that exponent loop working and thanks for your suggestions on fflush too
Report
Re: Floating point error in turbo c++ Posted by betanumeric on 11 Mar 2009 at 9:04 PM
I'm getting the same problem too. Say, what sort of "modern compiler" would you recommend if I'm to give up using Turbo C++?
Report
Re: Floating point error in turbo c++ Posted by Aelphaeis on 11 Mar 2009 at 11:22 PM
Dev - C++
or
Visual C 07 (or higher)
Report
Re: Floating point error in turbo c++ Posted by Lundin on 12 Mar 2009 at 3:44 AM
: I'm getting the same problem too. Say, what sort of "modern
: compiler" would you recommend if I'm to give up using Turbo C++?


Free compilers:
Dev C++ (gcc) www.bloodshed.net/dev/devcpp
Microsoft Visual Studio Express
Borland Turbo C++ Builder 2006 www.turboexplorer.com

Commerical compilers:
Embarcadero Builder 2009 (former Borland)
Microsoft Visual Studio 2008
Report
Re: Floating point error in turbo c++ Posted by ankit_cha on 16 Aug 2009 at 1:28 AM
#include<math.h>
#include<stdio.h>
#include<conio.h>
#define EPS 0.000001
void main()
{
float F(float x);
float x1,x2,x3,x4,f1,f2,f3,f4,h1,h2,d1,d2,a0,a1,a2,h;
clrscr();
printf("\nInput three initial points \n");
scanf("%f %f %f",&x1,&x2,&x3);
f1=F(x1);
f2=F(x2);
f3=F(x3);
begin:
h1=x1-x3;
h2=x2-x3;
d1=f1-f2;
d2=f2-f3;
a0=f3;
a1=(d2*h1*h1-d1*h2*h2)/(h1*h2*(h1-h2));
a2=(d1*h2-d2*h1)/(h*h2*(h1-h2));
if(a1>0.0)
h=(-2.0*a0)/(a1+sqrt(a1*a1-4*a2*a0));
else
h=(-2.0*a0)/(a1-sqrt(a1*a1-4*a2*a0));
x4=x3+h;
f4=F(x4);
if(f4<=EPS)
{
printf("\n\nRoot By Muller'S Method\n\n");
printf("Root=%f\n",x4);
printf("\n");
}
else
{
x1=x2;
x2=x3;
x3=x4;
f1=f2;
f2=f3;
f3=f4;
goto begin;
}
}
float F(float x)
{
float f;
f=(x*x*x)+(2*x*x)+(10*x)-20;
return (f);
}

hey compiler is showing me the same error please help me out!!!!!
Report
Re: Floating point error in turbo c++ Posted by inspoken on 4 Mar 2013 at 7:22 AM
you need to protect t=pow(2,t) before calling it. You also might want to cast t into a double not an int for instance
double tmp=(double)t;
if(fabs(tmp)>1000.0)
return some_error_code;
tmp=pow(2.0,tmp);
if(fabs(tmp)<32000.0)
t=(int)tmp;
else
return some_other_type_of_error
Report
Re: Floating point error in turbo c++ Posted by inspoken on 4 Mar 2013 at 7:24 AM
you need to protect t=pow(2,t) before calling it. You also might want to cast t into a double not an int for instance
double tmp=(double)t;
if(fabs(tmp)>1000.0)
return some_error_code;
tmp=pow(2.0,tmp);
if(fabs(tmp)<32000.0)
t=(int)tmp;
else
return some_other_type_of_error
Report
Re: Floating point error in turbo c++ Posted by inspoken on 4 Mar 2013 at 7:26 AM
you need to protect t=pow(2,t) before calling it. You also might want to cast t into a double not an int for instance
double tmp=(double)t;
if(fabs(tmp)>1000.0)
return some_error_code;
tmp=pow(2.0,tmp);
if(fabs(tmp)<32000.0)
t=(int)tmp;
else
return some_other_type_of_error
Report
Re: Floating point error in turbo c++ Posted by inspoken on 4 Mar 2013 at 7:39 AM
you need to protect t=pow(2,t) before calling it. You also might want to cast t into a double not an int for instance
double tmp=(double)t;
if(fabs(tmp)>1000.0)
return some_error_code;
tmp=pow(2.0,tmp);
if(fabs(tmp)<32000.0)
t=(int)tmp;
else
return some_other_type_of_error
Report
Re: Floating point error in turbo c++ Posted by inspoken on 4 Mar 2013 at 7:41 AM
you need to protect t=pow(2,t) before calling it. You also might want to cast t into a double not an int for instance
double tmp=(double)t;
if(fabs(tmp)>1000.0)
return some_error_code;
tmp=pow(2.0,tmp);
if(fabs(tmp)<32000.0)
t=(int)tmp;
else
return some_other_type_of_error
Report
Re: Floating point error in turbo c++ Posted by inspoken on 4 Mar 2013 at 7:43 AM
you need to protect t=pow(2,t) before calling it. You also might want to cast t into a double not an int for instance
double tmp=(double)t;
if(fabs(tmp)>1000.0)
return some_error_code;
tmp=pow(2.0,tmp);
if(fabs(tmp)<32000.0)
t=(int)tmp;
else
return some_other_type_of_error
Report
Re: Floating point error in turbo c++ Posted by inspoken on 4 Mar 2013 at 7:51 AM
you need to protect t=pow(2,t) before calling it. You also might want to cast t into a double not an int for instance
double tmp=(double)t;
if(fabs(tmp)>1000.0)
return some_error_code;
tmp=pow(2.0,tmp);
if(fabs(tmp)<32000.0)
t=(int)tmp;
else
return some_other_type_of_error
Report
Re: Floating point error in turbo c++ Posted by inspoken on 4 Mar 2013 at 7:53 AM
you need to protect t=pow(2,t) before calling it. You also might want to cast t into a double not an int for instance
double tmp=(double)t;
if(fabs(tmp)>1000.0)
return some_error_code;
tmp=pow(2.0,tmp);
if(fabs(tmp)<32000.0)
t=(int)tmp;
else
return some_other_type_of_error
Report
Re: Floating point error in turbo c++ Posted by inspoken on 4 Mar 2013 at 7:54 AM
you need to protect t=pow(2,t) before calling it. You also might want to cast t into a double not an int for instance
double tmp=(double)t;
if(fabs(tmp)>1000.0)
return some_error_code;
tmp=pow(2.0,tmp);
if(fabs(tmp)<32000.0)
t=(int)tmp;
else
return some_other_type_of_error



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.