Debug255 Debug255 - 3 months ago 12
C# Question

Read() only takes first input but no other input from other Console.Read()

I am following instructions in a C# Tutorial video that is great. I am building a file with the notes and code from the video.

I reviewed similar questions but they do not solve this problem.

Here is a copy of the CS file:

static void Main(string[] args)
{
// Single line comments
/* test multi-line comments
* asldkjasldkjaskd
* asldkjasldkja
* alsdkjalksdj
* */
Console.WriteLine("Hello world!");
Console.Write("What is your name? ");
string name = Console.ReadLine();
Console.WriteLine("Hello " + name);

bool canVote = true;

char grade = 'A';

// Integer with a max number of 2,147,483,647
int maxInt = int.MaxValue;

//Long with max value of 9,223,372,036,854,775,807
long maxLong = long.MaxValue;

// Decimal has a max value of 79,228,162,514,264,337,593,543,950,335
// If you need something bigger, look up BigInteger
decimal maxDecimal = decimal.MaxValue;

// A float is a 32 bit number with a max value of 3.402823E+38 with 7 decimal positions
float maxFloat = float.MaxValue;

// A double is a 32 bit number with a max value of 1.797693134E+308 with 15 decimal positions
double maxDouble = double.MaxValue;

Console.WriteLine("Max Int : " + maxInt);
Console.WriteLine("Max Long : " + maxLong);
Console.WriteLine("Max Decimal : " + maxDecimal);
Console.WriteLine("Max Float : " + maxFloat);
Console.WriteLine("Max Double : " + maxDouble);

var anotherName = "Tom";
// anotherName = 2; Cannot implicitly convert a gype 'int' to a 'string'

Console.WriteLine("anotherName is a {0}", anotherName.GetTypeCode());

// Math

Console.WriteLine("5 + 3 = " + (5 + 3));
Console.WriteLine("5 - 3 = " + (5 - 3));
Console.WriteLine("5 * 3 = " + (5 * 3));
Console.WriteLine("5 / 3 = " + (5 / 3));
Console.WriteLine("5.2 % 3 = " + (5.2 % 3));

int i = 0;

Console.WriteLine("i++ = " + (i++));
Console.WriteLine("++i = " + (++i));
Console.WriteLine("i-- = " + (i--));
Console.WriteLine("--i = " + (--i));

Console.WriteLine("i +- 3 = " + (i +- 3));
Console.WriteLine("i -= 2 = " + (i -= 2));
Console.WriteLine("i *= 2 = " + (i *= 2));
Console.WriteLine("i /= 2 = " + (i /= 2));
Console.WriteLine("i %= 2 = " + (i %= 2));

// casting

// if no magnitude is lost, casting will happen automatically. But otherwise, you set it up as follows

double pi = 3.14;
int intPi = (int)pi;
Console.WriteLine("intPi = " + intPi);

// Math functions
// Acos, Asin, Atan, Atan2, Cos, Cosh, Exp, Log, Sin, Sinh, Tan, Tanh

double number1 = 10.5;
double number2 = 15;

Console.WriteLine("number1 is " + number1);
Console.WriteLine("number2 is " + number2);

Console.WriteLine("Math.Abs(number1) " + (Math.Abs(number1)));
Console.WriteLine("Math.Ceiling(number2) " + (Math.Ceiling(number1)));
Console.WriteLine("Math.Floor(number1) " + (Math.Floor(number1)));
Console.WriteLine("Math.Max(number1,number2) " + (Math.Max(number1,number2)));
Console.WriteLine("Math.Min(number1,number2) " + (Math.Min(number1,number2)));
Console.WriteLine("Math.Pow(number1, 2) " + (Math.Pow(number1, 2)));
Console.WriteLine("Math.Round(number1) " + (Math.Round(number1)));
Console.WriteLine("Math.Sqrt(number1) " + (Math.Sqrt(number1)));

// random numbers

Random rand = new Random();
Console.WriteLine("Random number between 1 and 10 is " + rand.Next(1, 11));

// Relational operators : > < >= <= == !=
// Logical operators : && || ^ !
// note: ^ is the exclusive or

Console.WriteLine("What is your child's age? (enter 0 - 18)");
int age = Console.Read();

if ((age >= 5) && (age <= 7))
{
Console.WriteLine("Go to Elementary School");
} else if ((age > 7) && (age <= 13))
{
Console.WriteLine("Go to middle school");
} else if ((age < 5) || (age > 13))
{
Console.WriteLine("Your child does not meet our age requirements.");
} else
{
Console.WriteLine("Go to high school");
}


Console.WriteLine("What is your age? ");
int workingAge = Console.Read();

if ((workingAge < 14) || (workingAge > 67))
{
Console.WriteLine("You shouldn't work.");
}

}


The program ignores the input from the following:

Console.WriteLine("What is your age? ");
int workingAge = Console.Read();


The output is:

What is your age?
You shouldn't work.


So, the program is not stopping for my input, but rather seems to process its conditions based on the previous integer input value of 2 or 5.

Other articles talked about doing the following which I tried with no avail:

Console.WriteLine("What is your age? ");
int workingAge = Convert.ToInt32(Console.Read());


And

Console.WriteLine("What is your age? ");
int workingAge = int32.Parse(Console.Read());


The second one produced an error in Visual Studio
The name 'int32' does not exist in the current context


I updated my script as follows and it works now thanks to your answers:

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

namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
// Single line comments
/* test multi-line comments
* asldkjasldkjaskd
* asldkjasldkja
* alsdkjalksdj
* */
Console.WriteLine("Hello world!");
Console.Write("What is your name? ");
string name = Console.ReadLine();
Console.WriteLine("Hello " + name);

bool canVote = true;

char grade = 'A';

// Integer with a max number of 2,147,483,647
int maxInt = int.MaxValue;

//Long with max value of 9,223,372,036,854,775,807
long maxLong = long.MaxValue;

// Decimal has a max value of 79,228,162,514,264,337,593,543,950,335
// If you need something bigger, look up BigInteger
decimal maxDecimal = decimal.MaxValue;

// A float is a 32 bit number with a max value of 3.402823E+38 with 7 decimal positions
float maxFloat = float.MaxValue;

// A double is a 32 bit number with a max value of 1.797693134E+308 with 15 decimal positions
double maxDouble = double.MaxValue;

Console.WriteLine("Max Int : " + maxInt);
Console.WriteLine("Max Long : " + maxLong);
Console.WriteLine("Max Decimal : " + maxDecimal);
Console.WriteLine("Max Float : " + maxFloat);
Console.WriteLine("Max Double : " + maxDouble);

var anotherName = "Tom";
// anotherName = 2; Cannot implicitly convert a gype 'int' to a 'string'

Console.WriteLine("anotherName is a {0}", anotherName.GetTypeCode());

// Math

Console.WriteLine("5 + 3 = " + (5 + 3));
Console.WriteLine("5 - 3 = " + (5 - 3));
Console.WriteLine("5 * 3 = " + (5 * 3));
Console.WriteLine("5 / 3 = " + (5 / 3));
Console.WriteLine("5.2 % 3 = " + (5.2 % 3));

int i = 0;

Console.WriteLine("i++ = " + (i++));
Console.WriteLine("++i = " + (++i));
Console.WriteLine("i-- = " + (i--));
Console.WriteLine("--i = " + (--i));

Console.WriteLine("i +- 3 = " + (i +- 3));
Console.WriteLine("i -= 2 = " + (i -= 2));
Console.WriteLine("i *= 2 = " + (i *= 2));
Console.WriteLine("i /= 2 = " + (i /= 2));
Console.WriteLine("i %= 2 = " + (i %= 2));

// casting

// if no magnitude is lost, casting will happen automatically. But otherwise, you set it up as follows

double pi = 3.14;
int intPi = (int)pi;
Console.WriteLine("intPi = " + intPi);

// Math functions
// Acos, Asin, Atan, Atan2, Cos, Cosh, Exp, Log, Sin, Sinh, Tan, Tanh

double number1 = 10.5;
double number2 = 15;

Console.WriteLine("number1 is " + number1);
Console.WriteLine("number2 is " + number2);

Console.WriteLine("Math.Abs(number1) " + (Math.Abs(number1)));
Console.WriteLine("Math.Ceiling(number2) " + (Math.Ceiling(number1)));
Console.WriteLine("Math.Floor(number1) " + (Math.Floor(number1)));
Console.WriteLine("Math.Max(number1,number2) " + (Math.Max(number1,number2)));
Console.WriteLine("Math.Min(number1,number2) " + (Math.Min(number1,number2)));
Console.WriteLine("Math.Pow(number1, 2) " + (Math.Pow(number1, 2)));
Console.WriteLine("Math.Round(number1) " + (Math.Round(number1)));
Console.WriteLine("Math.Sqrt(number1) " + (Math.Sqrt(number1)));

// random numbers

Random rand = new Random();
Console.WriteLine("Random number between 1 and 10 is " + rand.Next(1, 11));

// Relational operators : > < >= <= == !=
// Logical operators : && || ^ !
// note: ^ is the exclusive or

Console.WriteLine("What is your child's age? (enter 0 - 18)");
int age = Console.Read();

if ((age >= 5) && (age <= 7))
{
Console.WriteLine("Go to Elementary School");
} else if ((age > 7) && (age <= 13))
{
Console.WriteLine("Go to middle school");
} else if ((age < 5) || (age > 13))
{
Console.WriteLine("Your child does not meet our age requirements.");
} else
{
Console.WriteLine("Go to high school");
}


Console.WriteLine("What is your age? ");
int workingAge = Console.Read();

if ((workingAge < 14) || (workingAge > 67))
{
Console.WriteLine("You shouldn't work.");
}

}
}
}

Answer

.Read() reads only a single character. You probably want .ReadLine() instead, which reads all the characters up to Enter, and returns a string.

int workingAge = int.Parse(Console.ReadLine());