4 Replies - 8944 Views - Last Post: 03 October 2016 - 12:17 PM Rate Topic: -----

#1 ogn   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 02-June 16

Check whether the cstring is empty.

Posted 03 October 2016 - 08:50 AM

Hi guys, i would like to know how to check whether the cstring is empty.

 for (int i=0; i<room; i++)
    {
        cout<<setw(4)<<i+1<<"\t";
        in>>numcomp;
        for(int j=0; j < numcomp;j++)
        {
            cout<<setw(9)<<j+1<<":";
            if (ptr[i][j].userID=NULL)
            {
                cout<<"Empty"<<"\t";
            }
            else
            {
                cout<<ptr[i][j].userID<<"\t";
            }
        }
        cout<<endl;
    }



I have try NULL but it is not working. Can I know how to do it.

This post has been edited by ogn: 03 October 2016 - 08:53 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Check whether the cstring is empty.

#2 Atli   User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4241
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

Re: Check whether the cstring is empty.

Posted 03 October 2016 - 08:56 AM

I don't know much about cstrings (or C/C++ in general), but there does seem to be a simple syntax error in there:
if (ptr[i][j].userID=NULL)


Shouldn't there be two equals signs there (==) to perform a check, rather than just one, which will perform an assignment?
Was This Post Helpful? 0
  • +
  • -

#3 ogn   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 02-June 16

Re: Check whether the cstring is empty.

Posted 03 October 2016 - 10:10 AM

After I solve the problem, there are one more problem appear, there is my Empty word didn't appear. If suppose to be if the array is empty, it should display a word empty. Can i know what problem is it?
Was This Post Helpful? 0
  • +
  • -

#4 horace   User is offline

  • D.I.C Lover
  • member icon

Reputation: 768
  • View blog
  • Posts: 3,832
  • Joined: 25-October 06

Re: Check whether the cstring is empty.

Posted 03 October 2016 - 10:37 AM

are you considering the standard C++ functions in <cstring> where you can use strlen() to check if a string is empty, e.g.
#include <cstring>
...

char c[20]="";
if(strlen(c)==0)
                cout<<"Empty"<<endl;


or Microsoft's CString in Visual Studio
https://msdn.microso...3(v=vs.60).aspx
https://msdn.microso...y/ms174288.aspx
Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3872
  • View blog
  • Posts: 14,211
  • Joined: 08-August 08

Re: Check whether the cstring is empty.

Posted 03 October 2016 - 12:17 PM

The first character in an empty string will be the terminator.
	char C[10] = {""};
	if(C[0] == '\0') {
		cout << "Empty\n";
	}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1