C # console user login

Well, I want to start first by saying that I am not a student, so this question has nothing to do with homework. I am trying to learn C # because the company I want to work with uses it. I heard that C # is very similar to java, so I use my java book, which has problems with exercises, to practice C #. Here is my question: I am trying to create a simple program in which the user enters 3 classes, and he stores it in an array, and then displays the three classes that have been entered. The problem is that it does not store grades. However, it displays some random number, for example, if I put 34, 44 and 54, it returns 51. Here is my code and thanks to everyone:

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Practice1 { class Program { static void Main(string[] args) { int[] test = new int[4]; int i = 1; for (i = 1; i <= 3; i++) { Console.WriteLine("Please enter test " + i); test[i] = Console.Read(); Console.ReadLine(); } for (i = 1; i <=3; i++) { Console.WriteLine(test[i]); Console.ReadLine(); } } } } 
+6
source share
7 answers

Your problem is here:

 test[i] = Console.Read(); 

This puts the character (which is the whole character code) in your test array.

Instead of this

 test[i] = int.Parse(Console.ReadLine()); 

Edit: If you are not sure if the user will type a syntactic integer, maybe they will type β€œsix”, for example, you might consider using try / catch (if you want to know why he will not parse), or int.TryParse, which returns true to indicate success, and assigns the parsed integer to the index of the variable, field or array:

 if(int.TryParse(Console.ReadLine(), out test[1]) Console.WriteLine("Successfully parsed integer"); else Console.WriteLine("Please enter an integer."); 
+8
source

Console.Read () returns the ASCII value of the entered key. For example, if you type β€œA”, you get a value of 65, which is the ASCII code for β€œA”.

You will need to parse your string to an integer:

 for (i = 0; i < 4; i++) { Console.WriteLine("Please enter test " + i); string input = Console.ReadLine(); int value; bool success = int.TryParse(input, out value); if (success) { test[i] = value } else { // Show an error message that the user must enter an integer. } Console.ReadLine(); } 

Also note that arrays are indexed starting at 0 in C #, not 1, as your code suggests.

Alternatively, you can still use Console.Read (), which returns an integer representation of the entered character, confirm that the character is actually a number and convert from ASCII code to the corresponding number.

+2
source

From docs Console.Read() "Reads the next character from standard input."

You want the following Integer, so something like

 bool cont = false; int val = 0; do { cont = int.TryParse(Console.ReadLine(), out val); if(!cont){Console.WriteLine( "please enter a real number you fool" );} } while (!cont); 

Must work.

+2
source
  int[] test = new int[3]; for (int i = 0; i < 3; i++) { Console.WriteLine("Please enter test " + i + 1); test[i] = Int.Parse(Console.ReadLine()); } for (int i = 0; i < 3; i++) { Console.WriteLine(test[i]); Console.ReadLine(); } 

As you can see, arrays start at index 0, so there is no need to define int [4] (another int than what is required), and you need to loop from index 0 to length-1

+2
source

The problem is that you are reading a character. So the β€œ51” you see is the decimal (base 10) ASCII value for number 3. What you need to do is this:

 string result = Console.ReadLine(); int grade = 0; int.TryParse(result, out grade) test[i] = grade; 
+2
source

Console.Read() returns a character. You want to read string from the console, convert it to int , and then store this value in your array.

+1
source

Here is the code:

int [] test = new int [3];

  for (int e = 0; e < 3; e++) { Console.WriteLine("Please enter test "); test[e] = int.Parse(Console.ReadLine()); } Console.WriteLine("000000000000000000000000000\n"); for (int e = 0; e < 3; e++) { Console.WriteLine(test[e]); //Console.ReadLine(); } 
0
source

Source: https://habr.com/ru/post/919280/


All Articles