//Program checks employment status, if part time, NO VACATION, if full time, program goes into if/else nest and asks for number of years working at company. More than five years,full time employee granted 3weeks of vacation. Five years or less, full time employee is granted 2weeks of vacation.

#include <iostream>
using namespace std;

char empTime=' ';
int years=0;

int main()
{
    cout<<"Employment status? (F=fulltime, P=parttime)";
    cin>>empTime;
    cin.ignore(100,'\n');                       [B]//(1)[/B]
    empTime=toupper(empTime);

    if(empTime=='F')
    {
      cout<<"Years working at company XYZ?";
      cin>>years;

      if(years>5)
      {
          cout<<"Vacation weeks = 3";
      }
      else if(years<=5 && years>=0){        [B]//(2)[/B]
            cout<<"Vacation weeks = 2";
            }
      else{cout<<"Invalid input";}
    }
    else if(empTime=='P'){
        cout<<"Vacation weeks = 0";
        }
    else{cout<<"Invalid input";}

    cin.get();
}

(1) I put ignore function in b/c I thought clearing the buffer may help..but I don't think char data type needs clearing..does it?

MAIN PROBLEM:
(2)This is where I suspect the problem is...when I run the program, I input 'F', and then when the screen displays number of years I punch in a bunch of random charcters ex "dhsdkhfebs" [enter] --> and right away it displays "vacation weeks = 2", but I was hoping it would go to "invalid input".

When I put a negative number in..it goes to "Invalid input"...but not charcters..

Any help would be appreciated. Please note that I am only a week in learning c++ and am using a book from 2003, so if my syntax is either too formal, off, retarded in anyway please correct me. Also if I am using unneeded functions or anything else that's bloated please tell me, better I fix bad habits now than later.

P.S.
_btw I use cin.get(); so user has to press [enter] before the program returns 0.
_using codeblocks as my compiler.

Recommended Answers

All 6 Replies

There is a function available if you include the <cctype> header.It's form is

int isdigit(int)

which returns zero in case the passed argument is an integer and nonzero if else.Try yo make use of that

1. int_isdigit(int)

Thanks for responding, but I am still unclear about using the function.
How would I integrate that function into the source code?

Should I place it before the if/else?

You can try a simple approach by by containing all the conditional statements inside another statement using this function.Something like this:

while (cin >> year)
if (!isdigit(year)) { // vacation rights check } 
else cout << "Wrong input";

If you are inputting into an integer variable, you cannot test for characters because they can't be entered into an integer.

If you input into a string you can then test if the values are digits or not.

The function takes an int as an argument.

Understandable, so..

If I have a data type of that's an integer(int) I cannot test for a char, BUT what I find boggling is if that is true, then why does it jump to the else if(years<=5 || years>=0) and display "two weeks"? Why not to the else?

In other words, if I type characters into an input that's suppose to contain an integer (the variables data type is int), then how does the computer handle that?..particularly WHY does it go to the if else?...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.