1

I just started learning C++ and I'm currently following a tutorial on YouTube.

I thought it was fun to make a very simple 'access' program. If I type in my name it says, "Welcome!" If I type in another name it says, "access denied". It worked perfectly fine, but then I wanted the program to say "Welcome!" to two different names. So, I wanted to add a second name in the string, but I couldn't figure out how to do that. I googled a lot but I couldn't find anything. In the end, I came to string name = ("Joe", "Sean");, but here, it was only valid for Sean. I just can't figure out how to put multiple names in one string and make them both work. I hope you can help me, here is my code:

#include <iostream>
#include <cmath>
#include <string>

using namespace std;




int main()
{
    string name = ("Joe", "Sean");
    string input;
    cout << "What is your name?\nMy name is: ";
    cin >> input;



    if(input == name){
        cout << "Welcome, "<< input <<"! ";

    } else {
        cout << "Access denied";
    }



    
    return 0;
}
  • You're trying to only greet people whose names appear in a set of names? – erip Oct 25 '20 at 18:24
  • 2
    "Following a tutorial on Youtube" is the worst possible way to learn C++. C++ is the most complicated general purpose programming language in use today. The only way to thoroughly learn C++ is from a fully vetted and edited textbook, any clown can upload a video to Youtube, that says anything he wants to say. A C++ textbook will teach you how to use arrays and vectors, which is what you need to do for this task. – Sam Varshavchik Oct 25 '20 at 18:25
  • To extend what @SamVarshavchik wrote there, the art of googling is posing the right question, and the art of research is questioning the source you have. (i.e. find a better source to learn C++ from) – Tony Tannous Oct 25 '20 at 18:26
  • 4
    I think you should follow that tutorial (or some [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) ;] )a bit further until they cover vectors, arrays and other stl-containers. Right now you've arrived at a solution that is definitely not what you want, see also: [How does the comma operator work?](https://stackoverflow.com/questions/54142/how-does-the-comma-operator-work). And I think just giving you some code would confuse you more than if you learned it on your own. – Lukas-T Oct 25 '20 at 18:27
  • 2
    What you probably want is a `std::vector names = { "Joe", "Sean" };` and `std::find()`. – πάντα ῥεῖ Oct 25 '20 at 18:28
  • 1
    What πάνταῥεῖ said, though it looks like you want `std::any_of`. – cigien Oct 25 '20 at 18:33
  • 1
    Or an `unordered_set` and `contains()`. – Ted Lyngmo Oct 25 '20 at 18:35
  • 1
    Learn about what the comma operator does. It's not what you expect. Also a std::string can hold a single string. – drescherjm Oct 25 '20 at 18:55

2 Answers2

2

This is a way to do it using a vector of strings, so you can adapt easily with more names :

#include <iostream>
#include <vector>

using namespace std;

void printMessage(string message)
{
    std::cout << message << std::endl;
}

int main()
{
    vector<string> names{"Joe", "Sean", "Paul"};
    string input;
    cout << "What is your name? " << endl;
    cin >> input;

    for (string name : names)
    {
        if (name == input)
        {
            printMessage("Welcome!");
            return 0;
        }
    }
    printMessage("Access Denied!");

    return 0;
}
Sambalika
  • 100
  • 7
1

The problem is in the string variable "name". You need an array of strings, not a single string.

This is an example implementation:

#include <iostream>
#include <cmath>
#include <string>

using namespace std;

int main()
{
    string names[] = {"Joe", "Sean"};

    string input;
    cout << "What is your name?\nMy name is: ";
    cin >> input;

    for (int i = 0; i < end(names) - begin(names); i++) {
      if(input == names[i]){
          cout << "Welcome, "<< input <<"! " << endl;
          return 0;
      }
    }

    cout << "Access denied" << endl;

    return 0;
}

You encountered some quirky features of C++ in the approach you are using to initialize your string variable:

string s1 = ("Joe"); // creates a string "Joe"
string s2 = ("Joe", "Sean"); // creates 2 strings, "Joe" and "Sean", and the variable s2 stores only the latter!

For more details on the different methods for initializing variables there has been an interesting discussion in this previous question.

gkaf
  • 80
  • 6
  • I would recommend using `std::size(names)` instead of `end(names) - begin(names)` - or `std::distance(begin(names), end(names))` - or better, use a range-based for-loop: `for(auto& name : names) { if(input == name) ...` – Ted Lyngmo Oct 26 '20 at 15:38