if statement and char arrays

Pages: 12
I am having trouble with this if statement, I know I'm missing something.


{
			std::cout<<"Get ready for the amazingly cool game.\n";
			const char cName[8] = "Jackson";
			char PlayerRN[33] = "Other";
			std::cout<<"I need your name before you can continue (just your first): ";std::cin>>PlayerRN;

			 {
				 if (PlayerRN == "Jackson")
				 {
					 std::cout<<"Wow your name is the same as mine!\n";
						 system("Pause");
				 }
				 else {
					
				 std::cout<<"Shame we dont have the same name.";
			 system("Pause");
				 }
		}
		}
Last edited on
char arrays are not strings.

1
2
std::string cName = "Jackson";
std::string PlayerRN = "Other";
I see...Will Google, thankyou.
Good answer. The reason operator== fails with char arrays is that you're comparing the starting address of the char array with the starting address of the string literal "Jackson". They're always different, even if they point to C strings that are equal.
Well I do need more help. A person needs to be able to input their name from keyboard. Cant seem to get the right info from googling.
You don't have to change anything else. Just make sure <string> is included.
<iostream> is included that also includes string right?
No, that's not guaranteed, so you should include it explicitly.
Okay I keep trying the code but I cant seem to get it, more and more red squiggly lines.
Just replace your two lines with the two I posted. That's all.
Also, there are error messages associated with those red lines. Those do matter.
Did that, still broken, I get errors on the ==
		{
			std::cout<<"Get ready for the amazingly cool game.\n";
			std::string cName = "Jackson";
			std::string PlayerRN = "Other";
			std::cout<<"I need your name before you can continue (just your first): ";std::cin>>PlayerRN;

			 {
				 if (PlayerRN == "Jackson")
				 {
					 std::cout<<"Wow your name is the same as mine!\n";
						 system("Pause");
				 }
				 else {
				 std::cout<<"Shame we dont have the same name.";
			 system("Pause");
		}
		}
Those do matter.
Okay, lets do it this way. When you look at the code I posted last post. What is wrong with it?
Nothing, it's correct. What's the error message?
c:\users\admin\_prog\lemonadestand\lemonadestand\lemonadestand.cpp(20): error C2446: '==' : no conversion from 'const char *' to 'std::string *'


c:\users\admin\_prog\lemonadestand\lemonadestand\lemonadestand.cpp(20): error C2440: '==' : cannot convert from 'const char [8]' to 'std::string [33]'
Last edited on
std::string*? Why a pointer? Your actual code seems to be different from what you posted.
Last edited on
Thats the whole program. Problem is under iChoice1.




#include <iostream>
#include <string.h>

int main(int argc, char* argv[])
{
	int iChoice = 5;
	//Place menu here and get users choice
	std::cout<<"Menu\n-----\n1. Play Game\n2. View Instructions\n3. View Credits\n4. Exit Program\nPlease enter your choice:";
	std::cin>>iChoice;

	while ( iChoice <5) //This will make the program loop
	{
		if (iChoice == 1) //Checks if user entered 1
		{
			std::cout<<"Get ready for the amazingly cool game.\n";
			std::string cName[33] = {"Jackson"};
			std::string PlayerRN[33];
			std::cout<<"I need your name before you can continue (just your first): ";std::cin>>PlayerRN;

			 {
				 if (PlayerRN == "Jackson")
				 {
					 std::cout<<"Wow your name is the same as mine!\n";
						 system("Pause"
				 }
				 else {
				 std::cout<<"Shame we dont have the same name.";
			 system("Pause");
		}
		}
		if (iChoice == 2) //Checks if user entered 2
		{
			std::cout<<"This section of the program is still under devleopment. Please come back later.";
			system("Pause");
		}
		if (iChoice == 3) //Checks if user entered 3
		{
			const int cDevYear = 2012;
			char cVersion[] = "a0.1";
			
			std::cout<<"Hi my name is Jackson known as NuclearNarwhal on Steam and this is the program I am making for my programming course.\n";
			std::cout<<"Current Version: "<<cVersion<<std::endl;
			std::cout<<"Year of Devleopment: "<<cDevYear<<std::endl;
			system("Pause");
		}
		if (iChoice == 4) //Checks if user entered 4
		{
			return 0; //Exits program
		}
		//place menu here to get users choice.
		//This is because one at top of
		//program is not reached again.
		std::cout<<"Menu\n-----\n1. Play Game\n2. View Instructions\n3. View Credits\n4. Exit Program\nPlease enter your choice:";
		std::cin>>iChoice;
	}

	system("Pause");
	return 0;
}
You declared arrays of strings. Considering you just need one, that makes no sense.
Last edited on
Please expand on your response. More information and how to fix if you could please.
Last edited on
Pages: 12