Sentinel controlled while loops C#

flimflam57

Not Homework..So I've got a simple console program to read exam scores and prints out the average and grade. So far it's the following:

public static void Main ()
    {
        int sum = 0;
        int count = 0;
        double average = 0;

        Console.WriteLine ("Enter all your exam scores one by one. When finished, enter -99");

        string scores = Console.ReadLine ();

        while (scores != "-99") {
            sum += int.Parse (scores);
            count++;
            scores = Console.ReadLine ();

        } 
        if (scores == "-99") {
            average = sum / count;
            if (average >= 90)
                Console.WriteLine ("Your average score is {0}. This is good for a letter grade of A", average);
                Console.WriteLine(....more scores etc...);

Now I want to check for invalid entries with TryParse. I thought I'd stick in another while loop before the other and change the original one like this:

 Console.WriteLine ("Enter all your exam scores one by one. When finished, enter -99");

        string scores = Console.ReadLine ();

          while (int.TryParse(scores, out numbers) == false){
               Console.WriteLine("Please enter a valid integer")
               scores = Console.ReadLine();
               sum += int.Parse(scores);
               count++;

        } 
          while (scores != "-99" && int.TryParse(scores, out numbers) == true) {
            sum += int.Parse (scores);
            count++;
            scores = Console.ReadLine ();

        } 
          if (scores == "-99") {
            average = sum / count;
            if (average >= 90)
                Console.WriteLine ("Your average score is {0}. This is good for a letter grade of A", average); ...etc...

The problem here is that if the user enters valid entries at first and then enters an invalid one, the compiler can't get back to the first while loop to check for the invalid entry. So I tried to swap the positions of the while loops. But this has the same effect; it can't get back to the first while loop to check for valid entries after an invalid one is entered. The answer is most likely simple, but I'm stuck.

JNYRanger

The issue you are having is that you break from the first loop when the TryParse returns true, but have no recourse to re-enter the loop. Instead you should nest your loops. The loop with the sentinel should be the outer loop, and the loop that validates and re-prompts the user should be the inner loop. Here is an example:

while(scores != "-99")
{
    scores = Console.ReadLine();

     while((int.TryParse(scores, out numbers) == false)
     {
        //validation failed, re-prompt user for better number
        Console.WriteLine("Bad value, try again")
        scores = Console.ReadLine()
     }

     //do stuff here with the valid score value
  }  

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Converting for loops to while loops in python

From Dev

How to alter my program to use while loops instead of for loops. Asterisk triangle in c

From Dev

Optimising while loops in C?

From Dev

SQL WHILE Loops

From Dev

Stepper motor controlled by C#

From Dev

While Loops Syntax Errors

From Dev

C program counting coins with while loops

From Dev

Data controlled programs in c++

From Dev

writing python for loops as while?

From Dev

Why is UI unresponsive while being programmatically controlled?

From Dev

Flag controlled while loop to search txt file

From Dev

While loop in C - read untiL sentinel variations

From Dev

Need help adding controlled loops in my python code

From Dev

While & Sentinel Values

From Dev

How to alter my program to use while loops instead of for loops. Asterisk triangle in c

From Dev

Optimising while loops in C?

From Dev

C - Strange behaviour of while and for loops

From Dev

Comparing strings from two files within two while loops in C

From Dev

Need to return a value from a do/while and multiple while loops - C

From Dev

PHP While Loops In While Loops not working

From Dev

C - Senitel controlled loop

From Dev

C Code; nested do while loops with inputs

From Dev

C++ sentinel in a for and while loop with a count. Stuck in infinite loop

From Dev

Issue while using Cartalyst Sentinel in Laravel 5.1

From Dev

c++ declaration and initialization variables inside while loops

From Dev

Factorials with count-controlled or sentinel loops in java

From Dev

Using iter() with sentinel to replace while loops

From Dev

Does C have enumeration-controlled loops?

From Dev

Nested While Loops in C

Related Related

HotTag

Archive