4

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;
}
user968683
  • 65
  • 1
  • 4

8 Answers8

5

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).

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
4

You can not use "==" operator. Try to find out more about

strcmp

if (strcmp(membername, "Westley") == 0)
   ...
dip
  • 530
  • 3
  • 7
4

this is c++ and you are using a char array not a string you need to use strcmp for string comparison

you can do this like

if(strcmp(membarname, Westley))

or if possible you can use std::string instead of char[] to store a string then you can use == operator

you can use the std::string as follows

#include <string>
using namespace std;

int main(){
    string Westley = "Westley";

    ....

    string membername;
    cout << "Please Enter a Beta Team Members Name.\n";
    cin >> membername;
    if (membername == Westley){

        ....
    }
    ....
}
danishgoel
  • 3,650
  • 1
  • 18
  • 30
2

Instead of character array char[]; use std::string and you will get the desired result.

if (membername == Westley)

Because, for char[] data, above comparison results in address comparison and not the content comparison.

iammilind
  • 68,093
  • 33
  • 169
  • 336
1

Since it is arrays you are working with you need to use strmcp instead e.g.

if (!strcmp(membername, Westley) ...

since you are working in C++ use instead string:

#include <string>

using namespace std;

string Westley = "Westley";

... then you can do

if (membername == Westley) ...
AndersK
  • 35,813
  • 6
  • 60
  • 86
0

First of all, if you are coding with C++ you should use std::string instead of char []. It has the convenience that you can compare two strings (alas with char [] you have to call functions like strcmp or the like).

For example:

#include <iostream>
#include <string> // <-- important
using namespace std;

int main ()
{
    string Westley("Westley");
    ...

    if(membername == Westley) // now works!
    {
        ...
Constantinius
  • 34,183
  • 8
  • 77
  • 85
0

The == operator you are using compares the addresses of the strings. To compare two C Strings use the strcmp function from <cstring> (string.h). In C++ you should use the std::string class template, which can be intuitively compared using the == operator.

hansmaad
  • 18,417
  • 9
  • 53
  • 94
0
if (membername == Westley)

only compare two pointers' value.

Huskar
  • 1