Checking equality for Characters

Dec 8, 2016 at 3:33am
I need to check whether a variable is equal to a character. is that okay?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 //Modify the previous program such that only the users Alice and Bob are greeted with their names.

#include <iostream>
#include <string>

using namespace std;
int main()
{
	string x;

	cout<<"Hello! Please enter your name.\n\n";
	cin>>x;

	if (x=="Alice" or x=="Bob")
	{
		cout<<"Hello there, "<<x;
	}
	else
	{
		cout<<"sorry don't know you haha";
	}
	system("pause");
	return 0;

}


halp
Last edited on Dec 8, 2016 at 3:36am
Dec 8, 2016 at 7:56am
use this || for or operator yes you can do that
Last edited on Dec 8, 2016 at 7:56am
Dec 8, 2016 at 8:39am
bird1234 that was not helpful.

OP, this works:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>

int main()
{
    std::string str;

    std::cout << "Enter a character: " << std::flush;
    while (!(std::cin >> str) || str.length() != 1)
    {
        std::cin.clear();
        std::cin.ignore();
        std::cout << "Invalid. Re-enter: " << std::flush;
    }

    std::cout << "You " << (str == "a" ? "entered" : "didn't enter") << " \'a\'\n";

    return 0;
}
Last edited on Dec 8, 2016 at 8:41am
Dec 8, 2016 at 10:11am
The original code works fine but requires the user to input the name with that exact capitalization. "bob", "BOB" or "bOb" will give the "sorry don't know you" response. To make the comparison case-insensitive you could loop through the string and turn every character into lower case using std::tolower, and then compare the string to the lower case "bob".

1
2
3
4
5
6
7
8
9
10
11
12
void convertToLowerCase(std::string& str)
{
	for (char& c : str)
	{
		c = std::tolower(c);
	}
}

// in main
convertToLowerCase(x);
if (x == "alice" || x == "bob")
{


Another way is to write a comparison function that loops through both strings and call std::tolower on each pair of character before comparing them.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
bool isEqualCaseInsensitive(const std::string& str1, const std::string& str2)
{
	if (str1.length() != str2.length())
	{
		return false;
	}
	
	for (std::size_t i = 0; i < str1.length(); ++i)
	{
		if (std::tolower(str1[i]) != std::tolower(str2[i]))
		{
			return false;
		}
	}
	return true;
}

// in main
if (isEqualCaseInsensitive(x, "Alice") ||
    isEqualCaseInsensitive(x, "Bob"))
{
Last edited on Dec 8, 2016 at 10:17am
Dec 11, 2016 at 4:02am
I appreciate the help! but I can't understand :(
Dec 11, 2016 at 4:11am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <string>
#include <cctype>

using namespace std;

string toLowerString(string s) 
{
    int i;
    for(i = 0; i < s.size(); i++) s[i] = tolower(s[i]);
    return s;
}

int main()
{
	string x;

	cout << "Hello! Please enter your name : ";
	cin >> x;

	if (toLowerString(x) == "alice" || toLowerString(x) == "bob")
	{
		cout << "Hello there, " << x << endl;
	}
	else
	{
		cout << "Sorry don't know you haha" << endl;
	}

	system("pause");
	return 0;
}
Last edited on Dec 11, 2016 at 4:15am
Dec 11, 2016 at 6:29am
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <string>

using namespace std;
int main()
{
    string x;
    char y = ' ';
    
    cout<<"Hello! Please enter your name.\n\n";
    cin>>x;
    
    if (x=="Alice" or x=="Bob")
    {
        cout<<"Hello there, "<<x << '\n';
    }
    else
    {
        cout<<"sorry don't know you haha\n";
    }
    
    cout<<"Hello! Please enter your first initial.\n\n";
    cin >> y;
    
    // NOT CASE SENSITIVE - ONE WAY THE HARD WAY
    if (y == 'A' or y == 'a')
        cout<<"Hello there, "<< y << '\n';
    else
        cout<<"sorry don't know you haha\n";
    
    // NOT CASE SENSITIVE - BETTER WAY
    if(tolower(y) == 'a')
        cout<<"Hello there, "<< y << '\n';
    else
        cout<<"sorry don't know you haha\n";
    
    
    
    system("pause");
    return 0;
    
}
Last edited on Dec 11, 2016 at 8:30am
Dec 11, 2016 at 8:15am
closed account (E0p9LyTq)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string>
#include <cctype>

int main()
{
   std::string name;

   std::cout <<"Hello! Please enter your name: ";
   std:: cin >> name;

   std::cout << "\n";

   for (auto& str_loop: name)
   {
      str_loop = toupper(str_loop);
   }

   if (name == "ALICE" || name == "BOB")
   {
      std::cout << "Hello there, " << name << "\n";
   }
   else
   {
      std::cout << "I don't know you...haha\n";
   }
}

Hello! Please enter your name: bob

Hello there, BOB


Hello! Please enter your name: Tom

I don't know you...haha
Last edited on Dec 11, 2016 at 8:16am
Dec 11, 2016 at 8:17am
@FurryGuy
Your example does not preserve the original input the user has entered.
Better create a temporary variable to do the comparison.
Dec 11, 2016 at 8:19am
closed account (E0p9LyTq)
@Natako, if you think that is required, then by all means make it so. I don't see any requirement in the OP post that prevents altering the input.
Topic archived. No new replies allowed.