C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 7th, 2013, 10:00 PM
devdoc345 devdoc345 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 8 devdoc345 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 14 m 34 sec
Reputation 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);   
        }
    }
}

Reply With Quote
  #2  
Old March 7th, 2013, 11:50 PM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,905 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 4 Days 1 h 9 m 41 sec
Reputation Power: 1774
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

Reply With Quote
  #3  
Old March 8th, 2013, 12:15 AM
devdoc345 devdoc345 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 8 devdoc345 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 14 m 34 sec
Reputation Power: 0
fixed

Reply With Quote
  #4  
Old March 8th, 2013, 12:19 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,905 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 4 Days 1 h 9 m 41 sec
Reputation Power: 1774
> 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.

Reply With Quote
  #5  
Old March 8th, 2013, 12:51 AM
devdoc345 devdoc345 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 8 devdoc345 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 14 m 34 sec
Reputation 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);   
        }
    }
}

Reply With Quote
  #6  
Old March 8th, 2013, 08:42 AM
bullet's Avatar
bullet bullet is offline
Java Junkie
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2004
Location: Mobile, Alabama
Posts: 3,828 bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 6 Days 11 h 21 m 4 sec
Reputation Power: 1248
Send a message via ICQ to bullet Send a message via AIM to bullet Send a message via MSN to bullet
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Sentinal Controled loop

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap