Basically the if(array[2] == "Hello ") will not work correctly because each time I enter the input it's always the "(He left) " so I know the first if statement with the array isn't working. Any other way to compare values in the if statement?
There is no array[2]. An array of size 2 has elements [0] and [1], i.e., the indexing is zero-based.
And it's array[0] (the first element) that has the value "Hello ".
Line 17 is working correctly. I have the feeling that your understanding is working incorrectly.
"array", bad name BTW can easily be confused with "std::array", is defined as having the size of 2 and you initialize the array with 2 strings.
On line 17 you are trying to access the third element off the array which only has 2 elements, numbered 0 and 1. "array[2]" is 1 element past the end of the array and does not exist, so there is no way to know what "array[2]" is accessing if anything. Therefor the if statement is always false.
Remember C++ is a zero based language meaning that arrays and other containers start st (0) zero not (1).
"int cin" may work, std::cin" or "cin" is the reserved word for input from the keyboard. It did not flag any error or warning when I compiled the code, but I think it should have. Do try to come up with a better more descriptive name.
Thanks for the feedback. Yes I'm aware it is bad naming but I was having the problem on my other project so I made a mini one to figure it out, therefore I had used some not so good naming.
std::cin" or "cin" is the reserved word for input from the keyboard. It did not flag any error or warning when I compiled the code, but I think it should have
cin is not a reserved word. It is an identifier defined in the standard library for standard input (which may or may not be "input from the keyboard"). The compiler should definitely not give an error or warning since std::cin is not the same as cin unless you say using namespace std or at least using std::cin.
A note: Do not change your OP, (Original Post), with new code. It makes it difficult for other to follow not knowing what you started with. Better to put updated code changes in a new reply.
I realize that you created this program to figure something out, but the code you started with made no sense.
#include <iostream>
#include <iomanip>
#include <limits>
#include <string>
int main()
{
int choice{}, idx{};
std::string array[2] = { "Hello ", "Bye " };
//std::string hi[1] = { "Hello " }; // <--- Never used and no real need for an array for just a single string.
//srand((unsigned int)time(NULL));
srand(static_cast<unsignedint>(time(nullptr)));
// <--- Needs a prompt.
std::cout << "\n Enter 1 or 2: ";
std::cin >> choice;
std::cout << '\n';
idx = rand() % 2;
if (choice == 1)
{
std::cout << " You choose " << choice << " " << array[idx] << " ";
}
elseif (choice == 2)
{
std::cout << " You choose " << choice << " " << array[idx] << " ";
}
else
{
std::cout << "\n Invalid Input! Leaving program.\n\n";
return 1;
}
//HERE is where I'm talking about
if (array[idx] == "Hello ")
{
std::cout << "(He's here) " << "\n\n";
}
else
{
std::cout << "(He left) " << "\n\n";
}
// <--- Keeps console window open when running in debug mode on Visual Studio. Or a good way to pause the program.
// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue: ";
std::cin.get();
return 0; // <--- Not required, but makes a good break point.
}