ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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
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;
}
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.