LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-04-2009, 12:14 AM   #1
jus71n742
Member
 
Registered: Apr 2008
Distribution: Ubuntu 8.04 64bit/ 32bit, FreeBSD 8
Posts: 185

Rep: Reputation: 30
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.
 
Old 01-04-2009, 02:08 AM   #2
AceofSpades19
Senior Member
 
Registered: Feb 2007
Location: Chilliwack,BC.Canada
Distribution: Slackware64 -current
Posts: 2,079

Rep: Reputation: 58
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;
}
 
Old 01-04-2009, 08:16 AM   #3
rob33n
Member
 
Registered: May 2007
Location: Turkey
Distribution: Debian, Windows
Posts: 134

Rep: Reputation: 16
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?
 
Old 01-04-2009, 09:27 AM   #4
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
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.
 
Old 01-04-2009, 02:56 PM   #5
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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
 
Old 01-04-2009, 03:13 PM   #6
jus71n742
Member
 
Registered: Apr 2008
Distribution: Ubuntu 8.04 64bit/ 32bit, FreeBSD 8
Posts: 185

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by AceofSpades19 View Post
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;
}

Last edited by jus71n742; 01-04-2009 at 03:15 PM.
 
Old 01-04-2009, 03:42 PM   #7
AceofSpades19
Senior Member
 
Registered: Feb 2007
Location: Chilliwack,BC.Canada
Distribution: Slackware64 -current
Posts: 2,079

Rep: Reputation: 58
Quote:
Originally Posted by jus71n742 View Post
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.
 
Old 01-04-2009, 03:51 PM   #8
jus71n742
Member
 
Registered: Apr 2008
Distribution: Ubuntu 8.04 64bit/ 32bit, FreeBSD 8
Posts: 185

Original Poster
Rep: Reputation: 30
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.
 
Old 01-04-2009, 04:24 PM   #9
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
What is meant by " file > /dev/null 2>&1 </dev/null " attockonian Linux - Newbie 5 06-30-2006 10:51 PM
JavaScript:: alert(node) shows null, but node != null taylor_venable Programming 1 05-01-2006 09:51 PM
fstab-sync: error: libhal_ctx_init_direct: (null): (null) rpz Linux - Hardware 1 11-01-2005 05:42 AM
C++ operator += uman Programming 1 02-20-2005 04:37 PM
~ operator linuxanswer Programming 7 04-08-2004 04:56 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration