Sentinel Controlled Loop

Mar 4, 2009 at 3:46am
I am trying to write a program in Visual C++ 2008 Express Edition that accepts sales amounts input from the keyboard and counts the sales that are at or above the target amount or below the target amount. I am getting an error when trying to debug. I have to set a target amount. This is what I have:

#include <iostream>
using namespace std;

const float targetAmount = 45.00;
const int sentinel = 0;

int main()
{
int salesAtOrAboveTA, salesBelowTA, Count; // Declare variables as integer
float salesAmount; // Declare variable as float
salesAmount = 0; // Initialize loop variable
Count = 0;

cin >> salesAmount;
while (salesAmount != sentinel)
{
Count++;
cin >> salesAmount;
}
if (salesAmount >= targetAmount)
{
cout << "Number of sales at or above target: " << salesAtOrAboveTA << endl;
}
else
cout << "Number of sales below target: " << salesBelowTA << endl;

return 0;
}
Mar 4, 2009 at 4:03am
Let me guess. You enter an infinite loop.

http://www.cplusplus.com/forum/articles/6046/
Mar 4, 2009 at 4:11am
I don't see the infinite loop, but you're trying to cout salesAtOrAboveTa and salesBelowTa without initializing them. You only declare them:

int salesAtOrAboveTA, salesBelowTA,
Mar 4, 2009 at 4:15am
I actually did not get to the debugging stage. When I try to rebuild I get these error messages:

warning C4700: uninitialized local variable 'salesAtOrAboveTA' used
warning C4700: uninitialized local variable 'salesBelowTA' used

Should I have set these variables equal to zero before my 'while' statement?
Mar 4, 2009 at 4:19am
Those variables are what I'm referring to. In the below lines, you are attempting to output the variables that have no values:

1
2
cout << "Number of sales at or above target: " << salesAtOrAboveTA << endl;
cout << "Number of sales below target: " << salesBelowTA << endl;


They need some sort of value. Just initialize them to 0 until you put them to use.
Mar 4, 2009 at 4:22am
Thanks for all of the feedback. Initializing them to 0 got rid of the errors.
Topic archived. No new replies allowed.