LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Null operator (https://www.linuxquestions.org/questions/programming-9/null-operator-694809/)

jus71n742 01-04-2009 12:14 AM

Null operator
 
What is the correct way to say that if something is not there then do the following


EX:
Code:

if (i=='\0'){
i=p*r*t;

I was told in cpp that the null operator is '\0' but I am not getting the result I want. and no this isn't homework. I am tinkering with cpp and trying to teach myself. figured simple interest would be a good place to start. thanks.

AceofSpades19 01-04-2009 02:08 AM

From what I understand, you can just compare it to zero to see if the variable is null.
eg.
Code:

if(i!=0){
  i=p*r*t;
}


rob33n 01-04-2009 08:16 AM

But it give us a result if the i different from 0. So i can be 1,2,3,... etc
'\0' is using for strings. What is your purpose for this example?

taylor_venable 01-04-2009 09:27 AM

If i is a pointer then you should test to ensure it is not pointing to null (which is memory address 0x0). This is different from the NUL (note the spelling - only one "l") character which is '\0' and represents the end of a string. To do the comparison, you could do if (i == 0x0) or since truth is non-zero in C and C++ you could do if (!i) but the method I prefer is to use NULL (in C you may have to #define NULL 0x0 first):
Code:

if (i == NULL) {
  /* your code here */
}

This is the clearest way of writing it, because it obviously indicates that you're determining if the memory address is valid.

ta0kira 01-04-2009 02:56 PM

POD implicitly converts to a boolean value to denote zero/non-zero; you don't need to compare it to any variant of zero.
Code:

if (!i) i=p*r*t;
In fact, boolean operators in C return int, which is modified in C++ with bool (normally only uses LSB,) which allows binary operators to yield true boolean results.
ta0kira

jus71n742 01-04-2009 03:13 PM

Quote:

Originally Posted by AceofSpades19 (Post 3396469)
From what I understand, you can just compare it to zero to see if the variable is null.
eg.
Code:

if(i!=0){
  i=p*r*t;
}


That is saying that if i is not equal to zero then calculate isn't it? I need to to be if i== 0 then right? so then if the user input is 0 then for that cin calculate for that missing variable. correct?

Or just use the if(!i) i=p*r*t and so on and so forth for the rest of the code. but how would the user denote that i is the missing variable? use 0?

Here is what I have so far
Code:

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
double r;
int p,t,i;
cout <<"What would you like to find?(i,p,r,t)(use \0 if missing): ";
cin>>i>>p>>r>>t;
if (i =='\0'){
i=p*r*t;
cout<<"Your interest is $"<<i<<endl;
}
else if(p=='\0'){
p=i/(r*t);
cout<<"Your principle is $"<<p<<endl;
}
else if(r=='\0'){
r=(i/(p*t))*100;
cout<<"Your rate is "<<r<<"%"<<endl;
}
else{
t=i/(p*r);
cout<<"Your time is "<<t<<"years"<<endl;
}
return 0;
}


AceofSpades19 01-04-2009 03:42 PM

Quote:

Originally Posted by jus71n742 (Post 3397043)
That is saying that if i is not equal to zero then calculate isn't it? I need to to be if i== 0 then right? so then if the user input is 0 then for that cin calculate for that missing variable. correct?

Or just use the if(!i) i=p*r*t and so on and so forth for the rest of the code. but how would the user denote that i is the missing variable? use 0?

Here is what I have so far
Code:

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
double r;
int p,t,i;
cout <<"What would you like to find?(i,p,r,t)(use \0 if missing): ";
cin>>i>>p>>r>>t;
if (i =='\0'){
i=p*r*t;
cout<<"Your interest is $"<<i<<endl;
}
else if(p=='\0'){
p=i/(r*t);
cout<<"Your principle is $"<<p<<endl;
}
else if(r=='\0'){
r=(i/(p*t))*100;
cout<<"Your rate is "<<r<<"%"<<endl;
}
else{
t=i/(p*r);
cout<<"Your time is "<<t<<"years"<<endl;
}
return 0;
}


my bad, I just realized comparing it to zero only works to see if pointers are null. as was said above, '\0' only works for char not int type.

jus71n742 01-04-2009 03:51 PM

OK OK then that makes a little more sense.. I do have it working now. I dropped the *100 on the rate calculation and have everything that if the user inputs 0 then it will calculate for that value. only thing that I noticed...with the rate formula that will only give you a decimal answer. I have r as a double. I tested with the *100 still in the r formula and I get a whole number but the decimal place is 2 to many. I took out the *100 and it is correct. I am confused.

taylor_venable 01-04-2009 04:24 PM

Oh, now with context the question is much easier to address. Don't use \0 for your checks, when other people read your code it implies NUL not zero (even though NUL is ASCII code zero, so it might work sometimes but it's confusing to read). My advice would be to prompt one-at-a-time and read strings, then convert these to numbers. If the user just presses <ENTER> then treat that as the thing to solve for. Also note that division in C and C++ is integral on integral types; if you divide ints by ints you'll get an int, so 1/2 == 0 and probably not what you want. Just read floats in the first place and you won't have to worry about it.


All times are GMT -5. The time now is 08:13 AM.