• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

C# system.Console.read() help needed..

beer

Lifer
I'm learning C# so I'm converting some of my C++ projects to C# to learn the synatical differences. One thing I'm working on is the APCS Marine Biology Case Study Part I - eventually I will do part II but there are some fundamental C# things I'm failing to grasp, and I need help.

Console.read() reads in a stream of characters
Console.readline() reads in a string;

So now the question is, how do I read in integers?

Case in point:


using System;

class Demo
{
public static void Main()
{
int tankSize;
int stepsPerSim;
int step;

Console.Write("Tank size? ");
Size = Console.Read();
Console.WriteLine("tankSize = {0}", tankSize);
Console.Write("Steps per simulation? ");
stepsPerSim = Console.Read();
Console.WriteLine("stepsPerSim = {0}", stepsPerSim);

AquaFish fish = new AquaFish(tankSize);

for (step = 0; step < stepsPerSim; step++)
fish.Swim();

Console.WriteLine("Bump count = {0}", fish.BumpCount());
}
}

Appearantly, inputting '10' or some other integer comes out as a character, and myTankSize ends up being around 50, and I don't know why.

Can anyone explain?

BTW - is there a way for me to declare Main() just as void outside a class, as opposed to having to create a class and declaring it as public static void?
 
Receive input as a string and cast them into integer with the class libraries under the System.Convert namespace.
 
Back
Top