Is there a way to enter number without showing them immediately? - c#

I want to enter 10 numbers, and then show them in 1 line like this:
1,4,5,2,456,23,... and so on..
and it keeps writing them as I am entering them, and in the end when it's supposed to show all numbers in 1 line it shows only the last one.
I know it's possible with random numbers but when I enter them on my own I don't know how not to show them at all or keep them in 1 line and if it is even possible?
int a;
int x;
Console.WriteLine("a:");
a = int.Parse(Console.ReadLine());
x = 10;
for (int i = 0; i < x; i++)
{
a = int.Parse(Console.ReadLine());
}
Console.ReadKey();

you can use
Console.ReadKey(true)
it reads a key from console and does not show it.
you can use this to read word from console without showing it
public static string ReadHiddenFromConsole()
{
var word = new StringBuilder();
while (true)
{
var i = Console.ReadKey(true);
if (i.Key == ConsoleKey.Enter)
{
Console.WriteLine();
break;
}
if (i.Key == ConsoleKey.Backspace)
{
if (word.Length > 0)
{
word.Remove(word.Length - 1, 1);
Console.Write("\b \b");
}
}
else
{
word.Append(i.KeyChar);
Console.Write("*");
}
}
return word.ToString();
}

You can use this code:
static void Main(string[] args)
{
int length = 10;
int[] myNumbers = new int[length];
for (int i = 0; i < length; i++)
{
Console.Write("Enter number:" );
myNumbers[i] = Convert.ToInt32(Console.ReadLine());
Console.Clear();
}
Console.WriteLine("Your numbers: {0}", string.Join(",", myNumbers));
}

i dont know how to write them at the end all in 1 line?
Well you need to save them as they are entered:
int num;
var nums = new List<int>();
while (nums.Count < 10)
{
Console.Write("Enter: ");
if (int.TryParse(Console.ReadLine(), out num))
{
nums.Add(num);
Console.Clear();
}
}
Console.WriteLine(string.Join(", ", nums));

Related

Read input different menu c#

Im trying to make a menu where an array will store input from a user (enterednumber 1) and then an admin can check the contents of the array in a different menu(enterednumber 9), but I can't seem to make it readable inside the admin menu.... I've left out the methods for login & startmenu, that part works. - the relevant part of the code currently looks like this:
static void Main(string[] args)
{
while (true)
{
int enteredNumber;
int[] myArray = new int[10];
Startmenu();
enteredNumber = Convert.ToInt32(Console.ReadLine());
if (enteredNumber == 1)
{
for (int i = 0; i < 1; i++)
{
Console.WriteLine("Insert Number:");
myArray[i] = Convert.ToInt32(Console.ReadLine());
}
Console.Clear();
Console.WriteLine("blabla");
Thread.Sleep(2000);
Console.Clear();
}
if (enteredNumber == 9)
{
if (Login(1234, 3) == true)
{
foreach (int number in myArray)
{
Console.WriteLine(number);
}
}
}
}
}
try this:
static void Main(string[] args)
{
int[] myArray = new int[10];
while (true)
{
int enteredNumber;
// int[] myArray = new int[10]; //when you create a new int array , the array not contain values.
Startmenu();
enteredNumber = Convert.ToInt32(Console.ReadLine());
if (enteredNumber == 1)
{
for (int i = 0; i < 1; i++)
{
Console.WriteLine("Insert Number:");
myArray[i] = Convert.ToInt32(Console.ReadLine());
}
Console.Clear();
Console.WriteLine("blabla");
Thread.Sleep(2000);
Console.Clear();
}
if (enteredNumber == 9)
{
if (Login(1234, 3) == true)
{
foreach (int number in myArray)
{
Console.WriteLine(number);
}
}
}
}
}
when you create a new int array, the array contains no values, you have creation of an array within the while loop, then, int [] myArray = new int [10]; this empty when you start the cycle again, place the creation before starting the loop.

C# How do I use a variable outside of a loop that changes from string to int inside the loop?

The code:
string[] numbers = new string[2];
for (int i = 0; i < numbers.Length; i++)
{
numbers[i] = Console.ReadLine();
if (int.TryParse(numbers[i], out int numberTry) && i == 0)
Console.WriteLine("That would be a number yes.");
else if (int.TryParse(numbers[i], out numberTry))
Console.WriteLine("Lovely work! That is indeed two numbers!");
else
{
Console.Clear();
Console.WriteLine("That's not a number. I am dissapointed.");
Console.ReadKey();
Environment.Exit(0);
}
}
My problem here is that I can't use numbers[] outside of the loop as an int because it chancges from a string to an int inside of the loop. I need to be able to send it with a method as an int to perform a operation on numbers[0] and numbers[1].
Thanks in advance!
Could you try this. It should work.
int[] numbers = new int[2];
for (int i = 0; i < numbers.Length; i++)
{
var number = Console.ReadLine();
if (int.TryParse(number, out int numberTry) && i == 0)
{
Console.WriteLine("That would be a number yes.");
numbers[i] = numberTry;
}
else if (int.TryParse(number, out numberTry))
{
Console.WriteLine("Lovely work! That is indeed two numbers!");
numbers[i] = numberTry;
}
else
{
Console.Clear();
Console.WriteLine("That's not a number. I am dissapointed.");
Console.ReadKey();
Environment.Exit(0);
}
}
Console.ReadKey();

Error in parsing an input string to int value in c#

First if works great
Second if throws an exception
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter a number and click enter, continue doing this process ");
Console.WriteLine("When you finish, just click enter without giving any input");
int i = 0;
int[] numbersArray;
List<int> numbersList = new List<int>();
while (true)
{
String numInput = Console.ReadLine();
numbersList.Add(Int32.Parse(numInput));
numbersArray = numbersList.ToArray();
if (i >= 1)
{
if (numbersArray[i] < numbersArray[i - 1])
{
Console.WriteLine("Your series is not going up!");
break;
Environment.Exit(0);
}
if (numbersArray[i] > numbersArray[i - 1])
{
if (numInput == "") {
break;
}
}
}
i++;
}
Console.WriteLine("You entered this series: ");
for (int j = 0; j < numbersArray.Length; j++)
{
Console.WriteLine(" " + numbersArray[j]);
}
Console.WriteLine("The length of the series youve entered is: " + numbersArray.Length);
}
}
You can't parse a string wihout digits like numInput = ""
EDIT: Try this code:
static void Main(string[] args)
{
Console.WriteLine("Enter a number and click enter, continue doing this process ");
Console.WriteLine("When you finish, just click enter without giving any input");
int i = 0;
int[] numbersArray = new []{1};
List<int> numbersList = new List<int>();
while (true)
{
String numInput = Console.ReadLine();
if (numInput == null || !numInput.All(char.IsDigit)) continue;
if (numInput != "")
{
numbersList.Add(Int32.Parse(numInput));
numbersArray = numbersList.ToArray();
if (i >= 1)
{
if (numbersArray[i] < numbersArray[i - 1])
{
Console.WriteLine("Your series is not going up!");
break;
Environment.Exit(0); // <-- Code is unreachable!
}
}
i++;
}
else if(i >= 1)
{
break;
}
}
Console.WriteLine("You entered this series: ");
foreach (int t in numbersArray)
{
Console.WriteLine(" " + t);
}
Console.WriteLine("The length of the series youve entered is: " + numbersArray.Length);
Console.ReadLine();
}
I assume you are trying to look at a index not existing.
Not sure of your language but I guess numbersArray[0] is the first index and numbersArray[1] is the second. So when you input your first number then you try to look at numbersArray[1] which doesn't exist.

Console (Input string was not in correct format)

Hi guys so i'm starting to learn C# and I came up with this problem when I was trying to mix things up
It says "Input string was not in correct format" at the
n = Convert.ToInt32(Console.ReadLine());
Here's the whole code
namespace Exercise13
{
class Program
{
static void Main(string[] args)
{
char choice;
Console.Write("What operation would you like to use?");
Console.WriteLine("\na. Addition \tb. Subtraction \tc.Multiplication \td.Division");
choice = (char)Console.Read();
if (choice == 'a')
{
sumValues();
}
else if (choice == 'b')
{
minusValues();
}
else if (choice == 'c')
{
timesValues();
}
Console.ReadLine();
}
static void sumValues()
{
int n = 0;
int sum = 0;
int i = 0,val = 0;
Console.Write("How many numbers do you want calculate: ");
n = Convert.ToInt32(Console.ReadLine());
for (i = 0; i < n; i++)
{
Console.Write("\nInput number: ");
val = Convert.ToInt32(Console.ReadLine());
sum += val;
}
Console.Write("\nThe Answer is: "+sum);
}
static void minusValues()
{
int diff = 0, m, z, value;
Console.Write("How many numbers do you want calculate: ");
m = int.Parse(Console.ReadLine());
for (z = 0; z < m; z++)
{
Console.Write("\nInput number: ");
value = int.Parse(Console.ReadLine());
diff -= value;
}
Console.Write("\nThe Answer is: " + diff);
}
static void timesValues()
{
int prod = 0, e, i, val;
Console.Write("How many numbers do you want to calculate: ");
e = Convert.ToInt32(Console.ReadLine());
for (i = 0; i < e; i++)
{
Console.Write("\nInput number: ");
val = int.Parse(Console.ReadLine());
prod *= val;
}
Console.Write("\nThe answer is: " + prod);
}
}
}
Use Integer.TryParse to handle the strings potentially not being numbers. Then prompt the user if the input is not parsable to enter valid input.
Convert and Parse both will throw exceptions if the string is not an exact number.
https://msdn.microsoft.com/en-us/library/f02979c7%28v=vs.110%29.aspx
int n = 0;
int sum = 0;
int i = 0,val = 0;
Console.Write("How many numbers do you want calculate: ");
var isValidNumber = Int32.TryParse(Console.ReadLine(), out n);
if(!isValidNumber) {
Console.WriteLine("Invalid number entered!");
}
else {
//Use the number
}

When input is 0 stop taking values

I need code, that takes inputs from user and then adds them together-simple. But I can't find a way, to take inputs until 0 is pressed and than add the numbers together..
So far, I made it take 10 values, but like I said it needs to be custom.. Thanks for your help.
int[] myarray = new int[10];
for (int i = 0; i < 10; i++)
{
myarray[i] = Convert.ToInt32(Console.ReadLine());
}
int a = 0;
for (int j = 0; j < 10; j++)
{
a = a + myarray[j];
}
Console.WriteLine(a);
Console.ReadLine();
The code below is not limited to 10 inputs, you can give as many input as you like
int sum=0, input;
do
{
input = Convert.ToInt32(Console.ReadLine());
sum += input;
}
while(input != 0);
Console.WriteLine(sum);
Console.ReadLine();
Check the input before adding it, and break out of the loop if it is 0:
int input = Convert.ToInt32(Console.ReadLine());
if(input == 0)
{
break;
}
myarray[i] = input;
As you don't know the length of the array, I'd recommend using a list. I've also added a tryparse to cope with dodgy user input. You can use Sum() on the list to avoid writing out another loop.
IList<int> myList = new List<int>();
string userInput = "";
int myInt = 0;
while (userInput != "0")
{
userInput = Console.ReadLine();
if(Int32.TryParse(userInput, out myInt) && myInt > 0)
{
myList.Add(myInt);
}
}
Console.WriteLine(myList.Sum());
Console.ReadLine();
When you have an unknown size array you should use a list.
var ls = new List<int>();
while(true)
{
var input = Convert.ToInt32(Console.ReadLine());
if(input == 0)
break;
ls.Add(input);
}
Lists by MSDN
You could use something like this :
while(true)
{
int input = Convert.ToInt32(Console.ReadLine());
if(input == 0)
break;
//do you math here
}

Categories