Creating Shapes with functions

There's something wrong with the functions I beleive, the error it gives me is in the switch function where I put the DrawRactangle and DrawTriangle, it says "expected '(' for a function-style cadt or type construction. Not sure what to do.

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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
  #include <iostream>
using namespace std;


void DrawRectangle(int width,int height)
{
    cout << "Please enter the height of the rectangle: ";
    cin >> height;
    
    cout << endl;
    
    cout << "Please enter the width of the rectangle: ";
    cin >> width;
    
    cout << endl;
    
    while (height<=0||width<=0)
    {
        cout << "Please enter the height of the rectangle: ";
        cin >> height;
        
        cout << endl;
        
        cout << "Please enter the width of the rectangle: ";
        cin >> width;
        
        cout << endl;
    }
    
    for (int i=0; i < height;i++)
    {
        for(int j=0;j < width;j++)
        {
            cout << "*";
        }
        cout << endl;
    }
    
    
}

void DrawTriangle(int rows)
{
    cout << "Enter the number of rows for the triangle: ";
    cin >> rows;
    
    for ( int i=1; i<= rows; ++i)
    {
        for(int j = 1; j <=i; ++j)
        {
            cout << "* ";
        }
        cout << "\n";
    }
}


int main()
{
    int choice;
    char rerun;
    
    
    do{
        
    
    cout << "Choose which shape you want to print: \n";
    cout << "1. Rectangle \n";
    cout << "2. Triangle \n";
    
    cin >> choice;
    
    switch (choice)
    {
        case 1: //Rectangle
            DrawRectangle(int width, int height);
            
        case 2: //Triangle
            DrawTriangle(<#int rows#>);
            
    }
        
    cout << "Do you want to run the program again? (Y/N) : ";
        cin >> rerun;
    } while (rerun =='y' || rerun == 'Y');
    
    
    
    
    
    return 0;
    
}
Each of the functions DrawTriangle() and DrawRectangle() will prompt the user for input and get the required dimensions via cin. Therefore there is no need to pass any parameter to either function.

Declare the function like this (at line 5)
 
void DrawRectangle()

and call it like this (at line 76)
 
    DrawRectangle();


You will need to define inside the function the local variables width and height. Similarly with the DrawTriangle().

By the way, you will need a break; statement after the code for each case in the switch statement.
I agree with @Chervil. However, if you wanted to keep the ...(int width, int height), you could add & sign to make it a call-by-reference parameter. See here for more information on call-by-reference parameter: https://www.tutorialspoint.com/cplusplus/cpp_function_call_by_reference.htm

Also, like Chervil said, you'll need a break; statement after each case statement. So, it will look something like this:

1
2
3
4
5
6
7
8
9
10
switch (choice)
{
    case 1: //Rectangle
        DrawRectangle(width, height);
        break;
            
    case 2: //Triangle
        DrawTriangle(rows);
        break;    
}


Your whole code should look something like this:
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>
using namespace std;

void DrawRectangle(int& width, int& height)
{
    cout << "Please enter the height of the rectangle: ";
    cin >> height;
    
    cout << endl;
    
    cout << "Please enter the width of the rectangle: ";
    cin >> width;
    
    cout << endl;
    
    while (height<=0||width<=0)
    {
        cout << "Please enter the height of the rectangle: ";
        cin >> height;
        
        cout << endl;
        
        cout << "Please enter the width of the rectangle: ";
        cin >> width;
        
        cout << endl;
    }
    
    for (int i=0; i < height;i++)
    {
        for(int j=0;j < width;j++)
        {
            cout << "*";
        }
        cout << endl;
    }
    
    
}

void DrawTriangle(int& rows)
{
    cout << "Enter the number of rows for the triangle: ";
    cin >> rows;
    
    for ( int i=1; i<= rows; ++i)
    {
        for(int j = 1; j <=i; ++j)
        {
            cout << "* ";
        }
        cout << "\n";
    }
}


int main()
{
    int choice, width, height, rows;
    char rerun;
    
    do{
        
    
    cout << "Choose which shape you want to print: \n";
    cout << "1. Rectangle \n";
    cout << "2. Triangle \n";
    
    cin >> choice;
    
    switch (choice)
    {
        case 1: //Rectangle
            DrawRectangle(width, height);
            break;
            
        case 2: //Triangle
            DrawTriangle(rows);
            break;
    }
        
    cout << "Do you want to run the program again? (Y/N) : ";
    cin >> rerun;
    } while (rerun =='y' || rerun == 'Y');
    
    return 0;
    
}


-VX
Last edited on
Thank to both of you :)))))
No problem! :)
You could also add a good-bye statement before line 86, something like

 
cout << "Thank you for using this shape drawing application! " << endl;


so users know the program has ended. It's entirely optional, but I like to add stuff like that.

-VX
Last edited on
Topic archived. No new replies allowed.