RoxyP
RoxyP

Reputation: 1

Validate an input from user

I have to write a console program to obtain test scores and set the maximum value to be greater than 0 and no less than the highest score obtained but I am stuck on the validation of the "maxscore".

This is what I have so far:

int[] score = new int[5];
int highScore = 0;
int sum = 0;
int ave = 0;

//Requests the user to input 5 numbers
Console.WriteLine("Please enter 5 test scores:");

//Obtains the input and sets the highScore as well as the sum for all the number entered

for (int i = 0; i < 5; i++)
{
    score[i] = Int32.Parse(Console.ReadLine());
    if (i > highScore)
    {
        highScore = i;
        sum += score[i];
    }
}

//Requests the user to enter the max score
Console.WriteLine("Please enter the max score:");
int maxScore = Int32.Parse(Console.ReadLine());

Upvotes: 0

Views: 88

Answers (2)

Zein Makki
Zein Makki

Reputation: 30022

You need to differentiate between the index i and the score score[i]. i is the counter, which you are using as the index to the array element, while score[i] is the actual value of the score inside the array named score

for (int i = 0; i < 5; i++)
{
    score[i] = Int32.Parse(Console.ReadLine());
    if (score[i] > highScore)
    {
        highScore = score[i];
        sum += score[i];
    }
}

Upvotes: 1

ttaaoossuu
ttaaoossuu

Reputation: 7884

You have a mistake when comparing highScore with i instead of score[i]. And then you should also highScore = score[i].

Upvotes: 3

Related Questions