printf - Pulling multiple variables in one line

Sep 3, 2020 at 3:46pm
Hey Guys,

I'm a noob programer. Im tryin to figure out how to pull multiple variables into one line of printf. the code below should be in a sinlge line! i can't crack it! Can anyone HElP?

printf ("The value of number is %c\n", number);
printf ("the value of fraction is %f\n", fraction);
printf ("and the value of letter is %d\n", letter);
Sep 3, 2020 at 4:06pm
std::cout << "The value of number is " << number << '\n' << "the value of fraction is " << fraction << '\n' << "and the value of letter is " << letter << '\n';
Sep 3, 2020 at 4:12pm
What @Repeater is saying is that if you are using C++, you should be using C++ functions.

The printf function is a carryover from C.

If you must use printf, the statement would be:

printf ("The value of number is %c\nthe value of fraction is %f\nand the value of letter is %d\n", number, fraction, letter);
Sep 3, 2020 at 4:36pm
Like so
1
2
3
4
printf("The value of number is %c "
       "the value of fraction is %f "
       "and the value of letter is %d\n",
       number, fraction, letter);

Sep 3, 2020 at 5:39pm
@salem! loved that line! It worked to perfection! I now have that on my toolbox! appreciate it guys!!!!!!!!!!!!!!

CeErre
Sep 3, 2020 at 9:55pm
It is worth noting that to concatenate literal strings, they just need to be written together. So in:

1
2
3
	std::string text = "foo"  "bar";

	std::cout << text << std::endl;


text becomes foobar as the literal strings "foo" and "bar" are concatenated together as they are written together. Any number of white-space chars can separate the literal strings. So

1
2
3
4
5
6
7
	std::string text = "foo" 



 "bar";

	std::cout << text << std::endl;


has the same meaning and text is still foobar. The code displaying:

foobar


in both cases.
Topic archived. No new replies allowed.