Click here to Skip to main content
16,009,318 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want a user to ONLY be able to enter int values from the console namely; the litre, length and height. Alert the user that other datatypes(eg string) was inserted and prompt them to try again. The problem is the code crashes as soon a non-int value is inserted at any of the given values. Any thoughts on how to accomplish this?

What I have tried:

The interfaceClass for calculation. The interfaceClass has two methods calculateArea and totalCost
C#
public interface ICalculation
               {int calculateArea();int totalCost()
}


Inheriting the interface class
The variables are litre, length, height.
C#
 public class Calculation : ICalculation
    {
        public int litre;
        public int length;
        public int height;
        public Calculation()
        {litre= 0;length = 0;height = 0;
  }

   public Calculation(int litre2, int lenght2, int height2)
   {
      this.litre = litre2;
      this.length = lenght2;
      this.height = height2;
   }

     public int calculateArea() {

        return length * height;
     }
     public int totalCost() {

        return calculateArea()* litre;
     }
}

Testing the program to see the output. Create a new object calculation and then prompt the user to fill in the litre, lenght and height. Calcualate the area and total cost after getting the values.
C#
class Tester
   {
       static void Main(string[] args)
       {

           Calculation c = new Calculation();


           try{
               Console.WriteLine("How much litre?");
               c.litre = int.Parse(Console.ReadLine());
               Console.WriteLine("What is the lenght");
               c.length = int.Parse(Console.ReadLine());
               Console.WriteLine("What is the height");
               c.height = int.Parse(Console.ReadLine());

               Console.WriteLine("The Total area " + c.calculateArea());
               Console.WriteLine("The Total cost for the project is " + c.totalCost());
               Console.ReadKey();
          }
          catch(FormatException ){
             Console.WriteLine("{0}is not an integer, try again!");
             // Console.ReadLine();
          }
      }
   }  
}
Posted
Updated 9-Oct-16 17:39pm
v3
Comments
[no name] 9-Oct-16 19:01pm    
Yep. Don't use int.Parse use int.TryParse.

1 solution

Quote:
Alert the user that other datatypes (eg string) was inserted without crashing
You have a misconception of how user input is done.
Console.ReadLine() takes user input as a string and only a string.
int.Parse convert the contains of a string and crashes the program if not happy with the string contains.
int.TryParse tells you if the parse was successful instead of crashing.

If you want yo parse more complex data, you will have to learn Regular Expressions.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx:
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
 
Share this answer
 

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