Floating type problem with turbo c++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Parul Bagadia
    New Member
    • Mar 2008
    • 188

    Floating type problem with turbo c++

    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.
    [code=c]
    #include<stdio. h>
    #include<conio. h>
    void assign();
    void check();
    float c;
    void main()
    {
    int choise;
    do{
    printf("\nEnter your choise from the given menu:\n 1.Assign\n 2.Check \n 3.Exit\n");
    scanf("\n%d",&c hoise);
    switch(choise)
    {
    case 1:
    assign();
    break;
    case 2:
    check();
    break;
    case 3:
    break;
    }
    }while(choise!= 3);
    }
    void check()
    {
    float i;
    printf("%f",&c) ;
    i=c+1;
    printf("\n%f",i );
    printf("\n%f",& i);
    }
    void assign()
    {
    printf("\n Enter the value of c:\n");
    scanf("\n%f",&c );
    }
    [/code]

    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
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    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.

    Comment

    Working...