Answer to Question #167280 in C++ for Mary Hubbard

Question #167280

Create a conditional expression that evaluates to string "negative" if userVal is less than 0, and "non-negative" otherwise. Ex: If userVal is -9, output is:

-9 is negative.
1
Expert's answer
2021-02-26T19:54:03-0500
#include <iostream>

int main()
{
    int userVal;
    std::cout << "Please enter number: ";
    std::cin >> userVal;

        if(!std::cin)
        {
            std::cout << "Bad input\n";
            return 1;
        }

    std::cout << userVal << " is "
              << (userVal < 0 ? "negative" : "non-negative")
              << ".\n";
    
    return 0;    
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment