Hi folks, just a queary about checking chars. I have a char[] variable. I ask the user a question and the user types a response and presses enter. I need to check the variable to see if it said particular things and more importantly if it said nothing.

#include <iostream>
#include <fstream>
using namespace std;

char _cFileName[100];

int main()
{
    cout<<"Enter the name of the file to open or press enter for HELP: ";
    cin>>_cFileName;

    if(_cFileName == 'HELP')
    {
        cout<<"DISPLAY HELP FILE"<<endl;
        system("PAUSE");
        return 0;
    }

    if(_cFileName == '')
    {
        cout<<"DISPLAY HELP FILE"<<endl;
        system("PAUSE");
        return 0;
    }

    ifstream infile("data.txt");
    cout<<"Your file is "<<sizeof(infile)<<" bytes."<<endl;

    system("PAUSE");
    return 0;
}

Apparently using either "HELP" or 'HELP' or HELP when using the if statement does not work. So I am just wondering where I am going wrong. Perhaps chars are not the way to go when doing this?

Also as an aside. I have not had a chance to test this yet, but does sizeof() work on streams?

Any help is much appriciated.

You can't use the == operator to compare arrays. Well, you can, but it almost certainly doesn't do what you expect or want.

Ideally you'd use a std::string object instead of an array of char for strings, because then the == operator is overloaded and does what you want. Otherwise, you're stuck with the C-style method of calling strcmp():

#include <cstring> // Include this guy for strcmp()

...

if(strcmp(_cFileName, "HELP") == 0)
{
    // The user typed "HELP"
}

Now the user typing nothing is a bit harder because nothing will be written to the string. You must initialize the array to an empty string, and then you can compare the first character against the string termination character to see if nothing was written:

_cFileName[0] = '\0'; // Clear the string

...

if (_cFileName[0] == '\0')
{
    // The user typed nothing
}

On a side note, the system() function is declared in <cstdlib>, which you neglected to include. This is an error that your compiler probably overlooked, but you shouldn't rely on that behavior.

as deceptikon said, you should stick with the std::string objects.

#include <iostream>
#include <string>
using namespace std;

int main(){
    string choice;
    cout<<"A or B?";
    cin>>choice;
    if (choice=="A")
        cout<<"A\n";
    else if (choice=="B")
        cout<<"B\n";
    else 
        cout<<"Invalid command.\n";
    return (0);
}

Here's a quick example of how you can compare strings, via the std::string.

Hi guys, thanks for your help so far. It seems you both think strings would be better so I shall adjust the program and ditch the char array. But how would I tell if the user enters nothing in the context of a string? Strings are not arrays so they dont end in \0 right? So how would i check if there is anything in the string at all?

strings can be converted to C arrays, meaning C style arrays by the funcion string.c_str(). However, you can check if a string is empty using the comparison operator ==:

#include <iostream>
#include <string>
using namespace std;

int main(){
    string a;
    if (a=="") cout<<"Empty.\n";
    else cout<<"Not empty.\n";
    return (0);
}

"" denotest an empty strinng, so by comparing an existing string with "" will give the rite answer.

Strings are not arrays so they dont end in \0 right? So how would i check if there is anything in the string at all?

There are a number of options, but if you want to say what you mean then this one stands out:

string s;

if (getline(cin, s))
{
    // Check for emptiness
    if (s.empty())
    {
        cout << "Empty!" << endl;
    }
    else
    {
        cout << "You typed '" << s << "'" << endl;
    }
}

Hey thanks a lot guys, it's all sorted and the program is running! Thanks!

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.