Lost Password?

Go Back   CodeCall Programming Forum > Software Development > C# Programming

Vote on your favorite hash algorithm here!

C# Programming C# (pronounced C-sharp) is a new object oriented language from Microsoft and is derived from C and C++. It also borrows a lot of concepts from Java too including garbage collection.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-30-2008, 06:14 PM
TheMagicalRock TheMagicalRock is offline
Newbie
 
Join Date: Mar 2008
Posts: 2
Credits: 0
Rep Power: 0
TheMagicalRock is on a distinguished road
Default Question about C# user int input

Hello, I have been programming for about two weeks in c#, and I have one question, that I could not find by searching the web.

while coding a string input, it goes like:
Code:
string sname = Console.ReadLine();
Console.WriteLine("you just wrote {0}", sname);
My question though, is how you would do an input with an int value, like the user inputting a value for int x, or something like that. Thanks for any help given.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 03-31-2008, 11:37 AM
Xav's Avatar   
Xav Xav is offline
Code Warrior
 
Join Date: Mar 2008
Location: London, England
Posts: 7,553
Last Blog:
Web slideshow in JavaS...
Credits: 1,358
Rep Power: 61
Xav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to behold
Send a message via MSN to Xav
Wink Re: Question about C# user int input

By default, Console.ReadLine() returns a string value, regardless of what the user types. However, you can convert this to a string using the "int" object itself:
Code:
int x = int.Parse(Console.ReadLine());
 Console.WriteLine("you just wrote {0}", x);
Be careful, though. What if the user types something other than a number into the command prompt? In this case, you can use TryParse() to test if the line is a number:

Code:
string line = Console.ReadLine();
if (int.TryParse(line))
{
   int x = int.Parse(Console.ReadLine());
   Console.WriteLine("you just wrote {0}", x);
}
else
{
   Console.WriteLine("You didn't enter a number.")
}
The problem with this is that it will not keep asking for the piece of info. Consider this:

Code:
Console.WriteLine("Please enter your age:");
string line = Console.ReadLine()

while (!int.TryParse(line))
{
Console.WriteLine("This is not a valid number. Please enter again:");
line = Console.ReadLine();
}

int x = int.Parse(line);
Console.WriteLine("you just wrote {0}", x);
In this case, we use a "while" loop. If the user doesn't enter a valid number, the program asks for it again, until the user does enter an age. You might want to put an "if" statement in, to test whether or not the user wants to cancel.

Hope this helps,

Xav
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-31-2008, 01:02 PM
TheMagicalRock TheMagicalRock is offline
Newbie
 
Join Date: Mar 2008
Posts: 2
Credits: 0
Rep Power: 0
TheMagicalRock is on a distinguished road
Default Re: Question about C# user int input

Thank you, this helped a lot!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-01-2008, 08:36 AM
Xav's Avatar   
Xav Xav is offline
Code Warrior
 
Join Date: Mar 2008
Location: London, England
Posts: 7,553
Last Blog:
Web slideshow in JavaS...
Credits: 1,358
Rep Power: 61
Xav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to behold
Send a message via MSN to Xav
Default Re: Question about C# user int input

Quote:
Originally Posted by TheMagicalRock View Post
Thank you, this helped a lot!
No problem, Magical Stone (or Rock, whatever it is - I'm no good with names). Good luck with your C#!
__________________
[TRUTH] TcM is the best moderator ever! [/TRUTH]
"Valid XHTML is like sex - everybody claims to have the same goal, but everybody has their own tricks and results vary wildly."
Mr. Xav | Website | Forums | Blog
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
how to write the code for user to input command ? worried_student C and C++ 3 12-26-2007 08:11 PM
How to use same input for multiple while loops/cal.? Please help thieflock Java Help 3 11-12-2007 08:26 AM
Future Windows User Interfaces for Applications totoliciu Pascal/Delphi 1 09-25-2007 08:59 AM
[C++] Validating user input Xochiquetzal C and C++ 2 07-08-2007 05:18 PM
Java:Tutorial - User Input John Java Tutorials 0 12-09-2006 07:25 PM


All times are GMT -5. The time now is 10:55 PM.

Contest Stats

Xav ........ 1357.94
MeTh0Dz|Reb0rn ........ 1077.71
WingedPanther ........ 919.18
marwex89 ........ 906.86
morefood2001 ........ 900.18
John ........ 890.77
Brandon W ........ 770.65
chili5 ........ 312.39
Steve.L ........ 264.99
dcs ........ 234.34

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 83%

Ads