-5
#include <iostream>
#include <Windows.h>
#include <string>
using namespace std;



int main(){
    string Whitelist[4] = {"Stian", "Mathias", "Modaser"};
    for (int x = 0; x < 3; x++){
        cout << x + 1<< ". " << Whitelist[x] << endl;
        if (Whitelist[x] == "Stian" && "Mathias" && "Modaser"){
            cout << "" << Whitelist[x] << " is here" <<  endl;
        }
        else{ cout << "no one is here" << endl; }
    }
    cin.get();
    return 0;
}

//so yeah basically im just trying to loop through my array and see if any of these names are there. so i guess u can pretty much read what the code does since most of u are pros :P. but when i asked my friend, whos been coding for 1-2 years, he said that i couldnt loop through arrays like this and told me to use a vector. what does he mean by that? and my code works?

manplox0
  • 3
  • 1
  • 1
    `Whitelist[x] == "Stian" && "Mathias" && "Modaser"` doesn't do what you think it does. – Biffen Sep 03 '16 at 11:17
  • 1
    They mean you rather should use `std::vector Whitelist = {"Stian", "Mathias", "Modaser"};`. Listen to them. – πάντα ῥεῖ Sep 03 '16 at 11:17
  • ok, but can u explain to me what it does, im pretty new to this programming :P – manplox0 Sep 03 '16 at 11:17
  • 1
    @manplox0 That's what [documentation](http://stackoverflow.com/documentation/c%2b%2b/511/stdvector#t=201609031118421129097) is for. – πάντα ῥεῖ Sep 03 '16 at 11:19
  • 2
    _"i guess u can pretty much read what the code does since most of u are pros"_ This assumes it does what you meant it to do. We can see what it does; we cannot see what you meant it to do. And, indeed, in this case the two are likely to differ. **Always explain, in words, what you are trying to do.** – Lightness Races in Orbit Sep 03 '16 at 11:20
  • 1
    _"he said that i couldnt loop through arrays like this and told me to use a vector. what does he mean by that?"_ Why don't you ask him? – Lightness Races in Orbit Sep 03 '16 at 11:20
  • ok my aplogies. but i meant my program to have a whitelist, with 3 names and then it would loop through it, and i would check if any of these names were there, it would say the elements, Stian, Mathias etc and then it would print them out and say stian is here, mathias is here, Modaser is here, but if one of they were missing from the list, it would say no one is here. but my friend told that i cannot loop through an array like that so thats why i asked. and he talked about vectors. – manplox0 Sep 03 '16 at 11:21
  • 2
    @manplox0: You're checking for the elements of `Whitelist` by iterating over `Whitelist`. That's pointless; you know the elements are there, because you're the one who put them there. If you meant to search `Whitelist` while iterating over some _other_ array, then you haven't done that. And let's forget about your friend: unless you can recall _specifically_ what he said and why, the anecdote is useless. – Lightness Races in Orbit Sep 03 '16 at 11:31

2 Answers2

0

This set of code is wrong

if (Whitelist[x] == "Stian" && "Mathias" && "Modaser"){
    cout << "" << Whitelist[x] << " is here" <<  endl;
}

Why? because suppose the first condition of the if statement evaluates to true like this:

if (true && "Mathias" && "Modaser")
{
    //...
}

Then the code wouldn't make sense. In an if statement, you have to check for every condition separately, like this:

if (Whitelist[x] == "Stian" && Whitelist[x] =="Mathias" && Whitelist[x] =="Modaser"){
    cout << "" << Whitelist[x] << " is here" <<  endl;
}

But since any 1 string cannot be three names at the same time, this condition will fail, (you used &&). Fix your code using the || operator, like this, for your final code (Also, remove << "", that is just redundant, and unnecessary):

if (Whitelist[x] == "Stian" || Whitelist[x] =="Mathias" || Whitelist[x] =="Modaser"){
    cout << Whitelist[x] << " is here" <<  endl;
}

BTW: As a recommendation, use a std::vector<std::string>, not a raw array, so you get easier and more capabilities than an array. Lastly, you also have 4 elements in your array, of which one is unused. It might be a typo, so make your array size 3.

Arnav Borborah
  • 11,357
  • 8
  • 43
  • 88
0

There is nothing fundamentally wrong with looping over an array like this.

We can only guess what your friend meant, but I can perform my own review of your code.

However, you have four array elements and only loop over three of them, which may be a mistake; if it is, it's evidence that you'd be better off using iterators, rather than hard-coding numbers that you can get wrong.

Furthermore, your if conditional is wrong: did you mean || ("or"), instead of && ("and")? And you have to write out the adjoined conditions in full, so:

if (Whitelist[x] == "Stian" || Whitelist[x] =="Mathias" || Whitelist[x] =="Modaser")

I'm not sure why you're comparing against all these values, when they're the only ones in the array. Well, except for that empty fourth element; perhaps you're trying to catch that. We don't know, because you didn't tell us. Did you mean to search Whitelist while iterating over some other array? We have no way of knowing. Maybe that's what your friend really meant? Again, I couldn't say.

Streaming "" to std::cout just waits resources and does literally nothing else. Remove it.

Finally, and somewhat tangentially, it would be better not to block waiting for input as a means to keep your console window open. That is not your program's job.

Community
  • 1
  • 1
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055