how can i fix this error?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • torres1
    New Member
    • Mar 2010
    • 2

    how can i fix this error?

    hi all,

    i am writing a weight conversion program that allows the user to input their weight, however i keep getting the same error saying operator '\' cannot be applied to operands of type decimal and double. can anyone help me with this?

    here is my code

    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace WeightConverter
    {
        class WeightConverter
        {
    
            static void Main(string[] args)
            {
                //declare variables
                int selection = 0;
    
    
    
                while (selection < 5)
                {
    
                    try
                    {
                        //Ask's the user to select a type to be converted
                        Console.WriteLine("Select type:" +
                            "\n1) Metric to Imperial\n2) Imperial to Metric\n3) Exit the program.\n");
                        selection = Convert.ToInt32(Console.ReadLine());
    
                        /*if statement which checks if the user wanted to end the
                          program of if they entered a selection between 1 and 5*/
                        if (selection == 5)
                            Console.WriteLine("Exiting program.");
                        else if (selection < 1 || selection > 3)
                            Console.WriteLine("Please enter a selection between 1 and 5.\n");
                        else
                            SelectionMethod(selection);
                    }//end try
                    //if input value is not numeric, display message
                    catch
                    {
                        Console.WriteLine("Invalid input value.");
                    }//end catch
                }//end while
    
            }//end Main method
    
            public static int SelectionMethod(int s)
            {     
                //declare variables and create reference to WeightConverter class
                WeightConverter weightConverter = new WeightConverter();
                
                decimal kilograms = 0;
                decimal pounds = 0;
                
                
                try
                {
                    
                    switch (s)
                    {
                        
                        //if user chose to input kilograms, prompt user to enter how many
                        case 1:
                            Console.WriteLine("Input amount of kilograms to be conveted:\n");
                            kilograms = Convert.ToDecimal(Console.ReadLine());
    
                            //call Kilograms method of class WeightConverter
                            weightConverter.Kilograms(kilograms);
                            break;
    
                       
                        //if user chose to input pounds, prompt user to enter how many
                        case 2:
                            Console.WriteLine("Input amount of pounds to be converted:\n");
                            pounds = Convert.ToDecimal(Console.ReadLine());
    
                            //call Pounds method of class WeightConverter
                            weightConverter.Pounds(pounds);
                            break;
                    }
                }//end try
                //if input value is not numeric, display message
                catch
                {
                    Console.WriteLine("Invalid input value.");
                }//end catch
    
                return s;
            }//end Selection method
    
            public decimal Kilograms(decimal kg)
            {
                decimal pounds = 0;
                int selection = 0;
                while (selection <= 2)
                {
                    try
                    {
                        //ask's the user to select what they would like to convert grams to
                        Console.WriteLine("Convert Kilograms to?\n1) Pounds" +
                                "\n2) Return to first selection.");
                        selection = Convert.ToInt32(Console.ReadLine());
    
                        switch (selection)
                        {
                            
                            //convert kilograms to pounds
                            case 1:
                                Kilograms = pounds / 2.2046;  
    
                                Console.WriteLine("{0} kilogram(s) is {1} pound(s).", kg, pounds);
                                break;
    
                            //return to first selection
                            case 2:
                                selection = 5;
                                break;
    
                        }
                    }//end try
                    //if value entered is not numeric, display message
                    catch
                    {
                        Console.WriteLine("Invalid input value.");
                    }//end catch
                }//end while
                return kg;
            }
    
            
            public decimal Pounds(decimal p)
            {
                decimal kilograms = 0;
                int selection = 0;
                while (selection <= 2)
                {
                    try
                    {
                        //prompt user to select what they would like to convert grams to
                        Console.WriteLine("Convert pounds to?\n1) Kilograms\n2) Return to first selection.");
                        selection = Convert.ToInt32(Console.ReadLine());
    
                        switch (selection)
                        {
                            //convert pounds to kilograms
                            case 1:
                                Pounds = kilograms / 0.453;
    
                                Console.WriteLine("{0} pound(s) is {1} kilogram(s).", p, kilograms);
                                break;
    
                           
                            //return to first selection
                            case 2:
                                selection = 5;
                                break;
    
                        }
                    }//end try
                    //if value entered is not numeric, display message
                    catch
                    {
                        Console.WriteLine("Invalid input value.");
                    }//end catch
                }//end while
                return p;
            }
        }
            }
    Last edited by tlhintoq; Mar 6 '10, 10:56 PM. Reason: [CODE] ...Your code goes between code tags [/CODE]
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      You left a lot of code. On what line do you get that error message?

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        If you are trying divide you want to use / not \

        Comment

        Working...