C++: Output with Variables

Post Reply
User avatar
Jason
Site Admin
Posts: 749
Joined: Tue Jul 01, 2025 10:13 am

Code: Select all

// output-with-variables.cpp

// Here is one of two ways shown on this thread to print out text and variables.

#include <iostream>

#include <string>

using namespace std;

int main( )
{
	string team_name = "Houston Texans";
	
	int in_the_top_10_expressed_as_number = 10;
	
	string my_opinion_about_the_Detroit_Lions = "My opinion is that the"  +  "  " + "team_name" +  "  " + "are a top"  +  "  " + in_the_top_10_expressed_as_number + "  " + "team in 2025.";
	
	cout << my_opinion_about_the_Detriot_Lions;
	
	return 0;
	
}	

Code: Select all

// output-with-variables-2.cpp

// Here is another way to output text and variables.

#include <iostream>

#include <format>

using namespace std;

int main( )
{
	std::string team_name = "Houston Texans";
	
	int in_the_top_10_expressed_as_number = 10;
	
	std:string my_opinion_about_the_Houston_Texans = 
	std::format("My opinion is that the {1} are a top {0} team in 2025.", team_name, in_the_top_ten_expressed_as_a_number); 
	
	std::cout << my_opinion_about_the_Houston_Texans << std::endln;
	
	return 0;
	
}	

 

POSTREACT(ions) SUMMARY

Post Reply