#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    421
    Rep Power
    13

    while loop not cooperating damnit!


    I would like my program to stop when I press 'n' or 'N' but it doesn't. Here is my code:

    #include <iostream>
    using namespace std;

    int main()
    {

    int quantity;
    int product_order;
    float PRODUCT;
    char flag = 'f';
    char quit;

    do
    {

    do
    {
    flag = 'f';
    cout << "Here are your product options:\n\n";
    cout << "1. Pizza\n2. Soda\n3. Burgers\n4. Steak\n5. Eggs\n\nPlease make your selection using options 1-5.\n\n";
    cout << "What product would you like to order?\n";
    cin >> product_order;

    if (product_order < 1 || product_order > 5)
    {
    flag = 't';
    cout << "Type in a number between 1-5 please!!\n\n\n\n\n\n";

    }
    }
    while (flag == 't');
    if (flag == 'f')

    {
    switch(product_order)
    {
    case 1:
    PRODUCT = 2.98f;
    cout << "You have selected product 1 which is $2.98!\n";
    break;
    case 2:
    PRODUCT = 4.50f;
    cout << "You have selected product 2 which is $4.50!\n";
    break;
    case 3:
    PRODUCT = 9.98f;
    cout << "You have selected product 3 which is $4.50!\n";
    break;
    case 4:
    PRODUCT = 4.49f;
    cout << "You have selected product 4 which is $4.49!\n";
    break;
    case 5:
    PRODUCT = 6.87f;
    cout << "You have selected product 5 which is $6.87!\n";
    break;
    }

    cout << "How many of product " << product_order << " would you like?\n";
    cin >> quantity;

    float total = quantity * PRODUCT;

    cout << "Your total is: "<< total << "\n\n\n";
    cout << "Would you like to do this again? (N for NO, and any other key for YES) \n";
    cin >> quit;


    }

    }
    while (quit != 'N' || quit != 'n');
    return 0;

    }
    Last edited by andy3109; February 27th, 2003 at 04:21 PM.
    hmmm...
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Posts
    138
    Rep Power
    12
    It's been a while since I've used cin but I'm guessing your quit variable is holding whitespace ie. "Carriage return" 0x13 or at least a space or something.
    put a
    Code:
    cout << (int) quit;
    inside the end of the loop and you should find out.
    You probably have to
    Code:
    cin >> quit; cin >> quit;
    once to eat the whitespace and the next to get the 'y' or 'n'
    --

    ngibsonau
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    "It's been a while since I've used cin but I'm guessing your quit variable is holding whitespace"

    cin skips over whitespace, so that's not the problem.

    Your loop control is faulty:

    while (quit != 'N' || quit != 'n');

    If I type in 'N', then the condition quit != 'n' is true, and with an OR conditional if one of the conditions is true, it returns true.
    Last edited by 7stud; February 27th, 2003 at 04:41 PM.
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Location
    Southern California
    Posts
    73
    Rep Power
    15
    I think your problem is that this:

    Code:
    while (quit != 'N' || quit != 'n');
    needs to be changed to this:

    Code:
    while (quit != 'N' && quit != 'n');
    Whether the user enters 'N' or 'n', one of the conditions in your while loop test will always be true, and thus the loop will continue.

    Using '&&', the loop will end when either 'N' or 'n' is entered.
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    421
    Rep Power
    13
    It is "and"....AND it makes sense to me now. Thanks for all your replies guys!
    -andy
    hmmm...

IMN logo majestic logo threadwatch logo seochat tools logo