if statement and char arrays

Pages: 12
Feb 27, 2012 at 5:21pm
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 Feb 27, 2012 at 5:26pm
Feb 27, 2012 at 5:26pm
char arrays are not strings.

1
2
std::string cName = "Jackson";
std::string PlayerRN = "Other";
Feb 27, 2012 at 5:27pm
I see...Will Google, thankyou.
Feb 27, 2012 at 5:29pm
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.
Feb 27, 2012 at 5:32pm
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.
Feb 27, 2012 at 5:34pm
You don't have to change anything else. Just make sure <string> is included.
Feb 27, 2012 at 5:35pm
<iostream> is included that also includes string right?
Feb 27, 2012 at 5:40pm
No, that's not guaranteed, so you should include it explicitly.
Feb 27, 2012 at 5:41pm
Okay I keep trying the code but I cant seem to get it, more and more red squiggly lines.
Feb 27, 2012 at 5:43pm
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.
Feb 27, 2012 at 5:46pm
Did that, still broken, I get errors on the ==
Feb 27, 2012 at 5:47pm
		{
			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");
		}
		}
Feb 27, 2012 at 5:48pm
Those do matter.
Feb 27, 2012 at 5:49pm
Okay, lets do it this way. When you look at the code I posted last post. What is wrong with it?
Feb 27, 2012 at 5:50pm
Nothing, it's correct. What's the error message?
Feb 27, 2012 at 5:51pm
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 Feb 27, 2012 at 5:53pm
Feb 27, 2012 at 5:53pm
std::string*? Why a pointer? Your actual code seems to be different from what you posted.
Last edited on Feb 27, 2012 at 5:55pm
Feb 27, 2012 at 5:54pm
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;
}
Feb 27, 2012 at 5:56pm
You declared arrays of strings. Considering you just need one, that makes no sense.
Last edited on Feb 27, 2012 at 5:57pm
Feb 27, 2012 at 5:56pm
Please expand on your response. More information and how to fix if you could please.
Last edited on Feb 27, 2012 at 5:57pm
Pages: 12