Issue with converting an if-else to a conditional statement

How do I convert this if-else statement to a conditional statement?

1
2
3
4
5
6
/*
 if (n != 1)
 	cout << "There are " << n << " items left.";
else
	cout << "There is 1 item left.";
*/


Here is what I tried:

 
n != 1 ? "There are " n " items left." : "There is 1 item left.";


Just having trouble finding the correct format. In a previous try, I assigned the text to string, but I'm still having trouble trying to determine the correct format. My book only shows me examples when dealing with numbers.
If this can be done, why do you want to? The first bit of code is easy to read and understand.

I am thinking to actual implement this it will end up looking messier than your above example.
The original code best describes what you want. I don't recommend you to convert it into conditional statement.

If you really want to do that, you could try this

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <sstream>
#include <string>

int main(void)
{
    int n = 2;
    
    std::cout << (n != 1 ? ((std::stringstream&)(std::stringstream() << "There are " << n << " items left.")).str() : std::string("There is 1 item left."));
    
    return 0;
}


AND THIS IS TERRIBLE!
Oh, it was just for an assignment. Our teacher wanted us to try it that way, but yeah I agree. It's messy haha, was sad I couldn't use if-else.
Topic archived. No new replies allowed.