C/C++ Solving C++ Logical Ops 2: Print "Special Number

  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    C++
AI Thread Summary
The discussion focuses on creating a C++ expression to print "Special number" for specific values of `specialNum` (-99, 0, or 44). A common mistake highlighted is using the assignment operator `=` instead of the equality operator `==`, which leads to incorrect behavior in the condition check. The correct expression should be `(specialNum == -99) || (specialNum == 0) || (specialNum == 44)`. Participants emphasize the importance of distinguishing between these operators to avoid logical errors in code. Overall, attention to detail in syntax is crucial for correct program functionality.
ineedhelpnow
Messages
649
Reaction score
0
Write an expression that prints "Special number" if specialNum is -99, 0, or 44.

Sample program:

Code:
#include <iostream>
using namespace std;

int main() {
   int specialNum = 0;

   specialNum = 17;
   
   if (<STUDENT CODE>) {
      cout << "Special number" << endl;
   }
   else {
      cout << "Not special number" << endl;
   }

   return 0;
}

i thought it would be
Code:
(specialNum == -99) || (specialNum == 0) || (specialNum = 44)
but its not
 
Technology news on Phys.org
ineedhelpnow said:
Write an expression that prints "Special number" if specialNum is -99, 0, or 44.

Sample program:

Code:
#include <iostream>
using namespace std;

int main() {
   int specialNum = 0;

   specialNum = 17;
   
   if (<STUDENT CODE>) {
      cout << "Special number" << endl;
   }
   else {
      cout << "Not special number" << endl;
   }

   return 0;
}

i thought it would be
Code:
(specialNum == -99) || (specialNum == 0) || (specialNum = 44)
but its not

That last condition is an assignment. You want specialNum == 44, because "specialNum = 44" is a statement that means "assign 44 to specialNum, and return that value". Thus it logically becomes true (as 44 is not zero) and you always get "special number" (and if you printed specialNum it would mysteriously always be 44.

Be careful between = and == :)
 
i literally read over that piece of code i wrote like ten times and i didnt even notice that i missed a '='. thanks bacterius!
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top