Since the very first time I programmed c++ I've been trying to make a program that looks like this

#include <iostream>

using namespace std;

int main()
{
    char name;
    cout << "please type in your name >> ";
    cin >> name;
    if(name == 'kevin'){
            cout << "hello bitch" << endl;
            }
            else{
                 cout << name << ", what a lovely name" << endl;
            }
    cin.get();
    cin.get();
    return 0;
}

the only problem is that the char operator only reads the first sign, so if i type in kevin it only output "k, what a lovely name"

if i cange the program to this

#include <iostream>

using namespace std;

int main()
{
    char name;
    cout << "please type in your name >> ";
    cin >> name;
    if(name == 'k'){
            cout << "hello bitch" << endl;
            }
            else{
                 cout << name << ", what a lovely name" << endl;
            }
    cin.get();
    cin.get();
    return 0;
}

and then type in k, it will say hello bitch, if I try to change the char name; to char name[30]; I receive a error message saying "[Warning] character constant too long for its type " and "ISO C++ forbids comparison between pointer and integer "

is there any way i can get this thing working?:?:

Recommended Answers

All 9 Replies

Three small changes :

1>Use #include<string> in the beginning
2>Take name as

string name;

rather than char name.
3>Use "kevin" rather than 'kevin' in the check.

if(name == 'kevin'){

This is quite wrong,

Firstly Make name into a C-styled array as in char name[30] Then, you should use double quotes (") and not (').

---------------------------------EDIT----------------

if(name == "kevin"){//This will not work aswell!!

You will need to use the function strcmp(); if you wish to compare.
The next time please avoid using word's which are considered abusive.

commented: That wont work,It needs to be atring of class string. +0

@skydiploma:

The "==" comparision for strings given in namespace std is given only for strings of class string.

@KangarooBlood

Make the changes given in post #3 it will work.

And ya don't use abusive words on the forum please.

commented: I almost forgot that It was a c-styled string. +5

Three small changes :

1>Use #include<string> in the beginning
2>Take name as

string name;

rather than char name.
3>Use "kevin" rather than 'kevin' in the check.

Thank You, at last it works!!!

You should either use a string from #include<string> Or else

Use the function strcmp(char ,char)

@skydiploma :

Use the function strcmp(char ,char)

Missing out on several things today ha buddy ???
Its strcmp(char *,char *) for strings. ;)

Well it seems that "today is not my day" .

Maybe its because of my anticipation towards the Wimbledon final. I am a big Roger Federer Fan!!

However I just realised that it is

strcmp ( const char * , const char *);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.