Newbie needs help with c++

Newbie needs help with c++

Post by Alasdair Baxte » Fri, 06 Dec 2002 09:24:51



I am trying to use c++ to do simple calculations. When I enter the
following, I get answer 4 instead of 4.5.  I have declared main() as
float.  Why?

#include <iostream>

using namespace std;

float main ()

{

        cout << ((3+6)/2) << endl;

        return 0;

Quote:}

--

Alasdair Baxter, Nottingham, UK.Tel +44 115 9705100; Fax +44 115 9423263

        "It's not what you say that matters but how you say it.
         It's not what you do that matters but how you do it"

 
 
 

Newbie needs help with c++

Post by Ron Natali » Fri, 06 Dec 2002 09:38:50



> I am trying to use c++ to do simple calculations. When I enter the
> following, I get answer 4 instead of 4.5.  I have declared main() as
> float.  Why?

Because declaring main as a float is wrong.  Your compiler was a fool
to accept it.   That float says that the return value from main should be
a float (it must be an int)... it doesn't change the behavior of anything inside
main.

Quote:> cout << ((3+6)/2) << endl;

All the things in the expression ((3+6)/2) are ints.   4 is the proper answer.
If you want to do this as a floating point operation, you must make them
a floating point type.

    (3.0 + 6.0) / 2.0

changes the types of the literals all to double and the value will be 4.5

 
 
 

Newbie needs help with c++

Post by Victor Bazaro » Fri, 06 Dec 2002 09:40:21



> I am trying to use c++ to do simple calculations. When I enter the
> following, I get answer 4 instead of 4.5.  I have declared main() as
> float.

main has return value 'int' no matter what you want.  Making
it 'float' doesn't help (BTW, what book told you to change
the return value type of 'main' to accomplish what you want?)

Quote:>  Why?

Because numbers without decimal point are considered integers.
Integer division results in an integer quotient.  Learn about
it instead of dreaming up language features.

Quote:

> #include <iostream>

> using namespace std;

> float main ()

> {

> cout << ((3+6)/2) << endl;

> return 0;
> }

Victor
--
Please remove capital A's from my address when replying by mail
 
 
 

Newbie needs help with c++

Post by Mike Wahle » Fri, 06 Dec 2002 09:53:04



Quote:> I am trying to use c++ to do simple calculations. When I enter the
> following, I get answer 4 instead of 4.5.  I have declared main() as
> float.  Why?

> #include <iostream>

> using namespace std;

> float main ()

int main()

Quote:

> {

> cout << ((3+6)/2) << endl;

  cout << (3 + 6) / 2.0 << endl;

Quote:

> return 0;
> }

-Mike
 
 
 

Newbie needs help with c++

Post by Alasdair Baxte » Fri, 06 Dec 2002 10:28:03





>> I am trying to use c++ to do simple calculations. When I enter the
>> following, I get answer 4 instead of 4.5.  I have declared main() as
>> float.  Why?

>Because declaring main as a float is wrong.  Your compiler was a fool
>to accept it.   That float says that the return value from main should be
>a float (it must be an int)... it doesn't change the behavior of anything inside
>main.
>> cout << ((3+6)/2) << endl;

>All the things in the expression ((3+6)/2) are ints.   4 is the proper answer.
>If you want to do this as a floating point operation, you must make them
>a floating point type.

>    (3.0 + 6.0) / 2.0

>changes the types of the literals all to double and the value will be 4.5

Thank you for a very comprehensive explanation.  One query:  I thought
a "literal" was a string in quotes but it obviously has another
meaning.
--

Alasdair Baxter, Nottingham, UK.Tel +44 115 9705100; Fax +44 115 9423263

        "It's not what you say that matters but how you say it.
         It's not what you do that matters but how you do it"

 
 
 

Newbie needs help with c++

Post by Dimitris Kamenopoulo » Fri, 06 Dec 2002 10:44:57



> Thank you for a very comprehensive explanation.  One query:  I thought
> a "literal" was a string in quotes but it obviously has another
> meaning.

Check out www.dict.org. Limit your search to The Free Online Dictionary of
Computing. I couldn't have explained literal any better myself, and,
besides, it is 4am in Greece right now :-)
 
 
 

Newbie needs help with c++

Post by Neil Cerutt » Sat, 07 Dec 2002 00:48:15




> Thank you for a very comprehensive explanation.  One query:  I
> thought a "literal" was a string in quotes but it obviously has
> another meaning.

In C++ terms, a literal X, where X is a built-in type, is a way
of writing an X directly in source code. "Literal" by itself
doesn't mean anything in C++.

--

 
 
 

Newbie needs help with c++

Post by Alexander Terekho » Sat, 07 Dec 2002 01:14:20





> > Thank you for a very comprehensive explanation.  One query:  I
> > thought a "literal" was a string in quotes but it obviously has
> > another meaning.

> In C++ terms, a literal X, where X is a built-in type, is a way
> of writing an X directly in source code. "Literal" by itself
> doesn't mean anything in C++.

Right on! Neither 'true' nor 'false' literals mean anything
[particularly interesting] in C++ ["by itself"]. ;-)

regards,
alexander.

--
"literal:
   integer-literal
   character-literal
   floating-literal
   string-literal
   boolean-literal
 ...
 The term "literal" generally designates, in this International
 Standard, those tokens that are called "constants" in ISO C."

 
 
 

Newbie needs help with c++

Post by Neil Cerutt » Sat, 07 Dec 2002 01:16:28






>> > Thank you for a very comprehensive explanation.  One query:  I
>> > thought a "literal" was a string in quotes but it obviously has
>> > another meaning.

>> In C++ terms, a literal X, where X is a built-in type, is a
>> way of writing an X directly in source code. "Literal" by
>> itself doesn't mean anything in C++.

> Right on! Neither 'true' nor 'false' literals mean anything
> [particularly interesting] in C++ ["by itself"]. ;-)

Doh!

--

 
 
 

1. C++ Windows :- Newbies need help! Please!

Hello,

        I have written a program in Borland Turbo C++ for windows which works ...
        BUT ... I have a few problems.

        1) I can run it more than once. I would like the old instance to pop up
           if the user tries to re-run the program.

        2) The same problem as above but with a window generated by the main program.

        Any help or sample code would be appreciated.


        Thanks in advance

2. PCMCIA Ethernet Adapter

3. newbie needs help with pthread_create & C++

4. Man pages?

5. Newbie needs help with ... using 'C' instead of C++?

6. C/C++ Compiler and ASM'r for 68HC11

7. Newbie to C++ needs help with Ini files

8. Wildcards in BIND

9. C++ Newbie/Student needs help!

10. Newbie help needed... standard C++

11. Newbie to C++ needs HELP!!

12. Newbie needs help with linked list program...(C++)

13. C++ newbie needs some adviice/help???!!!???ASAP