#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    8
    Rep Power
    0

    Sentinal Controled loop


    So i am writing a console based app useing a sentinal controled loop but when i write a while statment with
    while (inValue != "xxx") ;
    Nothing runs
    but
    while (inValue == "xxx") ;
    This does work but its not what i want to do.
    The question is Why is it not running while useing != ?
    Here is the code :
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace CH_6_ex3
    {
        class Ch6_ex3
        {
            static void Main(string[] args)
            {
                // declrations
                int totalAvg = inputAvg();
                displayAvg(totalAvg);
                
            }
    
            public static int inputAvg()
            {
    
                //declrations
                string inValue = "";
                int score;
                int count = 0;
    
                // sentinal control loop
                while (inValue != "-99") ;
    
                // count is for the average count
                count++;
    
                // input
                Console.WriteLine("Please Enter score 0 - 100, Enter xxx to quit");
                inValue = Console.ReadLine();
                score = Convert.ToInt32(inValue);
    
                // tempary test to make sure it is working
                Console.WriteLine("This is your score {0}", score);
    
                // return statment
                return score / (count * 100);
            }
            public static void displayAvg(int totalAvg)
            {
    
                // output / bool to determine grade
                if (totalAvg > 89)
                    Console.WriteLine("Your current grade is an A and your average score is: " + totalAvg);
                else if (totalAvg > 79)
                    Console.WriteLine("Your current grade is a B and your average score is: " + totalAvg);
                else if (totalAvg > 69)
                    Console.WriteLine("Your current grade is a C and your average score is: " + totalAvg);
                else if (totalAvg > 59)
                    Console.WriteLine("Your current grade is a D and your average score is: " + totalAvg);
                else
                    Console.WriteLine("Your current grade is a F and your average score is: " + totalAvg);   
            }
        }
    }
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,367
    Rep Power
    1870
    Please edit your post and put [code][/code] tags around your code.
    Copy again from your code editor so indentation is preserved.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    8
    Rep Power
    0
    fixed
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,367
    Rep Power
    1870
    > while (inValue != "-99") ;
    What's that ; doing at the end?
    It seems to me, you've written
    Code:
    while (inValue != "-99")  {
        // do nothing at all
    }
    If the condition is false, nothing happens.
    If the condition is true, then that's where it will remain stuck for the rest of time.

    Replace the ; with { } around the code you want to loop.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    8
    Rep Power
    0
    Ok so i got that fixed, now i have this error, where it says score is an unassiqned variable
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace CH_6_ex3
    {
        class Ch6_ex3
        {
            static void Main(string[] args)
            {
                // declrations
                //int totalAvg = inputAvg();
                //displayAvg(totalAvg);
                
            }
    
            public static int inputAvg()
            {
    
                //declrations
                string inValue = "";
                int score;
                int count = 0;
                int avg;
    
                // sentinal control loop
                while (inValue != "-99")
                {
                    // count is for the average count
                    count++;
    
                    // input
                    Console.WriteLine("Please Enter score 0 - 100, Enter -99 to quit");
                    inValue = Console.ReadLine();
                    score = Convert.ToInt32(inValue);
                }
                avg = score / (count * 100);
                return avg;
            }
            public static void displayAvg(int totalAvg)
            {
    
                // output / bool to determine grade
                if (totalAvg > 89)
                    Console.WriteLine("Your current grade is an A and your average score is: " + totalAvg);
                else if (totalAvg > 79)
                    Console.WriteLine("Your current grade is a B and your average score is: " + totalAvg);
                else if (totalAvg > 69)
                    Console.WriteLine("Your current grade is a C and your average score is: " + totalAvg);
                else if (totalAvg > 59)
                    Console.WriteLine("Your current grade is a D and your average score is: " + totalAvg);
                else
                    Console.WriteLine("Your current grade is a F and your average score is: " + totalAvg);   
            }
        }
    }
  10. #6
  11. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,020
    Rep Power
    1285
    In inputAvg, you define score, and don't give it an initial value. You set it's value inside the while loop, but it's possible the body of the loop won't be executed.

IMN logo majestic logo threadwatch logo seochat tools logo