-3
#include<iostream>
#include<conio>
#include<string>

using namespace std;

class CompanyName
{
public:

    char name[10];

    void display()
    {
        if(name=="emily")
            cout<<"Hired!"<<endl;
    }
};

void main()
{
    CompanyName c;

    cout<<"enter name"<<endl;
    cin>>c.name;

    c.display();

    getch();
}

So my friend just got placed in a company and I want to congratulate her (and announce that so-and-so company hired her) on her facebook wall, using a small CPP programme. I don't want to just print 'congratulations Emily!' because I already did that for something else. So this time you can see what I want to do if you just look at the code. Any better suggestions are highly welcome. But my problem here is, I can't get this code to run. I feel like the 'if' line is not right. Can someone please correct the code for me? I'm still a beginner.

Ahmad Khan
  • 2,655
  • 19
  • 25
Cersei
  • 95
  • 6
  • 3
    If you are going to deal with strings you should use a `std::string`. – NathanOliver Sep 01 '16 at 18:50
  • What does `I can't get this code to run` mean? What happens? – Fantastic Mr Fox Sep 01 '16 at 18:50
  • You cannot compare name to "emiky" it will never be true. Since yiu use c++ turn name into a string and comparison will work. – Jean-François Fabre Sep 01 '16 at 18:51
  • Well, it just asks me to 'enter name' and then nothing happens until I exit. The if condition doesn't work. And noted, NathanOliver. I'm using Microsoft Visual Studio 2010, does that make a difference? – Cersei Sep 01 '16 at 18:52
  • How do you turn it into a string? – Cersei Sep 01 '16 at 18:53
  • 1
    @Cersei You cannot apply the `==` equality test operator using a raw char array, either use `strcmp()` or change `name`'s type to `std::string`. – πάντα ῥεῖ Sep 01 '16 at 18:53
  • 2
    Would like to point out the elephant in the room, `conio` is not a standard header. Stop using it. –  Sep 01 '16 at 18:55
  • 1
    First, perepare yourself psychologically to live without `` because it's not standard C++ and certainly a legacy from the past. Then if you include ``, use them for example in `string name;`. Finally, could you clarify your expectations: do you really expect an answer with network code for posting on FB ? What else could you print than "congratulations" ? Where shall the text come from ? Is Emily the only employee of a company ? – Christophe Sep 01 '16 at 18:55
  • 2
    @Thebluefish and what about `void main`? – Bob__ Sep 01 '16 at 18:56
  • @Thebluefish: You can't make very interesting programs if you only stick to standard headers. – Benjamin Lindley Sep 01 '16 at 18:56
  • emily is the name of the company? – 463035818_is_not_an_ai Sep 01 '16 at 19:05
  • 1
    @Benjamin but one should try not use very old headers from old compilers from another era on new projects unless there is a purpose to it, as in to maintain legacy applications – rlam12 Sep 01 '16 at 19:11
  • Why is void main() inappropriate? You have to put return 0 at the end if you use int main() anyway, right? I'm probably way off mark since I don't fully understand everything. @Christophe I get your point about where the text will come from, but I don't wanna do the print thing. I'm going to make this in a .png format once I write everything. And yes, I certainly expect an answer regardless of where I use the code. I have valid doubts. – Cersei Sep 02 '16 at 06:32
  • @Cersei look at http://www.stroustrup.com/bs_faq2.html#void-main . But now i'm lost: first tou want to post on facebook (which would require networking+complex apis) then you want to generate a png (graphic file). I'd suggest One step at a time – Christophe Sep 02 '16 at 07:18
  • Possible duplicate of [How do I properly compare strings in C?](http://stackoverflow.com/questions/8004237/how-do-i-properly-compare-strings-in-c) – Gerhard Sep 29 '16 at 09:49

4 Answers4

2

You declared name as char name[10];, then name is going to be treated as a pointer. Your pointer is never going to be equal to the string "emily".

Solution: It is highly recommended to simply use the great std::string from #include <string>

This should work (since you included the library already):

class CompanyName
{
public:
    std::string name;

    // ..
        if (name == "emily")
           // cout
    // ..
};
Khalil Khalaf
  • 9,259
  • 11
  • 62
  • 104
1

You can't compare name just using == operator, as it is a char array, you should use strcmp(name, "emily") == 0

Better yet, use std::string class to store and manipulate strings, with std::string, you can simply compare it using == operator.

Another thing: You should not use getch() as it is not standard c++.

Ahmad Khan
  • 2,655
  • 19
  • 25
1

You know, if you used std::string, this code would be easier. See the code below:

#include <iostream>
#include <string>

using namespace std;

class CompanyName {
public:
    string name;

    void display()
    {
        if (name == "emily")
            cout << "Hired!" << endl;
    }
};

int main()
{
    CompanyName c;

    cout << "enter name" << endl;
    cin >> c.name;

    c.display();
    return 0;
}

Then here there is no limit to the size of your string. By the way, your code doesn't work because it can only contain 9 characters, + \0, which means that your string would be too long. Use the solution above. Live Example

Arnav Borborah
  • 11,357
  • 8
  • 43
  • 88
  • I did not know you could use 'string name'. We've only been taught to use a char array in CPP (but we did use strings in C). I think I remember my teacher saying you can't? use 'string' in CPP but it might be that I misunderstood...Thank you so much! – Cersei Sep 02 '16 at 06:25
1

It's not safe in C++ to directly compare a character array with a string. As the others have suggested, try using std::string, it will handle all the background stuff for you.

Here's a fun example using your class's destructor:

#include<iostream>
#include<string>

using namespace std;

class CompanyName
{
public:
    std::string name;
    ~CompanyName()
    {
        if (name == "emily")
            cout << "Hired!" << endl;
        else cout << "Who's that?" << endl;
    }
};

int main()
{
    CompanyName c;
    cout << "Enter new employee name" << endl;
    cin >> c.name;
    return EXIT_SUCCESS;
}

Compile online: http://cpp.sh/433h7

Zev Isert
  • 915
  • 11
  • 20
  • 2
    Making it *even more* misleading. –  Sep 01 '16 at 19:02
  • Thanks so much, this helps a lot! I am left with a few queries though, such as why did you use a destructor and not a constructor? And isn't EXIT_SUCCESS not an int value? – Cersei Sep 02 '16 at 06:22
  • @Thebluefish Misleading? – Cersei Sep 02 '16 at 06:23
  • 1
    @Cersei For someone just starting C++, it's better to go over best practices. A destructor-based method for doing standard stuff is frankly mis-leading. –  Sep 02 '16 at 14:57
  • @Cersei, this solution doesn't fall under best practices, but it still is amusing. C++ follows an idiom called [RAII](https://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization), which essentially translates to how long C++ maintains variables in scope. When the program comes to the `return` keyword, the variables in scope are destroyed, so the `CompanyName` destructor runs automatically at that point - this is when the name is checked and printed. EXIT_SUCCESS is a macro defined in `stdlib`, and it resolves to the proper `int` for successful termination. – Zev Isert Sep 02 '16 at 17:39