Null operator
What is the correct way to say that if something is not there then do the following
EX: Code:
if (i=='\0'){ |
From what I understand, you can just compare it to zero to see if the variable is null.
eg. Code:
if(i!=0){ |
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? |
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) { |
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; ta0kira |
Quote:
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> |
Quote:
|
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.
|
All times are GMT -5. The time now is 08:13 AM. |