2

I have an application that calculates your gold per minute farmed. I want to make an error report that wouldn't allow you to insert any text in the console input where yous hould insert the time and gold(as shown in code below) and show some error message if you do and let you redo the insertion (some sort of loop or if/else thing ...) Hope i made my self clear,thou I'm new to this... so i hope you understand. Here is my code so far : -------------------------------//////////
Update on question because i didn't want to make a new question for the same code :
How do i transform in this code my hours into minutes,the float calculation of 1.2 hours will calculate into 72 minutes not 80.(i've put comment below in code where the problem is)

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

namespace YourGold
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome to YourGold App! \n------------------------");
            Console.WriteLine("Inesrt your gold: ");
            int gold = int.Parse(Console.ReadLine());
            Console.WriteLine("Your gold is : " + gold);
            Console.WriteLine("Inesrt your time(In Hours) played: ");
            float hours = float.Parse(Console.ReadLine());
            int minutes = 60;
            float time = (float)hours * minutes; // Here the calculation are wrong...
            Console.WriteLine("Your total time playd is : " + time + " minutes");
            float goldMin = gold / time;
            Console.WriteLine("Your gold per minute is : " + goldMin);
            Console.WriteLine("The application has ended, press any key to end this app. \nThank you for using it.");
            Console.ReadLine();

        }
    }
}

Thank you!

Victor
  • 468
  • 1
  • 5
  • 17
  • Hope i didn't do a bad thing that i didn't post a new question for this problem... – Victor Sep 19 '13 at 07:57
  • Question to this answer is here : http://stackoverflow.com/questions/18889761/transform-minutes-into-hours?noredirect=1#comment27881379_18889761 . Thank you all! – Victor Sep 19 '13 at 08:54

2 Answers2

8

Instead of using int.Parse, you can use int.TryParse, and print a message:

int gold;
while(!int.TryParse(Console.ReadLine(), out gold))
{
    Console.WriteLine("Please enter a valid number for gold.");
    Console.WriteLine("Inesrt your gold: ");
}

This allows you to re-prompt and handle errors correctly.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • 1
    @ЗапорожанВиктор Yes, just use `float.TryParse` (or `double.TryParse` for double precision floats) – Reed Copsey Sep 18 '13 at 19:29
1

In console application you can't control text input (no keypressed event to handle).

This could be a solution

    Console.WriteLine("Inesrt your gold: ");
    int gold;
    while(!int.TryParse(Console.ReadLine(),out gold)){
    Console.WriteLine("Please provide a valid gold value");
    }
    Console.WriteLine("Your gold is : " + gold);
Xilmiki
  • 1,453
  • 15
  • 22