String array won't work correctly in an if statement

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?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
		int cin;
		srand((unsigned int)time(NULL));
		std::string array[2] = { "Hello ", "Bye " };

		std::cin >> cin;
		if (cin == 1)
		{
			std::cout << array[rand() % 2] << std::endl;
		}
		else if (cin == 2)
		{
			std::cout << array[rand() % 2] << std::endl;
		}

                //HERE is where I'm talking about 
		if (array == "Hello ")
		{
			std::cout << "(He's here) " << std::endl;
		}
		else 
		{
			std::cout << "(He left) " << std::endl;
		}
Last edited on
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 ".
Last edited on
Hello possum swallower,

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.

Andy
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.
Last edited on
andy wrote:
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.
cin is an abbreviation for the English "character input", as cout is an abbreviation for "character output".
Last edited on
 
if (array == "Hello ")


In the updated code in the OP - makes no sense as array is an array of std::string ???
@dutch,

Thank you for the correction and the input. Always open to input.

But bear with me it was late. I had the right idea just said it wrong. Sorry.

Andy
Hello possum swallower,

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.

I hope the following code will help:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#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<unsigned int>(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] << "  ";
    }
    else if (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.
}


Andy
or re-factored becomes simply:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <string>

int main()
{
	const std::string array[2] {"Hello ", "Bye "};

	srand(static_cast<unsigned int>(time(nullptr)));

	int choice {};
	std::cout << "\n Enter 1 or 2: ";
	std::cin >> choice;
	std::cout << '\n';

	const int idx {rand() % 2};

	if (choice == 1 || choice == 2) {
		std::cout << " You choose " << choice << " " << array[idx] << "  ";

		if (array[idx] == "Hello ")
			std::cout << "(He's here) " << "\n\n";
		else
			std::cout << "(He left) " << "\n\n";
	} else
		std::cout << "\n     Invalid Input!  Leaving program.\n\n";
}

@Handy Andy, thank you, it helps a lot. I was not sure about the syntax for the if statement, but that helps widen my knowledge of things
Last edited on
Topic archived. No new replies allowed.