Which type for characters?

I'd like to program a very primitive program in which you input a name and it controls whether this name is in the library or not. If yes it shoud output Hello input_Name... the problem I don't know how to declare names. thx for your help.

#include <iostream>

int main ()
{

int n;
std::cin >> n;
char m = Peter;
char s = Lily;

if (n == m)
std::cout << "Hello Peter";
else
std::cout << "Account not available";

if (n == s)
std::cout << "Hello Lily";



return 0;
}
char is for characters. like A or B.

If you want names, use string
http://www.tutorialspoint.com/cplusplus/cpp_strings.htm

Or if you want names with spaces, like first and last name, use getline
http://www.cplusplus.com/reference/string/string/getline/
thx... but I still get the message that a pointer and a integer can't be compared...


#include <iostream>

int main ()
{

char n;
std::cin >> n;
char name1 [] = "Peter";
char name2 [] = "Lily";

if (n == name1)
std::cout << name1;
else
std::cout << "Account not available";

if (n == name2)
std::cout << name2;



return 0;
}
Well yeh, n is a char, name1 is a char array. You cant compare them. And you also havent used anything I told you about. You are allowed to use c++ right? So use string.
I agree with TarikNeaj about using the string instead.

Because C-strings are stored in char arrays, you cannot use the relational operators to
compare two C-strings. To compare C-strings, you should use the library function strcmp .
This function takes two C-strings as arguments and returns an integer that indicates how
the two strings compare to each other.

For example:

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>

int main()
{

    char *n = nullptr;
    n = new char;

   char name1[] = "Peter";
   char name2[] = "Lily";

   std:: cin >> n;

    if (strcmp(n, name1) == 0)
	   std::cout << name1;

    else if (strcmp(n, name2) == 0)
	   std::cout << name2;

    else
	  std::cout << "Account not available" << std::endl;

    n = nullptr;


    return 0;
}


That being said, unless it is a specific homework, I would use the string and not char arrays.
Last edited on
I agree with chicofeo and will get back to you with that
I would recommend using a string, then using a series of if, else if , or else
so that would be
Here it is.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
using namespace std;
int main()
{
    string letter;
    cout<<"Enter your letter to see account information  ";
    cin>>letter;
    if(letter=="m")
    {
        cout<<"\nHello, Peter!";
    }
    else if (letter=="s")
    {
        cout<<"\nHello, Lily!";
    }
    else
    {
        cout<<"\nAccount details not available.";
    }
    cout<<"\nGoodbye!!!";
    return 0;
}
1
2
3
4
    char *n = nullptr;
    n = new char; //pointing to just 1 character, also ¿couldn't this be on the above line?
   std:: cin >> n; //sees a char*, treat it as such. Also, don't forget the '\0' terminator
   n = nullptr; //leak 

You could have a username and a password like accounts and stuff
Thank you for answering!!!... Tariks solution worked well for me!! :)
ok
Topic archived. No new replies allowed.