0
  1. I have to put in an extra "test score" to get an answer. (i.e i have to enter six 5' to get 25)
  2. I can't get the do/while & if statements to loop if there is more than one number outside the "while" range. I haven't been coding for very long, a couple weeks so try and break down the answers. Thanks for the help!

Here is my code

Console.Write("Enter the number of tests: ");
int n = int.Parse(Console.ReadLine());
int[] scores = new int[n];
Console.WriteLine("----------------------------------------");
Console.WriteLine("Please enter the test scores");
int i;

do
{
    i = Convert.ToInt32(Console.ReadLine());
    if (i < 0)
    {
        Console.WriteLine("Please enter a value greater than 0");

     }

    if (i > 100)
    {
        Console.WriteLine("Please enter a value less than 100");

    }
} while (i < 0 || i > 100);


for (i = 0; i < n; i++)
{
    scores[i] = int.Parse(Console.ReadLine());

}
int sum = 0;
foreach (int d in scores)
{
    sum += d;
}
Console.WriteLine("The sum of all the scores is {0}",sum);
Console.ReadLine();
D-Shih
  • 44,943
  • 6
  • 31
  • 51
  • `do ... while` will always run at least once. – mcalex Mar 02 '18 at 03:09
  • Your `for (i = 0; i < n; i++)` uses the variable `i` - so it overwrites the value you collected in the `do`/`while` loop. – Enigmativity Mar 02 '18 at 03:10
  • Thanks guys! I didn't realize my "for" line was doing that, So thats why it made sense to move the for loop up. I thought it was going to collect before the check. Hence me being a noob. Thanks again. – Dan Zaborowski Mar 02 '18 at 03:18
  • Inside the `do` if a valid number is given, add it to the score array. – CodingYoshi Mar 02 '18 at 03:32
  • You also have tests like `i < 0` but the error messages say `"value greater than 0"`. It's ambiguous with regard to `0` itself. The same with the `100` end. – Enigmativity Mar 02 '18 at 03:32

4 Answers4

2

Put the do block that does the input validation inside the for loop:

Console.Write("Enter the number of tests: ");
int n = int.Parse(Console.ReadLine());
int[] scores = new int[n];

Console.WriteLine("----------------------------------------");
Console.WriteLine("Please enter the test scores");

for (int i = 0; i < n; i++)
{
    int input = -1;
    do
    {
        input = Convert.ToInt32(Console.ReadLine());
        if (input < 0)
        {
            Console.WriteLine("Please enter a value greater than 0");    
        }    
        else if (input > 100)
        {
            Console.WriteLine("Please enter a value less than 100");    
        }
    } while (input < 0 || input > 100);

    scores[i] = input;
}

int sum = 0;
foreach (int d in scores)
{
    sum += d;
}

Console.WriteLine("The sum of all the scores is {0}", sum);
Console.ReadLine();
Samantha
  • 975
  • 6
  • 20
1

n is the number of tests, meaning the amount of scores counter i should be the same as the amount of tests. Also prefer Convert.ToInt32 to int.Parse since it throws exceptions in case it isn't able to make the conversion.

Console.Write("Enter the number of tests: ");
int n = Convert.ToInt32(Console.ReadLine());
int sum = 0, i = 0, score = 0;
int[] scores = new int[n];
Console.WriteLine("----------------------------------------");

do {
    Console.WriteLine("Please enter the test score #" + (i + 1));
    score = Convert.ToInt32(Console.ReadLine());

    if (score < 0) {
        Console.WriteLine("Please enter a value greater or equal to 0");
    }
    else if (score > 100)
    {
        Console.WriteLine("Please enter a value less or equal to 100");
    }
    else {
        scores[i] = score;
        i++;
    }
} while (i < n);

foreach (int d in scores) {
    sum += d;
}

Console.WriteLine("The sum of all the scores is {0}", sum);
Console.ReadLine();
Tiramonium
  • 557
  • 5
  • 15
  • @Enigmativity Did you? It was late when I wrote that, and only later I realized that OP wanted a infinite loop while the provided score was less than 0 or more than 100, which makes absolutely no sense. So now I moved the counter increment to the "right" place according to what he wanted. – Tiramonium Mar 02 '18 at 11:26
-1

Just for the potential learning opportunity, and and not as a direct answer to your question, here is the way I would do this exercise:

Console.Write("Enter the number of tests: ");
int n = int.Parse(Console.ReadLine());

Console.WriteLine("----------------------------------------");
Console.WriteLine("Please enter the test scores");

int AskForInteger()
{
    while (true)
    {
        if (int.TryParse(Console.ReadLine(), out int i) && i >= 0 && i <= 100)
        {
            return i;
        }
        Console.WriteLine("Please enter a value between 0 and 100 inclusive");
    }
}

int[] scores =
    Enumerable
        .Range(0, n)
        .Select(x => AskForInteger())
        .ToArray();

Console.WriteLine("The sum of all the scores is {0}", scores.Sum());
Console.ReadLine();
Enigmativity
  • 113,464
  • 11
  • 89
  • 172
  • This is awesome. I'm not too familiar with Linq so I'm going to save this for a reference once I get to that point considering its a basic application of Linq. Thank you. – Dan Zaborowski Mar 02 '18 at 15:26
-2

There are two options:

a. Start at 1 rather than 0 --> for (i = 1; i < n; i++)

b. Lower the value of n by 1 --> for (i = 1; i < n-1; i++)

Good luck