Create 3 functions that print initials using asterisk.

I'm writing a program that is supposed to print out the 3 letters of my initials using asterisks. My problem is I keep getting the error message, "a function-definition is not allowed here before "(" token" at every curly bracket. I'm literally following the code printed in my text so I'm having a tough time figuring out what I'm doing wrong. Any help would be greatly appreciated.

Ben

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
    // program will print "BXR" in block letters using asteriks
  //Fuctions used . . .
void printB(); //the following does not return a int to the main function
void printX();
void printR();

int main()
{
	printB(); // Print a B onto the screen
	printX(); // Print an X onto the screen
	printR(); // Print an R onto the screen

	system ("pause");
	return 0;


void printB()
{
	cout<<" *******\n"; //the block "B" asterik
	cout<<" *******\n";
	cout<<" **   **\n";
    cout<<" **   **\n";	
	cout<<" *******\n";
	cout<<" *******\n";
	cout<<" **   **\n";
    cout<<" **   **\n";	
	cout<<" *******\n";
	cout<<" *******\n";
	
	
	
} //end printB()
void printX()
{
	cout<<"***   ***\n"; //the block "X" asterik
    cout<<"***   ***\n";
	cout<<" *** ***\n";
	cout<<"   ***\n";
	cout<<" *** ***\n";
    cout<<"***   ***\n";
    cout<<"***   ***\n"; 
} //end print X

void printR()
{
	cout<<" *******\n"; //the block "R" asterik
	cout<<" *******\n";
	cout<<" **   **\n";
    cout<<" **   **\n";	
	cout<<" *******\n";
	cout<<" *******\n";
	cout<<" **   **\n";
    cout<<" **   **\n";	
	cout<<" **    **\n";
	cout<<" **     **\n";
} //end print R


    //Pause to read output 
   system ("pause"); 

    //Indicate to OS successful termination of program 
    return 0; 
}   //End main
Last edited on
Line 17,33,44: You can't nest function definitions. Move these functions outside main().
Thanks for the help AbstractionAnon. I removed that bit of code but now get different error messages.

"undefined reference to `printB()'

undefined reference to `printX()'

undefined reference to `printR()'

ld returned 1 exit status"


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
   // program will print "BXR" in block letters using asteriks
  //Fuctions used . . .
void printB(); //the following does not return a int to the main function
void printX();
void printR();

int main()
{
printB(); // Print a B onto the screen
printX(); // Print an X onto the screen
printR(); // Print an R onto the screen

	system ("pause");
	return 0;


printB();
{
	cout<<" *******\n"; //the block "B" asterisk
	cout<<" *******\n";
	cout<<" **   **\n";
    cout<<" **   **\n";	
	cout<<" *******\n";
	cout<<" *******\n";
	cout<<" **   **\n";
    cout<<" **   **\n";	
	cout<<" *******\n";
	cout<<" *******\n/n";
	
	
	
} //end printB()
printX();
{
	cout<<"***   ***\n"; //the block "X" asterisk
    cout<<"***   ***\n";
	cout<<" *** ***\n";
	cout<<"   ***\n";
	cout<<" *** ***\n";
    cout<<"***   ***\n";
    cout<<"***   ***\n/n"; 
} //end printX

printR();
{
	cout<<" *******\n"; //the block "R" asterisk
	cout<<" *******\n";
	cout<<" **   **\n";
    cout<<" **   **\n";	
	cout<<" *******\n";
	cout<<" *******\n";
	cout<<" **   **\n";
    cout<<" **   **\n";	
	cout<<" **    **\n";
	cout<<" **     **\n/n";
} //end printR


    //Pause to read output 
   system ("pause"); 

    //Indicate to OS successful termination of program 
    return 0; 
}   //End main
Last edited on
Lines 17,33,44: It doesn't look like you moved these functions outside main() at all.
They're still nested.

Lines 17,33,44: The function prototypes for these functions are declared void.
You've omitted the type on the function definitions. They must agree. Your compiler should have warned you about this. However, it may not have since these functions are out of place. For example, Line 17 is interpreted as a function call, not a function definition. Lines 18-32 are simply interpreted as a block of main(). Same with your other two functions.

Lines 17,33,44: Should not have ; after the function header once you move them.

Lines 58-63 are extraneous and should be removed.

Lines 28,41,55: The second newline has the wrong escape character.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <cstdlib>
#include <iostream>
using namespace std;

// program will print "BXR" in block letters using asteriks
//Fuctions used . . .
void printB(); //the following does not return a int to the main function
void printX();
void printR();

int main()
{
    printB(); // Print a B onto the screen
    printX(); // Print an X onto the screen
    printR(); // Print an R onto the screen

    system("pause");
    return 0;
}   //End main

void printB()
{
    cout << " *******\n"; //the block "B" asterisk
    cout << " *******\n";
    cout << " **   **\n";
    cout << " **   **\n";
    cout << " *******\n";
    cout << " *******\n";
    cout << " **   **\n";
    cout << " **   **\n";
    cout << " *******\n";
    cout << " *******\n/n";
} //end printB()

void printX()
{
    cout << "***   ***\n"; //the block "X" asterisk
    cout << "***   ***\n";
    cout << " *** ***\n";
    cout << "   ***\n";
    cout << " *** ***\n";
    cout << "***   ***\n";
    cout << "***   ***\n/n";
} //end printX

void printR()
{
    cout << " *******\n"; //the block "R" asterisk
    cout << " *******\n";
    cout << " **   **\n";
    cout << " **   **\n";
    cout << " *******\n";
    cout << " *******\n";
    cout << " **   **\n";
    cout << " **   **\n";
    cout << " **    **\n";
    cout << " **     **\n/n";
} //end printR
 *******
 *******
 **   **
 **   **
 *******
 *******
 **   **
 **   **
 *******
 *******
/n***   ***
***   ***
 *** ***
   ***
 *** ***
***   ***
***   ***
/n *******
 *******
 **   **
 **   **
 *******
 *******
 **   **
 **   **
 **    **
 **     **
/nPress any key to continue . . .


For extra credit, try printing the letters beside each other.


Last edited on
Thanks again AbstractionAnon. I was a little tired and out of it earlier. I took a little nap and now my brain is functioning semi-normal again. lol I've edited the program and think I have it right now.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// program will print "BXR" in block letters using asteriks
//Fuctions used . . .
void printB() //the following does not return a int to the main function
{
	
	cout<<" *******\n"; //the block "B" asterisk
	cout<<" *******\n";
	cout<<" **     **\n";
        cout<<" **     **\n";	
	cout<<" *******\n";
	cout<<" *******\n";
	cout<<" **     **\n";
        cout<<" **     **\n";	
	cout<<" *******\n";
	cout<<" *******\n\n";
}
void printX() //the following does not return a int to the main function
{

    cout<<"***   ***\n"; //the block "X" asterisk
    cout<<"***   ***\n";
    cout<<" *** ***\n";
    cout<<"    ***\n";
    cout<<" *** ***\n";
    cout<<"***   ***\n";
    cout<<"***   ***\n\n"; 
}
void printR() //the following does not return a int to the main function
{

        cout<<" *******\n"; //the block "R" asterisk
	cout<<" *******\n";
	cout<<" **     **\n";
        cout<<" **     **\n";	
	cout<<" *******\n";
	cout<<" *******\n";
	cout<<" **      **\n";
        cout<<" **      **\n";	
	cout<<" **       **\n";
	cout<<" **        **\n\n";
}
int main()
{
printB(); //end printB()

printX(); //end printX

printR(); //end printR


}
Topic archived. No new replies allowed.