if statement with strings

i got a problem i'm trying to see if the value for string cmd is set i use

if (cmd.length>0)

but i get error ( invalid use of member (did you forget the `&' ?) )


please help

P.S. this is my first c++ script
length is a function so you have to put parenthesis after the function name.
if (cmd.length()>0)
Or you can use the empty function
if (!cmd.empty())

And don't call C++ code for script
Last edited on
If cmd is a char*, use if( strlen(cmd) > 0 ).
Last edited on
ok it fixed but now another problem.. (sorry if i bother you)

here's the whole code

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 <cstdlib>
#include <iostream>

using namespace std;




int main()
{
    string cmd;
    if (cmd.length()>0)
    {
                if (cmd.compare("help")!=0)
                {
                                           cout << " List of commands :" << endl;
                                           cout << " exit — closes the program " << endl;
                }
                else if (cmd.compare("exit")!=0)
                {
                                                  return EXIT_SUCCESS;
                }
    }
    else
    {                                       
    cout << " INPUT command > " ;
    cin >> cmd ;
    main();

    system("PAUSE");
    }
}


if i input help or exit it sill writes down " INPUT command > " any ideas?
Last edited on
You have to read input before checking the input. Also calling main is not allowed.
Topic archived. No new replies allowed.