9 Replies - 16499 Views - Last Post: 17 September 2008 - 06:30 PM Rate Topic: -----

#1 SleepingOlive  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 16-September 08

Convert String to int

Post icon  Posted 17 September 2008 - 07:47 AM

I am trying to write a simple program that asks the user to inter a number and then i will take that number to decide what the cost of the ticket will be for there given age. I am having trouble when trying to convert the string to int otherwise the program layout is fine. Any suggestions?
thanks
SleepingOlive

p.s. i am using the command line for the input and output as well

using System;

class ticketPrice
{	
	public static void Main(String[] args)
	{
		Console.WriteLine("Please Enter Your Age");
		int input = Console.ReadLine();
		if (input < 5)
		{
			Console.WriteLine("You are "+input+" and the admisson is FREE!");
		}
		else if (input > 4 & input < 18)
		{
			Console.WriteLine("You are "+input+" and the admission is $5");
		}
		else if (input > 17 & input < 56)
		{
			Console.WriteLine("You are "+input+" and the admission is $10");
		}
		else if (input > 55)
		{
			Console.WriteLine("You are "+input+" and the admission is $8");
		}
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Convert String to int

#2 SleepingOlive  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 16-September 08

Re: Convert String to int

Post icon  Posted 17 September 2008 - 07:48 AM

I am trying to write a simple program that asks the user to inter a number and then i will take that number to decide what the cost of the ticket will be for there given age. I am having trouble when trying to convert the string to int otherwise the program layout is fine. Any suggestions?
thanks
SleepingOlive

p.s. i am using the command line for the input and output as well

using System;

class ticketPrice
{	
	public static void Main(String[] args)
	{
		Console.WriteLine("Please Enter Your Age");
		int input = Console.ReadLine();
		if (input < 5)
		{
			Console.WriteLine("You are "+input+" and the admisson is FREE!");
		}
		else if (input > 4 & input < 18)
		{
			Console.WriteLine("You are "+input+" and the admission is $5");
		}
		else if (input > 17 & input < 56)
		{
			Console.WriteLine("You are "+input+" and the admission is $10");
		}
		else if (input > 55)
		{
			Console.WriteLine("You are "+input+" and the admission is $8");
		}
	}
}


Was This Post Helpful? 0
  • +
  • -

#3 SleepingOlive  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 16-September 08

Re: Convert String to int

Post icon  Posted 17 September 2008 - 07:49 AM

I am trying to write a simple program that asks the user to inter a number and then i will take that number to decide what the cost of the ticket will be for there given age. I am having trouble when trying to convert the string to int otherwise the program layout is fine. Any suggestions?
thanks
SleepingOlive

p.s. i am using the command line for the input and output as well

using System;

class ticketPrice
{	
	public static void Main(String[] args)
	{
		Console.WriteLine("Please Enter Your Age");
		int input = Console.ReadLine();
		if (input < 5)
		{
			Console.WriteLine("You are "+input+" and the admisson is FREE!");
		}
		else if (input > 4 & input < 18)
		{
			Console.WriteLine("You are "+input+" and the admission is $5");
		}
		else if (input > 17 & input < 56)
		{
			Console.WriteLine("You are "+input+" and the admission is $10");
		}
		else if (input > 55)
		{
			Console.WriteLine("You are "+input+" and the admission is $8");
		}
	}
}


Was This Post Helpful? 1

#4 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,559
  • Joined: 25-January 07

Re: Convert String to int

Posted 17 September 2008 - 07:52 AM

I hope you double posted by mistake. :)
Was This Post Helpful? 0
  • +
  • -

#5 mascot  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 17-September 08

Re: Convert String to int

Posted 17 September 2008 - 07:53 AM

String input;

input = Console.ReadLine();

int yourIntValue = Int32.Parse(input);


^_^ hope it helps and delete 2 others topic pls :)

This post has been edited by mascot: 17 September 2008 - 07:54 AM

Was This Post Helpful? 0
  • +
  • -

#6 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,559
  • Joined: 25-January 07

Re: Convert String to int

Posted 17 September 2008 - 07:58 AM

Try System.Convert.ToInt32

I hope this will help you. :)
Was This Post Helpful? 0
  • +
  • -

#7 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Convert String to int

Posted 17 September 2008 - 08:07 AM

I would go with Int32.TryParse() over Int32.Parse() as Int32.Parse() will throw an exception if a non-numeric character is thrown to it, whereas Int32.TryParse will not throw that exception.
Was This Post Helpful? 0
  • +
  • -

#8 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Convert String to int

Posted 17 September 2008 - 08:07 AM

Please don't create duplicate topics. Topics merged :)
Was This Post Helpful? 0
  • +
  • -

#9 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Convert String to int

Posted 17 September 2008 - 08:09 AM

Ahhh a 3rd post for this? Please, please don't create duplicate topics, it isn't going to get you help any faster. Merged
Was This Post Helpful? 0
  • +
  • -

#10 gbertoli3  Icon User is offline

  • DIC at Heart + Code
  • member icon

Reputation: 40
  • View blog
  • Posts: 1,162
  • Joined: 23-June 08

Re: Convert String to int

Posted 17 September 2008 - 06:30 PM

Try this
Convert.ToInt32("String Value");



Hope this helps
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1