Aquanun
Aquanun

Reputation: 51

Need help determining source of bug in small C# number to percentile conversion program

I'm fairly new to programming in general but have been working with Java for the past 2-3 months and decided to take a crack at C# on the side just for fun because I heard they were similar. The problem I'm having with this program, which is just a way for someone to convert an int to a percentage, is that it takes all my input correctly but the percentage always shows up as zero! For example, there are two input prompts: one that asks you for a number for converting and one that asks you what the number is out of. After both prompts the program displays correctly "Your percentage is: " and then just writes 0 no matter what numbers I use for input, help please!

[code]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NumToPercentage
{
class Program
{
    static void Main(string[] args)
    {
        int userScore, userTotalScore, userPercentage;
        string userInput, userTotal;


        Console.WriteLine("Welcome to the number to percentile conversion program!");

        Console.WriteLine("Please enter your number here: ");
        userInput = Console.ReadLine();
        userScore = int.Parse(userInput); 

        Console.WriteLine("Please enter what the number is out of (i.e. 100, 42, 37, etc.): ");
        userTotal = Console.ReadLine();
        userTotalScore = int.Parse(userTotal);

        userPercentage = (userScore/userTotalScore)*100; 

        Console.WriteLine("Your percentage is: " + userPercentage); 
        Console.WriteLine("Press any key to exit.");
        Console.ReadLine();
    }
}
}

[/code]

Upvotes: 3

Views: 94

Answers (5)

Lee Greco
Lee Greco

Reputation: 743

Because you declare your vars as int, you're performing an integer division.

You need to declare userPercentage as a double instead of int and cast the userScore and userTotalScore to double before performing the division.

Upvotes: 0

Scen
Scen

Reputation: 1720

(userScore/userTotalScore)*100;

dividing two integers will result in an integer value. It chops off the fraction.

Instead, do this

(int)( ( (double) userScore )/userTotalScore)*100 );

By utilizing type casting, you can get the result you are looking for.

Upvotes: 2

Klaus Byskov Pedersen
Klaus Byskov Pedersen

Reputation: 120937

You need to use decimals instead of ints. Integer division behaves different than floating point division.

Change your declarations to:

decimal userScore, userTotalScore, userPercentage;

And then later:

userScore = decimal.Parse(userInput);

And

userTotalScore = decimal.Parse(userTotal);

Upvotes: 0

Anthony Pegram
Anthony Pegram

Reputation: 126884

The problem is integer math, it rounds towards zero.

  • 1 divided by 100 is 0.
  • 99 divided by 100 is 0.
  • 199 divided by 100 is 1.

Convert at least one of the operands to a floating point type (double, float, decimal) when you perform your calculation.

double result = ((double)x / y) * 100;

If needed, you can always cast the result back to an integer if you are uninterested in the decimal places.

int finalPercent = (int)result;

Upvotes: 7

vc 74
vc 74

Reputation: 38179

userPercentage = (int)((double)userScore/userTotalScore)*100); 

or

userPercentage = (userScore * 100) / userTotalScore;

int/int is an integer division and only returns the whole part which is always 0 for a percentage, which then multiplied by 0 equals... 0

Upvotes: 3

Related Questions