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.
#include <iostream>
usingnamespace 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?
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.
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++ :/