Works on Dev C++, Doesn't on Visual C++ :(

This is a discussion on Works on Dev C++, Doesn't on Visual C++ :( within the C++ Programming forums, part of the General Programming Boards category; Hi, I have this code: #include <iostream.h> typedef unsigned short int USI; float function( USI number1, USI number2 ); int ...

  1. #1
    marcvb
    Guest

    Question Works on Dev C++, Doesn't on Visual C++ :(

    Hi,

    I have this code:

    #include <iostream.h>
    typedef unsigned short int USI;
    float function( USI number1, USI number2 );
    int main()
    {
    USI number1, number2;
    float result;
    cout<<"Number1: ";
    cin>>number1;
    cout<<"Number2: ";
    cin>>number2;
    result = function( number1,number2 );
    if ( result == -1 )
    cout<<"The sum: "<<number1<<"/"<<number2<<" could not be worked out!";
    else
    {
    cout<<"----------------\n";
    cout<<"First Number: "<<number1<<endl;
    cout<<"Second Number: "<<number2<<endl;
    cout<<"----------------\n";
    cout<<number1<<"/"<<number2<<" = "<<result<<endl;
    }
    return 0;
    }

    float function( USI a, USI b )
    {
    if ( b == 0 )
    return -1;

    else
    return a/b;
    }

    -------------------------------------------

    Now on Dev C++ it works perfectly.
    When I enter 0 for the second number it reports the error.

    When I try to compile it on Visual C++, when I put a 0, it still works it out for me. Something is wrong.

    Anyone may help me?

    Thanks,
    Marc

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Something is wrong
    I cant see it!
    nor could my copy of visual c
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    marcvb
    Guest

    Unhappy :(

    but in Dev C++, when I input 0 as the second number, it tells me it could not work it out blabla....
    In visual C++, when I input 0 it still gets the working. I.e. it doesn't say that It could not work it out.

    What may be wrong?

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    i have visual c 6 and it ran perfectly for me!
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    You might want to return an integer from your function rather than a float, as you shouldn't really test for equality with floats (the internal representation may be very slightly different, your function might be returning -1.000000001).
    zen

  6. #6
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    On second thoughts, it looks like you're trying to return a float (you'll have to cast the interger division). You could test for less than zero -

    Code:
    #include <iostream.h> 
    typedef unsigned short int USI; 
    float function( USI number1, USI number2 ); 
    
    int main() 
    { 
    	USI number1, number2; 
    	float result; 
    	cout<<"Number1: "; 
    	cin>>number1; 
    	cout<<"Number2: "; 
    	cin>>number2; 
    	result = function( number1,number2 ); 
    	if ( result < 0 ) 
    		cout<<"The sum: "<<number1<<"/"<<number2<<" could not be worked out!"; 
    	else 
    	{ 
    		cout<<"----------------\n"; 
    		cout<<"First Number: "<<number1<<endl; 
    		cout<<"Second Number: "<<number2<<endl; 
    		cout<<"----------------\n"; 
    		cout<<number1<<"/"<<number2<<" = "<<result<<endl; 
    	} 
    	return 0; 
    } 
    
    float function( USI a, USI b ) 
    { 
    	if ( b == 0 ) 
    		return -1; 
    
    	else 
    		return (float)a/b; 
    }
    zen

  7. #7
    marcvb
    Guest

    Talking

    thx!!! it worked
    I am readin sams teach yourself C++ in 21 days ..3rd version ...seems very good.
    I am gonna become a member..I like this place

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Templates and Macros plus more...
    By Monkeymagic in forum C++ Programming
    Replies: 8
    Last Post: 01-20-2007, 05:53 PM
  2. Functions in C
    By shoobsie in forum C Programming
    Replies: 15
    Last Post: 11-17-2005, 01:47 PM
  3. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM
  4. Erros in Utility Header File
    By silk.odyssey in forum C++ Programming
    Replies: 4
    Last Post: 12-22-2003, 06:17 AM
  5. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21