#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;
int main()
{
double accNum;
string scode;
string scodeab;
double minu;
double minam;
double minpm;
cout << fixed << setprecision(2);
cout << "Please enter account number: " << endl;
cin >> accNum;
cout << "Please enter service code: P(for premium) or R(for regular)" << endl;
cin >> scode;
if (scode == "r"||"R")
{cout << "Pleas enter minutes used" << endl;
cin >> minu;
if (minu > 50)
{minu = ((minu - 50) * .20) + 10;
cout << "Your Total Balance is: $" << minu << endl;}
else
{cout << "Your total balance is: $10.00" << endl;}
}
else if (scode == "p" || "P")
{cout << "Pleas enter type A or B: " << endl;
cin >> scodeab;
- 4 Contributors
- forum10 Replies
- 17 Views
- 7 Years Discussion Span
- comment Latest Post by doug65536
gerard4143 371
Maybe if you tried
if ((scode == "r") || (scode == "R"))
vidit_X 29
I think its because you have ORed "R", which is a non-zero value and thus the statement always evaluates to true.
The same problem with your second if statement. And why you using string when you need to store just a character?
thanks to both of you guys you were right the variables needed to be of the char type and the sugestion to seperate the (scode =='r') and the (scode == 'R') statements was exactly what i needed to do thanks it was a subtle change in the syntax but exactly what was needed you guys are life savers!
doug65536 18
Study operator precedence. I know it's boring as hell, think of it as the same kind of thing as learning your times tables -> boring as hell but great to know once you know. It helps a lot to be able to look at an expression and know what order it will be evaluated in.
What it was doing before was evaluating scode == "p" first. If that was true, the whole expression is true because it's logical or (look up short-circuit evaluation).
If scode is not equal to "p", then it evaluated whether "P" is true, which it always is, because "P" is not a null pointer. A literal string evaluates to the pointer to the first character of the string. Therefore the if condition is always true.
ok see i havent learned anything about pointers as of yet and didnt realize that literal strings evaluate to there relitive pointers, so thank you for that information that will help alot. as far as the logic gose i wanted it to evaluate the "p" first if that was true to run the if statement if not true to check wether the "p" was true. i have an ok understanding of operator precedence the biggest issue was realy a matter of not understanding the way strings evaluate so swiching to char instead of strings was a perfect solution. setting up the boolien the way it is now should tell the compiler the same thing as before shouldn't it? its only realy differant synacticaly right? which was another of my issues i have been programing in python for some time now and am not used to c++ syntax. thanx again though for your help you really saved me. i was really worried i wouldnt get this assignment done.
doug65536 18
setting up the boolien the way it is now should tell the compiler the same thing as before shouldn't it? its only realy differant synacticaly right?
Not sure what you mean by "the way it is now" but if you mean like gerard4143's comment, yes that works. I simply explained how the compiler interpreted it the way you originally had it, so you'd know why your original code didn't work.
At first knowing other languages is going to bog you down a little, but soon that's going to turn around and make you able to learn new languages even faster. Programming languages are more alike than they are different, and knowing several sharpens your eye to the kinds of issues that you encountered in this post.
Edited
by doug65536: n/a
yah i was refering to setting the boolien up as if(scode == ('r' || 'R')) as apposed to if((scode == 'r') || (scode == 'R')). the logic of these two staements and oder in which i expected them to evaluate seems the same to me. firs if the 'r' portion is true and if not then if the 'R' portion is true. the only real differance i see is syntax. however if there is somthing i am missing or not understanding about how the order of precidence differs between them, i would verry mutch appreciate any help youd be willing to offer. thanx again for your post
doug65536 18
Character literals evaluate to the ascii value of the character. scode == 'A' is exactly the same thing as scode == 65.
Boolean expressions are calculations. When you evaluate 2 * 2 == 4, the result is the number 1. When you say 2 * 3 == 4, the result is the number 0.
In C, when it is evaluating boolean (true or false) types of expression (in 'if', 'while', 'for' etc), the whole expression is true if the answer is not zero, and it is false if the answer is zero.
When evaluating scode == ('r' || 'R'))
this is the order of operations:
- 'r' || 'R' means the following: if 'r' != 0 then the result is 1, else if 'R' != 0 then the result is 1, else the result is zero
so this is how it would break down:
(scode == ('r' || 'R')
simplifies to (scode == (114 || 82))
when it evaluates 114 || 82, it is essentially a calculation that means, if ((114 is not equal to 0) or (82 is not equal to 0)) then the result is 1, else the result is zero.
114 is not equal to zero, which means it is true. Expressions in C use short-circuit evaluation, so using logical OR, there is no need to evaluate the right hand side, because it is OR, 114 is already true. it can (and WILL) skip the other side of the evaluation because true OR anything is still true.
which means it becomes
scode == 1
And that is not what you intended to check.
ok so the statement (scode == ('r' || 'R')) checks if 'r' or 'R' is a numerical value != to zero, were as the statement if((scode == 'r') || (scode == 'R'))checks the relationship between the variable and the numerical value because of the grouping symbols its going to evaluate dose ('r' || 'R') = true ?, where as (variable == 'char') || (variable == 'char') allows for evaluation of two relational statements right?