Compare strings using ==

151 views Asked by At

Code, as shown below, can be complied.

Problem: It always says "Invalid Employee id" even when I enter the correct employee id.
Please tell me why and how to do this correctly.

#include <iostream>
#include <iomanip>
using namespace std;
char select, js;
char empid[4];
double bSalary, bonus, tot=0.0;
int main()
{
    do
    {
        cout<<"Employee id: ";
        cin>>empid;
        if(empid=="M001" || empid=="A004" || empid == "M002") //these are employee ids
        {   
            cout<<"Job Status: ";
            cin>>js;
            if(js=='P' || js=='C')
            {
                cout<<"Basic Salary: ";
                cin>>bSalary;
                if(bSalary>75000 && js=='P')
                {
                    bonus = bSalary*(20.0/100.0);
                    tot = tot + bonus + bSalary;
                }
                else if(bSalary>75000 && js=='C')
                {
                    bonus = bSalary*(15.0/100.0);
                    tot = tot + bonus + bSalary;
                }
                else
                    tot = tot+bonus+bSalary;
            }
            else
                cout<<"Invalid Job Status"<<endl;
        }
        else
            cout<<"Invalid Employee no"<<endl;
        cout<<"Do you want to continue: ";
        cin>>select;
        cout<<endl;
    }while(select=='y'||select=='Y');
    cout<<"Total cost: "<<setprecision(2)<<setiosflags(ios::fixed)<<tot<<endl;
    return 0;
}

Note: It is going to the else clause all the time.

4

There are 4 answers

2
Tony Delroy On BEST ANSWER

It's this:

char empid[4];

This is too small as there's no room for a NUL terminator after the id. You could just change it to 5, but then if someone deliberately or accidentally typed a longer string your program may crash (it's called a buffer overrun, and in some situations can allow whoever provides input to hack the account running the program).

Further, == doesn't work for character arrays: you have to use e.g.:

 if (strcmp(empid, "M001") == 0 || strcmp(empid, "A004") == 0 || ...

You would be much better off using a std::string, which will grow to accommodate the actual input (including a NUL terminator though that's not counted in a string's .size()), and works intuitively with ==.


Separately, your...

tot = tot+bonus+bSalary;

...is broken, as bonus may be uninitialised you mustn't read from it in an expression. You can simply remove bonus from the addition above, if it's meant to be 0 for the relevant employees.

0
Vishwas On

You can't compare C strings with == or !=. As you can see that empid here is just a pointer so == will compare the base addresses of those strings not the strings themselves.

You need to use strcmp and include

#include <cstring>
.
.
.
if(strcmp(empid,"M001")==0 || ...)
0
user4581301 On

Empid is not a string. C++ doesn't have a built-in == overload to compare char arrays.

You can make your own == operator. But don't bother. Declare empid as a string and watch magic happen.

string empid;
5
Spanky On

Changing the size of the char array to take care of NULL char will not work here.

char empid[5]

"==" operator do not work properly with char arrays. please change the condition to below:

if (0 == (strcmp(empid, "M001")) || (0 == (strcmp(empid, "A004"))) || (0 ==     
   (strcmp(empid, "M002"))))

EDIT:people above has already answered your question. My answer is redundant now.