468,512 Members | 971 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

Floating type problem with turbo c++

Parul Bagadia
188 100+
I just wrote a code for myself to clear certain things...........
When i made this program to execute in turbo c++ it didnt.....
it showed following error.
I have pasted the output after code is over.
But when i executed the same code in microsoft visual c++; it worked without showing any errors.
Can somebody tell me why that happened.
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<conio.h>
  3. void assign();
  4. void check();
  5. float c;
  6. void main()
  7. {
  8.  int choise;
  9.  do{
  10.     printf("\nEnter your choise from the given menu:\n 1.Assign\n 2.Check \n 3.Exit\n");
  11.     scanf("\n%d",&choise);
  12.      switch(choise)
  13.      {
  14.       case 1:
  15.          assign();
  16.          break;
  17.       case 2:
  18.          check();
  19.          break;
  20.       case 3:
  21.          break;
  22.      }
  23.    }while(choise!=3);
  24. }
  25. void check()
  26. {
  27. float i;
  28. printf("%f",&c);
  29. i=c+1;
  30. printf("\n%f",i);
  31. printf("\n%f",&i);
  32. }
  33. void assign()
  34. {
  35.  printf("\n Enter the value of c:\n");
  36.  scanf("\n%f",&c);
  37. }
  38.  
Enter your choise from the given menu:
1.Assign
2.Check
3.Exit
1

Enter the value of c:
2

Enter your choise from the given menu:
1.Assign
2.Check
3.Exit
2
Floating point error: Domain.
Abnormal program termination

Enter your choise from the given menu:
1.Assign
2.Check
3.Exit
2
Floating point error: Domain.
Abnormal program termination

Enter your choise from the given menu:
1.Assign
2.Check
3.Exit
1

Enter the value of c:
33.4

Enter your choise from the given menu:
1.Assign
2.Check
3.Exit
2
Floating point error: Domain.
Abnormal program termination

Enter your choise from the given menu:
1.Assign
2.Check
3.Exit
Mar 4 '08 #1
1 7393
Laharl
849 Expert 512MB
Since c is never initialized with a value, it simply holds whatever was left over there from the last time that memory was used. Thus, when you use it in check() before calling assign(), it has a garbage value that isn't in the domain of allowable floating-point bit patterns and thus Turbo C++ throws an error. When you ran it the next time, it had a valid leftover bit pattern through sheer luck. In short, assign c to some default value, say, 0.0 when you define it.
Mar 4 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by Anton Noll | last post: by
4 posts views Thread by Rajesh | last post: by
8 posts views Thread by neha | last post: by
16 posts views Thread by scott | last post: by
9 posts views Thread by ssubbarayan | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.