PDA

View Full Version : bug in simple program




crazyg
Jun 24, 2006, 09:16 PM
basically with this program you enter a customer name (Exit to stop processing) for each customer input amount spent on each service, and you display the customers purchases. But if i enter the number 0 the next customer that i enter something in the field that was 0 in the prvious statement does not show in the customers purchases.

double hairStyle[10] = {0};
double hairColour[10] = {0};
double manicure[10] = {0};
double total[10] = {0};
int stylepos = 0, int colourPos = 0, int maniPos = 0, int totalPos = 0;
int indexStyle = 0, int indexColour = 0, int indexManicure = 0, int indexTotal = 0 ;
int count = 0;
int numberOfTimesEntered = 0;

string name;
cout << "Enter customer's Name: ";
cin >> name;
while(name != "Exit")
{
cout << endl << endl;
cout << fixed << showpoint;
double styleIn;
cout << "Enter the price for this customer's hair style : ";
cin >> styleIn;
while(styleIn < 20.00 || styleIn > 200.00){
if(styleIn == 0){
break;
}
cout << "Price entered is out of range, Please re-enter price" << endl;
cout << "Enter the price for this customer's hair style : ";
cin >> styleIn;
}
hairStyle[stylepos++] = styleIn;
double colourIn;
cout << "Enter the price for this customer's hair colour : ";
cin >> colourIn;
while(colourIn < 20.00 || colourIn > 200.00){
if(colourIn == 0){
break;
}
cout << "Price entered is out of range, Please re-enter price" << endl;
cout << "Enter the price for this customer's hair colour : ";
cin >> colourIn;
}
hairColour[colourPos++] = colourIn;
double maniIn;
cout << "Enter the price for this customer's manicure : ";
cin >> maniIn;
while(maniIn < 20.00 || maniIn > 200.00){
if(maniIn == 0){
break;
}
cout << "Price entered is out of range, Please re-enter price" << endl;
cout << "Enter the price for this customer's manicure : ";
cin >> maniIn;
}
cout << endl << endl;
cout << setw(35) << "SALES SUMMARY FOR " << name << endl;
cout << setw(49) << "================================" << endl;
manicure[maniPos++] = maniIn;
cout << setprecision(2);
while (hairStyle[ indexStyle ] != 0){
cout << setw(26) << "Style" << setw(7) << " : " << setw(3) << " $ ";
cout << setw(6) << hairStyle[indexStyle++] << endl;
}
while (hairColour[ indexColour ] != 0){
cout << setw(27) << "Colour" << setw(6) << " : " << setw(3) << " $ ";
cout << setw(6) << hairColour[indexColour++] << endl;
}
while (manicure[ indexManicure ] != 0){
cout << setw(29) << "Manicure" << setw(4) << " : " << setw(3) << " $ ";
cout << setw(6) << manicure[indexManicure++] << endl;
}
double totalIn = styleIn + colourIn + maniIn;
total[totalPos++] = totalIn;
while (total[ indexTotal ] != 0){
cout << setw(49) << "--------------------------------" << endl;
cout << setw(26) << "Total" << setw(7) << " : " << setw(3) << " $ ";
cout << total[indexTotal++] << endl;
cout << setw(49) << "================================" << endl;
cout << endl << endl;
}

cout << "Enter customer's Name: ";
cin >> name;
numberOfTimesEntered++;
}
i suspect it has something to do with my int indexStyle = 0, int indexColour = 0, int indexManicure = 0, int indexTotal = 0 ;but i cant seem to figure it out,.can anyone help me



crazytom
Jun 24, 2006, 10:01 PM
In your while loops, you're looping until you hit a 0 entry...maybe you should initialize to -1 (or a unique number you'd never use for a real customer).

crazyg
Jun 24, 2006, 10:25 PM
In your while loops, you're looping until you hit a 0 entry...maybe you should initialize to -1 (or a unique number you'd never use for a real customer).
if i change the while loops from !=0 to -1 it loops forever dont know why and if i cahnge it toanother number it shows that number. Is that what i was supposed to do, still does not work,

crazyg
Jun 25, 2006, 12:31 AM
so the problem is here does anyone know of a better way to display the customer sale summary than this
while (hairStyle[ indexStyle ] != 0){
cout << setw(26) << "Style" << setw(7) << " : " << setw(3) << " $ ";
cout << setw(6) << hairStyle[indexStyle++] << endl;
}
while (hairColour[ indexColour ] != 0){
cout << setw(27) << "Colour" << setw(6) << " : " << setw(3) << " $ ";
cout << setw(6) << hairColour[indexColour++] << endl;
}
while (manicure[ indexManicure ] != 0){
cout << setw(29) << "Manicure" << setw(4) << " : " << setw(3) << " $ ";
cout << setw(6) << manicure[indexManicure++] << endl;
}
double totalIn = styleIn + colourIn + maniIn;
total[totalPos++] = totalIn;
while (total[ indexTotal ] != 0){
cout << setw(49) << "--------------------------------" << endl;
cout << setw(26) << "Total" << setw(7) << " : " << setw(3) << " $ ";
cout << total[indexTotal++] << endl;
cout << setw(49) << "================================" << endl;
cout << endl << endl;
}

iSee
Jun 25, 2006, 12:48 AM
The problem is with the way the index variables (indexStyle, indexColour, indexManicure, and indexTotal) are being incremented.

I have to guess at your intent a little, but assuming that you intend that each customer record can include at most one hair style, one hair color and one manicure, then you want to:

1. Change the while statements in the sales summary to if statements. You aren't recording more than one value for each type of service above, so you don't want to loop through multiple values in the summary, either.

2. Move the increment of the index variable out of the while statement (now an if statement). Right now, these are incremented until the values they reference are zero, and then never incremented again. That's why you are seeing these get stuck on zero.

So, for example, this:

while (hairStyle[ indexStyle ] != 0){
cout << setw(26) << "Style" << setw(7) << " : " << setw(3) << " $ ";
cout << setw(6) << hairStyle[indexStyle++] << endl;
}

becomes this:

if (hairStyle[ indexStyle ] != 0){
cout << setw(26) << "Style" << setw(7) << " : " << setw(3) << " $ ";
cout << setw(6) << hairStyle[indexStyle] << endl;
}
indexStyle++;


However, you can simplify your code a lot. Notice that all of you indexNNN and NNNPos variables start with the same value (zero) and are all incremented once through the main loop. Or can really use one variable for all of them. And, as a matter of fact, you already have that variable: numberOfTimesEntered. If you were to replace all the uses of the index and Pos variables with numberOfTimesEntered, except that you only incremented that variable once at the end of the main while loop (where you already doing it), then everything would work the same way. Of course that all assumes that you you intend that each customer record can include at most one hair style, one hair color and one manicure. If that's not true you'll need to make other changes to your code though, because right now you only record one value per customer.

crazyg
Jun 25, 2006, 12:58 AM
thanks alot i see what you mean, you have been a great help

Soulstorm
Jun 25, 2006, 01:58 AM
Oh, and, next time, use code tags. You will be surprised how many more answers you will receive