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

  • C/C++
  • Thread starter ineedhelpnow
  • Start date
  • Tags
    C++
In summary, the correct expression to print "Special number" if specialNum is -99, 0, or 44 is (specialNum == -99) || (specialNum == 0) || (specialNum == 44). Using a single equals sign instead of double will result in the statement being true and always outputting "special number". It is important to pay attention to the difference between = and == in conditional statements.
  • #1
ineedhelpnow
651
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
  • #2
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 == :)
 
  • #3
i literally read over that piece of code i wrote like ten times and i didnt even notice that i missed a '='. thanks bacterius!
 

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

What is the purpose of solving C++ Logical Ops 2?

Solving C++ Logical Ops 2 allows you to print out a specific message, in this case "Special Number", based on certain conditions being met. This can be useful for organizing and displaying data in a program.

How do I print "Special Number" in C++?

To print "Special Number" using C++, you need to use logical operators (such as == or !=) to compare a variable or input with a specific value. If the condition is met, you can use the cout function to output the message "Special Number" to the console.

Can I use other messages instead of "Special Number"?

Yes, you can customize the message to be printed by changing the value of the cout function. You can also add multiple conditions and corresponding messages using if-else statements.

How do I know if the conditions for printing "Special Number" are met?

You can use logical operators to compare a variable or input with a specific value. If the condition is true, the message "Special Number" will be printed. You can also add cout statements within the conditional statements to check if the conditions are being met.

Can I use this method for other programming languages?

Yes, the concept of using logical operators to print specific messages can be applied to other programming languages as well. However, the syntax and functions may vary depending on the language.

Similar threads

Replies
5
Views
2K
Replies
22
Views
3K
Replies
40
Views
3K
Replies
6
Views
10K
Replies
23
Views
2K
Replies
13
Views
1K
Back
Top