Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Floating point error in turbo c++

viveknvivekn Posts: 2Member
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[code] Floating point error:domain Abnormal program termination[/code]

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

[code]#include
#include
#include
#include
#include

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

for (int q=0;q>choice;




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

cout<<" Instructions
-------------


1.Choose whether to encrypt or decrypt a file ";
cout<<"
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<<"
3.Enter the key to encrypt/decrypt the file with(your file will be encrypted on the basis of the key entered).";
cout<<"
4.The output file will be your encrypted/decrypted file";
cout<<"



Press 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<<"
";
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<<"
";







outfile.open(ofname);


switch(choice) {

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

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


Key[i]=getch();


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

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

fflush(stdin);
cout<<"
";

while (infile) {

while(infile){

infile.get(ch);
if(!infile){
cout<<"
The 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] == '
')
break;
cout<<"*";

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

while(infile) {

while(infile) {

infile.get(ch);
if(!infile)
{
cout<<"
The 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<<"


Press any key to return ......... " ;

getch();
goto start;

return 0;
}
[/code]
«1

Comments

  • LundinLundin Posts: 3,711Member
    [blue]I don't know what is causing the error, but I can give you some feedback on the code.[/blue]


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

    [blue]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++.[/blue]


    : [code]: #include
    : #include
    : #include
    : #include
    : #include

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



    : t=pow(2,t);

    [blue]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;
    }[/blue]



    : goto start;

    [blue]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.[/blue]




    : fflush(stdin);

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

  • viveknvivekn Posts: 2Member
    thanks,i made some tweaks to get that exponent loop working and thanks for your suggestions on fflush too
  • betanumericbetanumeric Posts: 6Member
    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++?
  • AelphaeisAelphaeis Posts: 4Member
    Dev - C++
    or
    Visual C 07 (or higher)
  • LundinLundin Posts: 3,711Member
    : 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
  • ankit_chaankit_cha Posts: 1Member
    #include
    #include
    #include
    #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("
    Input three initial points
    ");
    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("

    Root By Muller'S Method

    ");
    printf("Root=%f
    ",x4);
    printf("
    ");
    }
    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!!!!!
  • inspokeninspoken Posts: 9Member
    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
  • inspokeninspoken Posts: 9Member
    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
  • inspokeninspoken Posts: 9Member
    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
  • inspokeninspoken Posts: 9Member
    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
«1
Sign In or Register to comment.