Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why comparing 2 strings doesn't give desired result?

Below is the code I've come up with and in all honesty tonight is my first attempt at coding at all. However I cannot seem to get my if statement to work properly. It just simply jumps to else even if I type Westley or westley or (space)Westley.

I want the program to be able to accept any of my team members names however I figured I get my name working and then I could add the rest of them later. Any thoughts or help would be wonderful. Also as a side note I was going to try and loop it if it went to the else back up to the begining any thoughts on that as well? Thank you

#include <iostream>
using namespace std;

int main ()
{
  char Westley[] = "Westley";
  char Alex[] = "Alex";
  char Andrea[] = "Andrea";
  char Bee[] = "Bee";
  char Gia[] = "Gia";
  char KaYeng[] = "Ka Yeng";
  char Chi[] = "Chi";
  char Corinne[] = "Corinne";
  char Joyce[] = "Joyce";
  char Parish[] = "Parish";
  char membername [80];
  cout << "Please Enter a Beta Team Members Name.\n";
  cin >> membername;
  if (membername == Westley)
  {   cout << "BETA TEAM ROSTER!!\n";
      cout << "Westley.\n";
      cout << "Alex.\n";
      cout << "Andrea.\n";
      cout << "Bee.\n";
      cout << "Gia.\n";
      cout << "Ka Yeng.\n";
      cout << "Chi.\n";
      cout << "Corinne.\n";
      cout << "Joyce.\n";
      cout << "Parish.\n";
  }
  else
      cout << "Not a Valid Beta Team Members Name!\n" << "Please Enter a Beta Team Members Name"<< endl;
cin >> membername;
  return 0;
}
like image 454
user968683 Avatar asked Dec 05 '22 20:12

user968683


1 Answers

Don't use char[]; use std::string for this sort of thing as that knows how to do comparisons in a helpful way (comparisons between char arrays test if they are the same array, not if the contents is identical).

like image 141
Donal Fellows Avatar answered Jan 31 '23 02:01

Donal Fellows