How to make function

This is my code, and I want to make a function to replace the Underline text on my program...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>
#include <string.h>

int main()
{
	int counter = 0;
	char *diamond[13] = 
	{
		"2 diamond", "3 diamond", "4 diamond", "5 diamond", "6 diamond", "7 diamond", "8 diamond",
		"9 diamond", "10 diamond", "Jack diamond", "Queen diamond", "King diamond", "Ace diamond"
	};
	while(++counter <= sizeof(diamond)/4)
	puts(diamond[counter - 1]);

	while(getchar() != '\n');
	return 0;
}

Please give me a function like this
1
2
3
4
5
6
7
void printloop(..............)
{

     while(++counter <= sizeof(diamond)/4)
	puts(diamond[counter - 1]);

}

Thank you...^^
1
2
3
4
template <class T, int N>
void print(T (&array)[N], const char *delim){
	std::copy( array, array+N, ostream_iterator<T> (std::cout, delim) );
}
Don't forget to include the proper headers.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdio.h>
#include <string.h>

void myfunction()
{
     while(++counter <= sizeof(diamond)/4)
     puts(diamond[counter - 1]);
}

int main()
{
	int counter = 0;
	char *diamond[13] = 
	{
		"2 diamond", "3 diamond", "4 diamond", "5 diamond", "6 diamond", "7 diamond", "8 diamond",
		"9 diamond", "10 diamond", "Jack diamond", "Queen diamond", "King diamond", "Ace diamond"
	};
         
        myfunction(); //calls the function containing your underlined code

	while(getchar() != '\n');
	return 0;
}
Scope problem.
Your code is bad ProgrammingNoob. diamond is not part of myfunction's scope.
Somehow, I think ProgrammingNoob knows.

-Albatross
hmmm... I am a beginner on iostream C++ syntax
could you give me a normal function C ?
Last edited on
If you're trying to learn c++ then you really should wean yourself off of using char* to represent strings, #include<string> and use an std::string, and this would include stopping using functions like puts, and instead using std::cout << "Boo!"

If on the other hand you want a C solution, I'm not your man I'm afraid
Last edited on
Topic archived. No new replies allowed.