help with void printpattern

Hello, I've been having problems with my program. My goal is to have the user enter a pattern type and pattern size and then it will print dollar signs using a nested loop with one outer loop and one inner loop. The pattern type can be between 1-3 and the pattern size can be between 1 and 10.
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
#include <iostream>

using namespace std;

void printpattern(int pattern_type, int pattern_size);//function call

int main()
{
        int pattern_type;
        int pattern_size;
        char ans;

        ans=='y';//this makes it loop until the uses enters a different character

        while (ans=='y' || ans == 'Y')
        {
                cout << "Please enter pattern type.\n";
                cin >> pattern_type;
                cout << "Please enter a pattern size.\n";
                cin >> pattern_size;

                printpattern(pattern_type, pattern_size);

 cout << "Please enter Y or y to continue. Enter any other character to exit the program.\n";
                cin >> ans;
        }

return 0;
}


void printpattern(int pattern_type, int pattern_size)
{

        if(pattern_size > 10 || pattern_size < 1)
        {
                cout << "Pattern size should be between (1-10)\n";
                return;
        }

        switch (pattern_type)
        {

        case 1:
                for (int i=0; i < pattern_size; i++)
                {
                        for (int j=0; j < pattern_size; j++)
                                if (i==j)
                                        cout << "\t" << "$";
                                else
                                        cout << ' ';
                        cout << endl;

        case 2:
                for (int i=0; i < pattern_size; i++)
                {
                        for (int j=0; j < pattern_size; j++)
                                if (j <= i)
                                        cout << "$";
                        else
                                cout << ' ';

                        cout << endl;
                }
                break;
         case 3:
                for (int i=0; i < pattern_size; i++)
                {
                        for (int j=0; j < pattern_size; j++)
                                if (j >= i)
                                        cout << "$";
                                else
                                        cout << ' ';
                        cout << endl;
                }
                break;

        default:
                cout << "Pattern type should be between (1-3).\n";
        }
}

For some reason, this code compiles, but won't run properly or even at all. Is there anything that I am doing wrong?
Last edited on
please edit your post and put all of your code between [code]code tags[/code].

Could you also provide a input, and what is currently being outputed vs what you want it to output.
Please edit your post and make sure your code is [code]between code tags[/code] so that it has syntax highlighting and line numbers, as well as proper indentation.

Your code does not compile:
http://ideone.com/O4twZ7
Hi

I would like it to output the following:

Please enter pattern type.
1
Please enter pattern size.
4
$
$
$
$

when I did the program I received the following errors:

prog.cpp: In function 'void printpattern(int, int)':
prog.cpp:55:6: error: jump to case label [-fpermissive]
case 2:
^
prog.cpp:46:10: error: crosses initialization of 'int i'
for (int i=0; i < pattern_size; i++)
^
prog.cpp:67:6: error: jump to case label [-fpermissive]
case 3:
^
prog.cpp:46:10: error: crosses initialization of 'int i'
for (int i=0; i < pattern_size; i++)
^
prog.cpp:79:1: error: jump to case label [-fpermissive]
default:
^
prog.cpp:46:10: error: crosses initialization of 'int i'
for (int i=0; i < pattern_size; i++)
^
I wish I knew how to go about fixing it, but i'm just starting out in C++ :/
You forgot to close your for loop on line 53. After fixing that, it compiles: http://ideone.com/t9FeOl
Last edited on
Thanks LB! I got it to work!
Topic archived. No new replies allowed.