problems on using the if and else statement and updating the data.
-
Right now Im new to this programming and Im having the problems of using the if and else statement on (data && data). Next is the update part which i really dont get it. Saw a lot of videos of using MySQL instead of normal programming update. So here is my code
f (ui-> AddOn1 -> isChecked()){ price = 2.50; } else if (ui-> AddOn2 -> isChecked()){ price = 1.20; } else if (ui-> AddOn3 -> isChecked()){ price = 1.10; } else if (ui-> (AddOn1 + AddOn2 )-> isChecked()){ price = 3.70; } else if (ui-> (AddOn1 + AddOn3 )-> isChecked()){ price = 3.60; } else if (ui-> (AddOn2 + AddOn3) -> isChecked()){ price = 2.30; } else if (ui-> (AddOn1 + AddOn2 + AddOn3) -> isChecked()){ price = 4.80; } total = price; total = ui -> lbTotal ->text().toDouble()+price; ui ->lbTotal->setText(QString::number(total,'f',2));
-
@Mr-Nobody645
if ... else
is just C++.In principle your code approach looks about what is necessary.
But what in the world is
else if (ui-> (AddOn1 + AddOn2 )-> isChecked()){
supposed to be?? Is it maybe:
else if (ui-> AddOn1-> isChecked() && ui-> AddOn2-> isChecked()){
? In which case you'll need to test that before testing
ui-> AddOn1-> isChecked()
orui-> AddOn2-> isChecked()
individually.You should read up on basic C++/programming. It's not to do with Qt, and you won't be able to do anything effectively in Qt (or any other toolkit) if you have simple programming issues. I realize this is your first post, but very kindly/helpful advice you should know that programming issues are better dealt with as such than on a Qt-specific forum.
-
Do you actually want to ask some question here? What is your problem?
Do you mean to ask how to check multiple values at once in an
if
statement?ui-> (AddOn1 + AddOn2 + AddOn3) -> isChecked()
Here is something which should work:
if (ui->AddOn1->isChecked() && ui->AddOn2->isChecked() && ui->AddOn3->isChecked())
But for sake of readability and maintenance (and, to some degree, performance), I'd suggest to get state first, and check later:
const bool isAddOn1 = ui->AddOn1->isChecked(); const bool isAddOn2 = ui->AddOn2->isChecked(); const bool isAddOn3 = ui->AddOn3->isChecked(); if (isAddOn1 && isAddOn2 && isAddOn3) // ...
All modernt compilers also understand
and
as a synonym to&&
so you can do:if (isAddOn1 and isAddOn2 and isAddOn3)
-
omg ty for the if and else part. Really appreciate your help.
-
im appreciate for both of you on helping me in the if and else part. However, when i try to checked AddOn1 and AddOn2, i got my price 2.50 instead of 3.70.
-
@Mr-Nobody645 said in problems on using the if and else statement and updating the data.:
im appreciate for both of you on helping me in the if and else part. However, when i try to checked AddOn1 and AddOn2, i got my price 2.50 instead of 3.70.
obviously, as
if (ui-> AddOn1 -> isChecked()){
price = 2.50;
}comes before everything else and its true so the rest of the checks is not executed
-
@Mr-Nobody645 said in problems on using the if and else statement and updating the data.:
However, when i try to checked AddOn1 and AddOn2, i got my price 2.50 instead of 3.70.
That's why I wrote earlier:
In which case you'll need to test that [
if (ui-> AddOn1-> isChecked() && ui-> AddOn2-> isChecked())
] before testingui-> AddOn1-> isChecked()
orui-> AddOn2-> isChecked()
individually. -
@sierdzio said in problems on using the if and else statement and updating the data.:
All modernt compilers also understand
and
as a synonym to&&
so you can do:I am speechless! Apart from the fact that I have never heard of this (old time C/C++ programmer, now returned to new C++). And apart from the fact that introducing
and
as a language keyword would potentially break one million old C/C++ programs which could have usedand
as a variable?You are obviously more expert than I, but do you want to recommend this to a newbie? I have never noticed
and
used in a C++ program, if OP is to produce standard code, and recognize others' code, would you not recommend sticking with&&
? -
@JonB apparently part of the standard since c++98
https://en.cppreference.com/w/cpp/keyword/and -
@J-Hilk , @sierdzio
Absolutely shocking! I see from https://en.cppreference.com/w/cpp/language/operator_alternative that there are actually 11 new reserved words for operators. Includingnot
, and even the stringcompl
, which I might have used as variables and would break upgrading to C++ '98.Thinking aloud, I believe all C operators were symbols and not words, part of the charm (for right or for wrong) of the language.
With all due respect to @sierdzio's comment, which I accept & respect, I have still never seen a piece of C++ code using any of these.... [And hence would caution a beginner against adopting these.]
[C++ looks like it's trying to be a Python-wannabe ;-) ]
-
@JonB said in problems on using the if and else statement and updating the data.:
I have still never seen a piece of C++ code using any of these
Me neither :-)
-
I have seen it but very, very rarely.
And hence would caution a beginner against adopting these
I'm split on this :D One one hand,
&&
is clearly, vastly, totally the established and well-known convention practically everywhere. So using it is definitely a good idea.But on the other hand,
and
is more readable and convenient. So it is also a good idea. -
@sierdzio said in problems on using the if and else statement and updating the data.:
But on the other hand, and is more readable and convenient.
In Pascal or Python, yes. When scanning C/C++ I expect to see all operators as symbols (and not some but not others) Next C++ will be offering
begin
/end
for{
/}
! [Some people used to do all this words-for-symbols via C#define
s.] I give up on the modern world, it's all going to pot... ;-) -
@JonB said in problems on using the if and else statement and updating the data.:
Next C++ will be offering begin/end for {/}!
you can use
{ ??< } ??> [ ??( ] ??) # ??= \ ??/ ^ ??' | ??! ~ ??-
but that was removed in c++17, :)
-
but that was removed in c++17, :)
Even better --- you start using new language constructs and then they remove them so that your code gets broken!
FWIW, I note from https://en.cppreference.com/w/cpp/language/operator_alternative
There are alternative spellings for several operators and other tokens that use non-ISO646 characters.
So the motivation never was "clarity", it's to do with people having funny keyboards....
-
@JonB said in problems on using the if and else statement and updating the data.:
funny keyboards
The hardware is innocent ;-P
-
whats next, espousing the virtues of trigraphs?