{Beginner}{G++} Having problem using a string in if statement

Here is a simple program.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>

using namespace std;

int main()
{
        string test;
        cout<<"Enter a string:\t";
        if (test = "")
        { cout<<"NULL STRING..."<<endl; main(); }
        else
        { cout<<test<<endl; return(0); }
}


Here is the error that g++ returns.
morestrings.cpp: In function ‘int main()’:
morestrings.cpp:10: error: could not convert ‘test.std::basic_string<_CharT, _Traits, _Alloc>::operator= [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((const char*)""))’ to ‘bool’
Can anyone please explain why this error is returned. And if strings cannot be used in if statements, how to handle decisions involving strings?
Did you mean if (test == "")?

What you've got there now is an assignment.
Also: you can't call main() in your code, only the OS can
Last edited on
Awwww shittttt man!!!

I was supposed to know that. Where the f*** were my brains!!! Damn, sorry for such a silly question... I have been wondering over this statement for like 48 hours. Think I am going crazy, being such a dumbhead. Sorry guys!!! Really sorry!
Hahahaha, forgot about logical operators...

And Bazzy, thanks for another piece of information, that would have been my next error and next question, LOLxxx
@Bazzy: I just tested it with modification to if (test == "")...
Well it seems main(); works anyway. An exception I suppose.
It may work on some compiler but it shouldn't
From C++ standard §3.6.1.3:
The function main shall not be used (3.2) within a program.
Topic archived. No new replies allowed.