Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Console User Input

Tags:

c#

Ok, I want to first start off by saying I'm not a student so this question has nothing to do with homework at all. I'm trying to learn C# because the company that I want to work for uses it. I heard that C# is very similar to java so I'm using my java book that has exercise problems to practice c#. Here is my question, I'm trying to make a simple program that the user enters 3 grades and it stores it in an array and then displays the three grades that were entered. The problem is that its not storing the grades. It does however display some random number like if I put in 34, 44, and 54 it returns 51. Here is my code and thanks 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();
            }
        }
    }
}
like image 735
Brandon Avatar asked Jun 28 '12 20:06

Brandon


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


3 Answers

Your problem is here:

test[i] = Console.Read();

This is putting a character (which is an integer character code) into your test array.

Instead do

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

Edit: If you aren't certain that the user will type a parsable integer, maybe they'll type in "six", for example you might consider using a try/catch (if you want to know why it wouldn't parse), or the int.TryParse, which returns true to indicate success and assigns the parsed integer to a variable, field, or array index:

if(int.TryParse(Console.ReadLine(), out test[1])
   Console.WriteLine("Successfully parsed integer");
else
   Console.WriteLine("Please enter an integer.");
like image 199
agent-j Avatar answered Sep 20 '22 09:09

agent-j


        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 starts from index 0, so there is no need to define int[4] (one more int than required), and you need to loop from index 0 to length-1

like image 23
eyossi Avatar answered Sep 18 '22 09:09

eyossi


The problem is that you are reading in the character. As such the "51" you are seeing is the decimal (base 10) ASCII value for the number 3. What you need to do is the following:

string result = Console.ReadLine();
int grade = 0;
int.TryParse(result, out grade)
test[i] = grade;
like image 21
Adam Gritt Avatar answered Sep 18 '22 09:09

Adam Gritt