bug in simple program

Discussion in 'Mac Programming' started by crazyg, Jun 24, 2006.

  1. macrumors newbie

    Joined:
    Jun 6, 2006
    #1
    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
     
  2. macrumors 6502a

    crazytom

    Joined:
    Jul 23, 2002
    Location:
    IL
    #2
    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).
     
  3. thread starter macrumors newbie

    Joined:
    Jun 6, 2006
    #3
    still does not work

    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,
     
  4. thread starter macrumors newbie

    Joined:
    Jun 6, 2006
    #4
    problem

    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;
    }
     
  5. macrumors 68040

    iSee

    Joined:
    Oct 25, 2004
    #5
    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:
    Code:
    while (hairStyle[ indexStyle ] != 0){
    	cout << setw(26) << "Style" << setw(7) << " : " << setw(3) << " $ ";
    	cout << setw(6) << hairStyle[indexStyle++] << endl;
    }
    
    becomes this:
    Code:
    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.
     
  6. thread starter macrumors newbie

    Joined:
    Jun 6, 2006
    #6
    thankyou

    thanks alot i see what you mean, you have been a great help
     
  7. macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #7
    Oh, and, next time, use code tags. You will be surprised how many more answers you will receive
     

Share This Page