Jump to content

simple codes for C#

- - - - -

  • Please log in to reply
4 replies to this topic

#1
niel

niel

    Newbie

  • Members
  • Pip
  • 2 posts
Collapse | Copy Code
using System;

namespace PrintPrice
{
class Price
{
const int pricePerLicense = 10;

static void Main()
{
int pricePerLicense;
Console.Write("Enter a price for license: ");
pricePerLicense = int.Parse(Console.ReadLine());
Console.Write("price for license: " + pricePerLicense);
Console.ReadKey();
}
}
}
If you press only ENTER without entering a value, use a default or the constant value
pleas help me :( thanks

Edited by Roger, 16 December 2011 - 08:28 AM.
moved to correct forum + added code tags


#2
lespauled

lespauled

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 231 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash
Instead of: pricePerLicense = int.Parse(Console.ReadLine());

You could do something like:

string inputString = Console.ReadLine();

if( string.IsNullOrEmpty(inputString))
...set a default value.

pricePerLicense = int.Parse in

#3
Todilo

Todilo

    Newbie

  • Members
  • Pip
  • 5 posts
also it is better to use int.TryParse(Console.ReadLine(), out intvariablehere); In conjuction with above writer you have yourself a great solution.

#4
niel

niel

    Newbie

  • Members
  • Pip
  • 2 posts
what if i enter a value for the variable "pricePerLicense". used you codes and when i enter a value for the "pricePerLicense" the output is still in the global value.

---------- Post added at 04:17 PM ---------- Previous post was at 04:16 PM ----------

Todilo said:

also it is better to use int.TryParse(Console.ReadLine(), out intvariablehere); In conjuction with above writer you have yourself a great solution.


what if i enter a value for the variable "pricePerLicense". used you codes and when i enter a value for the "pricePerLicense" the output is still in the global value.

#5
Todilo

Todilo

    Newbie

  • Members
  • Pip
  • 5 posts

niel said:

what if i enter a value for the variable "pricePerLicense". used you codes and when i enter a value for the "pricePerLicense" the output is still in the global value.

---------- Post added at 04:17 PM ---------- Previous post was at 04:16 PM ----------




what if i enter a value for the variable "pricePerLicense". used you codes and when i enter a value for the "pricePerLicense" the output is still in the global value.


By global value do you mean the value you set to begin with?

What you do is
string inputString ; 

while(true)

{

inputString = Console.ReadLine();

bool result = int.TryParse(inputString, out pricePerLicense);

if(result == true)

break;

}

I have not tested it but I think it will work. What happens is:
Console tries to read line. When line is read it tries to parse it into an int. If the parser sais OK result = true then it breaks and you can continue. If not then it tries ago. Note that this is NOT the most efficient way to write this, it could be significantly shorter, but for explaining how it all works I found this to be the best to use. Also, use "[CODE]" tags




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users