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");
}
}
}
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.
{
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");
}
}
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;
}