Click here to Skip to main content
15,904,822 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello
Im doing an C sharp console application, but im having an issue. Here is the warning message and the line that is marked


<br />
An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll<br />
<br />
line =Convert.ToInt32(temp);


here is the full code
C#
using System.Collections.Generic;
using System;
using System.Diagnostics;
using System.Data;
using System.Xml.Linq;
using Microsoft.VisualBasic;
using System.Collections;
using System.Linq;
 
 
using Microsoft.VisualBasic.CompilerServices;
 
namespace Assignment1
{
        sealed class Module1
        {
     
               
                static string temp; //Global variable
               
               
                static public void Main()
                {
                        int price = 0;
                        int paid = 0;
                        int balance = 0;
                       
                       
                        int no_of_500 = 0;
                        int no_of_100 = 0;
                        int no_of_50 = 0;
                        int no_of_20 = 0;
                       
                        int no_of_10 = 0;
                        int no_of_5 = 0;
                        int no_of_1 = 0;
                       
                        Console.WriteLine();
                        Console.Write("Enter the price =  ");
                       
                        //let user to enter and assigning it to variable by explicitly casing to int since option "strict on" is activated
                        price = validateInput();
                       
                        Console.WriteLine();
                        Console.Write("Paid  = ");
                        paid = validateInput();
                       
                       
                        //check paid cash is not less than the price
                        if (paid < price)
                        {
                                Console.WriteLine();
                                Console.WriteLine("Amount you paid is not sufficient.You need to pay more " + (price - paid).ToString() + " kr.");
                                Console.WriteLine();
                               
                                //show balance
                        }
                        else
                        {
                                balance = paid - price;
                                Console.WriteLine();
                                Console.Write("Balance = " + balance.ToString());
                                Console.WriteLine();
                                Console.WriteLine();
                               
                                //check for how many 500 notes
                                //if balance is greater than 500
                                //then using "\" divition find how many 500 s inside the balance
                                //finally update the balance by subtracting toatal value of 500 notes
                                //same logic will apply for all notes and coins in the order of 500,100,50,20,10,5,1
                                if (balance >= 500)
                                {
                                        no_of_500 = balance / 500;
                                        balance = balance - no_of_500 * 500;
                                }
                               
                                if (balance >= 100)
                                {
                                        no_of_100 = balance / 100;
                                        balance = balance - no_of_100 * 100;
                                }
                               
                                if (balance >= 50)
                                {
                                        no_of_50 = balance / 50;
                                        balance = balance - no_of_50 * 50;
                                }
                               
                                if (balance >= 20)
                                {
                                        no_of_20 = balance / 20;
                                        balance = balance - no_of_20 * 20;
                                }
                               
                                if (balance >= 10)
                                {
                                        no_of_10 = balance / 10;
                                        balance = balance - no_of_10 * 10;
                                }
                               
                                if (balance >= 5)
                                {
                                        no_of_5 = balance / 5;
                                        balance = balance - no_of_5 * 5;
                                }
                               
                                //at last  the remainig balance less than five means there will be equal no of 1 crown coins
                                if (balance <= 4)
                                {
                                        no_of_1 = balance;
                                }
                               
                                //show how many notes and coins to change back
                                if (no_of_500 > 0)
                                {
                                        Console.WriteLine(no_of_500.ToString() + " x 500 sek note");
                                }
                                if (no_of_100 > 0)
                                {
                                        Console.WriteLine(no_of_100.ToString() + " x 100 sek note");
                                }
                                if (no_of_50 > 0)
                                {
                                        Console.WriteLine(no_of_50.ToString() + " x 50 sek note");
                                }
                                if (no_of_20 > 0)
                                {
                                        Console.WriteLine(no_of_20.ToString() + " x 20 sek note");
                                }
                                if (no_of_10 > 0)
                                {
                                        Console.WriteLine(no_of_10.ToString() + " x 10 kr");
                                }
                                if (no_of_5 > 0)
                                {
                                        Console.WriteLine(no_of_5.ToString() + " x 5 kr");
                                }
                                if (no_of_1 > 0)
                                {
                                        Console.WriteLine(no_of_1.ToString() + " x 1 kr");
                                }
                               
                               
                        }
                       
                        //wait for key press to stop sudden closing
                        Console.ReadLine();
                       
                }
               
                private static int validateInput()
                {
                       
                        int value = default(int);
                        try
                        {
                               
                                //take no of items to string num
                                temp = Console.ReadLine();
                               
                                //if input is not numeric this will throw an InvalidCastException when it tries to cast to integer and
                //will jump to catch block
                 value =Convert.ToInt32(temp);
 
 
                //check whether is it has decimal places by checking for (".")
                                //if input is like  12.5  it will split in to two parts as 12 and 5 by "."
                                //and length of parts() will be two. that means entered value is not a whole number
                                string[] parts = temp.Split('.');
                                if (parts.Length == 2)
                                {
                                        Console.WriteLine();
                                        Console.WriteLine("  Input must be an Non negative / whole / numeric value.");
                                        Console.ReadLine();
                                        ProjectData.EndApp();
                                }
                                else
                                {
                                        // if it is not charcter and not fractional then cast  it to integer
                                        value = int.Parse(temp);
                                }
                               
                                //if number is negative
                                if (value < 0)
                                {
                                        Console.WriteLine();
                                        Console.WriteLine("  Input must be an Non negative / whole / numeric value.");
                                        Console.ReadLine();
                                        ProjectData.EndApp();
                                }
                        }
                        catch (InvalidCastException)
                        {
                                Console.WriteLine();
                                Console.WriteLine("  Input must be an Non negative / whole / numeric value.");
                                Console.ReadLine();
                                ProjectData.EndApp();
                        }
                        return value;
                }
        }
       
}
Posted

1 solution

Use Int32.TryParse[^] instead of a direct cast.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900