The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Sentinal Controled loop
Discuss Sentinal Controled loop in the C Programming forum on Dev Shed. Sentinal Controled loop C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

March 7th, 2013, 10:00 PM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 8
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);
}
}
}
|

March 7th, 2013, 11:50 PM
|
 |
Contributed User
|
|
|
|
|
Please edit your post and put [code][/code] tags around your code.
Copy again from your code editor so indentation is preserved.
|

March 8th, 2013, 12:15 AM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 8
Time spent in forums: 3 h 14 m 34 sec
Reputation Power: 0
|
|
|
fixed
|

March 8th, 2013, 12:19 AM
|
 |
Contributed User
|
|
|
|
> 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.
|

March 8th, 2013, 12:51 AM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 8
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);
}
}
}
|

March 8th, 2013, 08:42 AM
|
 |
Java Junkie
|
|
Join Date: Jan 2004
Location: Mobile, Alabama
|
|
|
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|