#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2002
    Posts
    0
    Rep Power
    0

    Question problems exiting loop


    i can't seem to exit this loop by entering -1 which is what I would like to do.
    //a couple defenitions

    #define ERROR 1
    #define OK 0
    #define QUIT -1

    //here's the loop in main...

    cout << "Employee information printed." << endl << endl << endl;

    int result = extpayroll.Search ();

    while (result > QUIT)
    {
    result = extpayroll.Search ();
    }


    //this is the class...

    int extEmployee::Search ()
    {

    int searchIDnum= 0;

    cout << endl << endl;
    cout << "=---------------=" << endl;
    cout << " Search Function" << endl;
    cout << "=---------------=" << endl;
    cout << "Enter an employee ID number to search for." << endl;
    cout << "Enter <-1> to exit the program." << endl;



    cin >> searchIDnum;


    while (searchIDnum >= 0)
    {


    int i = 0;

    while (i < employee::count && searchIDnum != employee::emplist[i].empId)
    {
    i++;
    }

    if (searchIDnum == employee::emplist[i].empId)
    {
    cout << setw(5) << "EmpId" << setw(11) << "First Name" << setw(10);
    cout << "Last Name" << setw(13) << "Hourly Wage"
    << setw(14) << "Hours Worked" << setw(15) << "Weekly Salary"
    << setw(8) << "Status" << endl;

    cout << setprecision(2) << fixed << endl << setw(5)
    << employee::emplist[i].empId << setw(5) << employee::emplist[i].firstname
    << setw(13) << employee::emplist[i].lastname << setw(7) << "$"
    << employee::emplist[i].wage << setw(11) << employee::emplist[i].hours
    << setw(12) << "$" << employee::emplist[i].wsalary;

    if (employee::emplist[i].flag == 0)
    {
    cout << setw(10) << "boss" << endl;
    }

    else
    {
    if (employee::emplist[i].wsalary < 1000)
    {
    cout << setw(10)<< "employee" << endl;
    }

    else
    {
    cout << setw(10)<< "employee" << endl;
    }
    }

    return (OK);
    }

    else if (searchIDnum == QUIT)
    {
    cout << "exit route" << endl;
    return (QUIT);
    }

    else
    {
    cout << "sorry no listing" << endl;
    return (ERROR);
    }

    }

    return (OK);
    }
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Posts
    20
    Rep Power
    0
    Its a long time I've done console IO, but I assume you aren't getting the sign in.
    Try a loop like

    [ccode]
    int i=0;
    while(i>=0) {
    cin >> i;
    cout << i;
    }
    [/ccode]

    and see if you can input negative numbers. (Or just use the debugger).
    If you can, just add the rest of your code block by block and see when it breaks.
  4. #3
  5. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Location
    India
    Posts
    7
    Rep Power
    0
    u have ur search function return OK irrespective of the input...u have to include this listing...
    if (searchIDnum == QUIT)
    {
    cout << "exit route" << endl;
    return (QUIT);
    }

    else
    {
    cout << "sorry no listing" << endl;
    return (ERROR);
    }

    outside the while loop..or change ur pgm to return QUIT all the time...
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2002
    Posts
    0
    Rep Power
    0
    aabha...you hit the nail on the head.

    "u have ur search function return OK irrespective of the input... "

    I moved the last return (OK) statement outside one set of brackets and it worked...:D

IMN logo majestic logo threadwatch logo seochat tools logo