1

I am trying to get input from user 5 times and add those values to marks array;

Then, it will calculate the average and print positive or negative accordingly. However, I can not take input from the user it just prints "Enter 5 elements". After getting input from user how can I add them to marks array? Any tips would be helpful.

using System;

    using System.Collections.Generic;
    using System.Linq;
    using System.Text.RegularExpressions;
    class Program
    {
        static void Main()
        {
                double average =0;
                int [] marks = new int[] { };

                 for (int a = 0; a < 5; a++){
                    Console.WriteLine("Enter 5 elements:"); 
                string line = Console.ReadLine(); 
                 Console.WriteLine(line);

            }
                for (int i = 0; i < marks.Length; i++){
                    average = marks.Average();
            }
                if(average>0){
                    Console.WriteLine("Positive");
                }else{
                    Console.WriteLine("Negative");
                }           
        }
    }
2
  • You should do something with "line" after reading it. int.TryParse(...) ought to help you. Also note that you're currently initializing a zero-lenghth array, so you might want to change it to int[] marks = new int[5]; Commented May 4, 2018 at 7:40
  • Depending on how you input the values from the console, it will also make it easier if you input the values comma-delimited. then you can go string[] values = InputStr.split(','); - which is then all string values and do what John said. (Or space- delimited... however you prefer) Commented May 4, 2018 at 7:46

4 Answers 4

2

I would use a while loop combined with int.TryParse to check if the user input is a number. Also it doesn't make any sense to put average = marks.Average(); inside a for loop, because LINQ Average calculates the average of a collection (in your case the marks array).

static void Main()
{
    int[] marks = new int[5];

    int a = 0;

    Console.WriteLine("Enter 5 elements:");

    while (a < 5)
    {
        if (int.TryParse(Console.ReadLine(), out marks[a]))
            a++;
        else
            Console.WriteLine("You didn't enter a number! Please enter again!");
    }

    double average = marks.Average();

    if (average > 0)
        Console.WriteLine("Positive");
    else
        Console.WriteLine("Negative");
}

DEMO HERE

Sign up to request clarification or add additional context in comments.

Comments

1

Edited my answer to illustrate solving your problem without a for loop.

class Program
{
    const int numberOfMarks = 5;
    static void Main()
    {
        List<int> marks = new List<int>();
        Enumerable.Range(1, numberOfMarks)
        .ForEach((i) => {
            Console.Write($"Enter element {i}:");
            marks.Add(int.TryParse(Console.ReadLine(), out var valueRead) ? valueRead : 0);
            Console.WriteLine($" {valueRead}");
        });
        Console.WriteLine(marks.Average() >= 0 ? "Positive" : "Negative");
    }
}

1 Comment

forgot to mention, it needs MoreLinq for the ForEach. :)
0

This will help you, just copy and paste it. There are some explanation with comments.

class Program
{
    static void Main()
    {
        const int numberOfMarks = 5;
        int[] marks = new int[numberOfMarks];

        Console.WriteLine("Enter 5 elements:");
        for (int a = 0; a < numberOfMarks; a++)
        {
            // If entered character not a number, give a chance to try again till number not entered
            while(!int.TryParse(Console.ReadLine(), out marks[a]))
            {
                Console.WriteLine("Entered not a character");
            }

            Console.WriteLine("You entered : " + marks[a]);
        }

        // Have to call Average only once.
        var avg = marks.Average();
        Console.WriteLine(avg > 0 ? "Positive average" : "Negative average");
        Console.ReadLine();
    }
}

4 Comments

A little tip : you don't need the intTemp just use out marks[a] in the TryParse. And you can use it after in the Console.WriteLine method
That's good point, you are right. Edited my Answer. And realized, almost the same as your code here.
It's a simple problem so there are not many solutions to this.
I will upvote you because beside of me you only have a decent answer here
0

Follow Stackoverflow Answer

since integer array is being used, and as input from the console is a string value, you need to convert it using Parse() Method. For e.g.

string words = "83";

int number = int.Parse(words);

Edit: using string variable in parsing.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.