[C++] Variable isn't being declared.

Newbie Spellweaver
Joined
Nov 22, 2009
Messages
28
Reaction score
0
I'm currently learning C++ and here's a very simple script i've coded:

Code:
#include <iostream>

using namespace std;

int main()
{
    char name;

    cout<<"Give me you're name and i'll tell you what kind of person you are!\n";
    cout<<"Go ahead! Type it in:\n";
    cin>> name;

    if (name == zair) {
        cout<<"You're a Fag!";
    }

    if (name == byron) {
        cout<<"You must be sexy!";
    }
    else {
        cout<<"Not as gay as zair...";
    }
    cin.get();
}
It wont compile, and it says theres an error in line 13
and that the variable is undeclared. I'm thinking it's because the type variable "char" is not compatible with words but just 1 letter? or is it something else? Thank you all who answer. :)
 
Last edited:
Yes, char can only hold one character. For text you should use string (include string). Also, constant text has to be written like "this" when comparing, too, and actually that's the reason for your current error message: the compiler thinks zair is a variable name.

Oh, another note: with c++ it's advised to use endl with cout instead of '\n' (cout << stuff << endl). In addition to looking super cool, it ensures portability to platforms where the end line character is something other than '\n'.
 
Last edited:
Learn the data types, it'll help alots. :)
 
Use std::string instead of char, add double-quotes around names in your if-statements and return 0 at the end of main. Also, the second if could as well be an else if.
 
Back