n00b If Statement Question
Hi there,
I'm beginning work on a University project, but I'm having trouble with a section of code. The following code asks the user to decide whether or not they require delivery, and if so, how many miles the delivery distance will be. A different delivery charge is applied based upon distance ranges (0-15, 16-25 etc). Finally, the delivery cost is output.
The problem i'm having is that the delivery cost is always output as 0, when it should be of some value. Can anyone identify if i've made any simple errors, as i've only been using c++ for a day or two.
Thanks,
GT
if (DeliveryAnswer == 'Y' || DeliveryAnswer == 'y')
{
if (DeliveryDistance == '>=0' && DeliveryDistance == '<=15')
{
DeliveryCost = Cost/10;
DeliveryRequired = true;
}
if (DeliveryDistance == '>=16' && DeliveryDistance == '<=25')
{
DeliveryCost = (Cost/100) * 5;
DeliveryRequired = true;
}
}
else
{
DeliveryCost=0;
DeliveryRequired = false;
}
[1223 byte] By [
gt123] at [2007-11-20 2:15:59]

# 1 Re: n00b If Statement Question
im by far an expert to c++ iom still a beginner learner...
if (DeliveryAnswer == 'Y' || DeliveryAnswer == 'y')
{
if (DeliveryDistance == '>=0' && DeliveryDistance == '<=15')
{
DeliveryCost = Cost/10;
DeliveryRequired = true;
}
just ive never used ' in my syntax before? just i notice you have put them around your if conditions and variables
# 2 Re: n00b If Statement Question
What is the data type of DeliveryDistance? If it is an integral type then remove your == and tick (') marks. Your code should look something like this...
if (DeliveryDistance >=0 && DeliveryDistance <=15)
sszd at 2007-11-9 1:10:07 >

# 3 Re: n00b If Statement Question
Hi all
Do your code is compiled correctly?
Expression '>=0' shouldn't be valid, because '' defines a single character, not a string.
In any case, expression "DeliveryDistance == '>=0'" seems very strange.
What does are you trying to do?
# 4 Re: n00b If Statement Question
What is the data type of DeliveryDistance? If it is an integral type then remove your == and tick (') marks. Your code should look something like this...
if (DeliveryDistance >=0 && DeliveryDistance <=15)
This fixed it, thanks alot. :)
gt123 at 2007-11-9 1:12:06 >

# 5 Re: n00b If Statement Question
The problem i'm having is that the delivery cost is always output as 0, when it should be of some value. Can anyone identify if i've made any simple errors, as i've only been using c++ for a day or two.Please show the complete code. As the others pointed out, expresions like '>=0' are not valid C++, so this should not compile. But you are saying it compiles and gives you 0 as output. To understand why it does that, we will also need to see as what your variables are declared and how you read input.
# 6 Re: n00b If Statement Question
Please show the complete code. As the others pointed out, expresions like '>=0' are not valid C++, so this should not compile. But you are saying it compiles and gives you 0 as output. To understand why it does that, we will also need to see as what your variables are declared and how you read input.
It is a valid C++ multicharacter literal, and it's an int.
The interpretation (mainly big-endian or little-endian order of chars in the int) of this multicharacter literal is implementation defined.
EDIT: Multi-character literals are not so uncommon; Have a look at:
http://www.google.com/codesearch?hl=en&lr=&q=%5E%5B%5E%27%22%2F%2A%5D%2A%27%5B%5E%5C%5C%27%5D%5B%5E%27%5D%7B3%7D%27.%2A%3B+lang%3Ac%28%5C%2B%5C%2B%29%3F&btnG=Search
# 7 Re: n00b If Statement Question
It is a valid C++ multicharacter literal, and it's an int.
The interpretation (mainly big-endian or little-endian order of chars in the int) of this multicharacter literal is implementation defined.
EDIT: Multi-character literals are not so uncommon; Have a look at:
http://www.google.com/codesearch?hl=en&lr=&q=%5E%5B%5E%27%22%2F%2A%5D%2A%27%5B%5E%5C%5C%27%5D%5B%5E%27%5D%7B3%7D%27.%2A%3B+lang%3Ac%28%5C%2B%5C%2B%29%3F&btnG=Search
OK.
Interesting, very interesting.
But in this case code won't work fine, because when program executes "if (DeliveryDistance >=0 && DeliveryDistance <=15)" instruction, DeliveryDistance is 'Y' or 'y', ie a integer value greater than 15 or 25, so program won't enter in if blocks.
# 8 Re: n00b If Statement Question
But in this case code won't work fine, because when program executes "if (DeliveryDistance >=0 && DeliveryDistance <=15)" instruction, DeliveryDistance is 'Y' or 'y', ie a integer value greater than 15 or 25, so program won't enter in if blocks.
No, DeliveryAnswer is 'Y' or 'y'!!!
sszd at 2007-11-9 1:16:11 >

# 9 Re: n00b If Statement Question
No, DeliveryAnswer is 'Y' or 'y'!!!
Infact.
So if (DeliveryDistance >=0 && DeliveryDistance <=15) and if (DeliveryDistance >=16 && DeliveryDistance <=25) will be always false, because the INTEGER value of CHAR 'Y', or 'y'is greater than 25: in C/C++ one can declare an int variable and assign a constant char to it:
int DeliveryDistance = 'Y';
and variable will hold the ASCII code of char (in this case DeliveryDistance=89).
# 10 Re: n00b If Statement Question
I believe you're confused. Look at the OP's original post. DeliveryAnswer is 'Y' or 'y'. DeliveryDistance, I believe, must be an integer value, and probably ranges within the correct values, as the OP indicated the correction worked!!!
sszd at 2007-11-9 1:18:08 >

# 11 Re: n00b If Statement Question
I believe you're confused. Look at the OP's original post. DeliveryAnswer is 'Y' or 'y'. DeliveryDistance, I believe, must be an integer value, and probably ranges within the correct values, as the OP indicated the correction worked!!!
OOOPSSSSSSSSSSS
You're right!!!
I'm sorry...