CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: how do i find decimal ?

  1. #1
    Join Date
    Dec 2017
    Posts
    2

    how do i find decimal ?

    Hi. I wrote this code below to find the average of three numbers. It works, but the answer is always rounded to a whole number. How can I change my code to make this work?

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Assignment_1_mcdaniel
    {
        class Program
        {
            static void Main(string[] args)
            {
                int f, s, t, r;
                Console.WriteLine("Enter Frst Number:");
                f = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("Enter Second Number:");
                s = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("Enter Third Number:");
                t = Convert.ToInt32(Console.ReadLine());
                r = (f + s + t) / 3;
                Console.WriteLine("Answer:" + r);
                Console.ReadKey();
            }
        }
    }
    Last edited by 2kaud; December 11th, 2017 at 04:23 AM. Reason: Added code tags

  2. #2
    Join Date
    Feb 2017
    Posts
    567

    Re: how do i find decimal ?

    Quote Originally Posted by 1236x View Post
    How can I change my code to make this work?
    It's because you are using integers (int) throughout and integers are whole numbers. If you want decimal numbers you should use floating point numbers ( float or double). I recommend double as first choice.

    Assuming you really want f, s and t to be integers you only need to declare r a floating point variable. Note that 3 is an integer literal whereas 3.0 is a floating point literal. To make your intentions crystal clear you could do this,

    Code:
    int f, s, t;
    double r;
    //
    r = double(f + s + t) / 3.0;
    The f+s+t summing is performed using integer arithmetic and then explicitly converted to a floating point. This sum is then divided with the floating point literal 3.0 and finally the result is assigned to the floating point variable r.
    Last edited by wolle; December 14th, 2017 at 10:54 AM.

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,797

    Re: how do i find decimal ?

    [moved from C++ (Non Visual C++ Issues) forum]
    Victor Nijegorodov

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,267

    Re: how do i find decimal ?

    [When posting code, please use code tags so that the code is readable. Go Advanced, select the formatted code and click '#'. Cheers!]
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.7.4)

  5. #5
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: how do i find decimal ?

    Wait... This isn't even C++/CLI, it's C#!
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,797

    Re: how do i find decimal ?

    Quote Originally Posted by Eri523 View Post
    Wait... This isn't even C++/CLI, it's C#!
    OK!
    The thread was moved from Managed C++ and C++/CLI forum.
    Victor Nijegorodov

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)