Basic templates

1
2
3
4
5
6
7
8
9
10
template< class T , class U ,class X >
void Printoutput( T value ,U value , X value  ){
	cout << value << value << value << endl;
}

int main(){
Printoutput("***",47,"***");
}

//Write a main() function that tests the function with char, int, double, and string arguments.   


err , which part i wrong at?
You can't have every single variable be named "value"? That'd be my first guess.
Last edited on
You can't have every single variable be named "value"? That'd be my first guess.

I think the same thing.

When you say
cout << value << value << value << endl;

It won't know what value to cout?

(Comp sci student, but that seems like an issue from what I know)
Last edited on
I know very little about templates, but do you need to create an object in main, to then access the Printoutput function?
here is my question
1
2
3
4
5
6
7
8
9
10
/*Write a code Symbolic.cpp that contains 
a function template to display a value preceded 
and followed by n elements of a symbol x on a line.  Write a main() function
 that tests the function with char, int, double, and string arguments.  

The output could be, for example:

***47***
00039.25000
aaaaBobaaaa*/


and here is my code


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>

using namespace std;

template< class T , class U ,class X >
void Printoutput( T value ,U value2 , X value3  ){
	cout << value << value2 << value3 << endl;
}

int main(){

	Printoutput("***",47,"***");
	Printoutput("000",39.25,"000");
	Printoutput("aaaa","Bob","aaaa");

	system( "pause" );//system pause
	return 0;//Exit program
}


but from here . i know that my code didn't fullfill the question for
contains a function template to display a value preceded and followed by n elements of a symbol x on a line. . Not really get understand about the question. can someone help? thanks
Last edited on
contains a function template to display a value preceded and followed by n elements of a symbol x on a line.
It'd look like this:
1
2
3
4
5
6
7
template< class T >
void Printoutput( T value , char symbol_before_and_after_value, int how_often_the_symbol_is_repeated){
	// insert the first loop for outputting the symbol here
	cout << value;
	// insert the second loop for outputting the symbol here
	cout << endl;
}


The call would look like this:

Printoutput(47,'*', 3);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
template< class T , class U ,class X >
void Printoutput( T value ,U elements , X timesRepeated  ){
	for( int i = 0 ; i < timesRepeated ; i++ )
		cout << elements ;
	cout << value;
	for( int i = 0 ; i < timesRepeated ; i++ )
		cout << elements;
	cout << endl;
}

int main(){

	Printoutput(47,'*',3);
	Printoutput(39.25,0,3);
	Printoutput("Bob",'a',4);

	system( "pause" );//system pause
	return 0;//Exit program
}


am i right?
after observe it , templates quite usefull!
Topic archived. No new replies allowed.