### Void function problem, confusion, etc.

I'm trying to complete a void function problem, but I'm having difficulty.

Here's the problem: I have two variables, pattern_type and pattern_size. pattern_type can be anything from 1 - 3 and pattern_size can be anything from 1 - 10. That gives me 30 possible outcomes but there should only be 3 loops, that are nested.

I've begun with something like this to output the \$ in a specific pattern outlined by my professor's guidelines:
 ``1234567891011121314151617181920212223242526272829303132333435`` ``````//Begin function definition void PrintPattern(int pattern_type, int pattern_size) { //Begin function body //pattern_type 1 for (int i=0; i < pattern_size; i++) { for int (j=0; j < pattern_type; j++) if (i==j) cout << "\$"; cout << endl; } //pattern_type 2 for (int i=0; i < pattern_size; i++) { for int (j=0; j < pattern_type; j++) if (i==j) cout << "\$"; cout << endl; } //pattern_type 3 for (int i=0; i < pattern_size; i++) { for int (j=0; j < pattern_type; j++) if (i==j) cout << "\$"; cout << endl; } //End function body } //End function definition ``````

Now I know this is far from complete, it's logically incorrect. The output should be:
 ``123456789101112131415161718192021222324252627282930313233`` ``````pattern_type=1: I:0 | J:0 I:1 | J:1 I:2 | J:2 I:3 | J:3 OR \$ \$ \$ \$ and so on until they reach a maximum pattern_size=10 for pattern_type=2: \$ \$\$ \$\$\$ \$\$\$\$ \$\$\$\$\$ \$\$\$\$\$\$ and finally, for pattern_type=3: \$\$\$\$\$\$\$\$\$ \$\$\$\$\$\$\$\$ \$\$\$\$\$\$\$ \$\$\$\$\$\$ \$\$\$\$\$ \$\$\$\$ \$\$\$ \$\$ \$``````

I've ALWAYS had problems with structuring the logical output in my programs, and this one is especially NOT an exception to my flaw. I understand the loop I need for the outside part of my program, which handles the basic output and input required to call the function "PrintPattern(int pattern_type, int pattern_size)" and so on, but the structure of the function is confusing me. I think the best approach would be to do something like:

 ``12345678910111213141516171819202122232425262728293031323334`` `````` void PrintPattern(int pattern_type, int pattern_size) { //if pattern_type is not valid, ie: 1-3 cout << "Pattern type isn't valid!\n"; return false; //if pattern_size is not valid, ie: 1-10 cout << "Pattern size isn't valid!\n"; return false; switch (pattern_type) { case 1: for (int i=0; i < pattern_size; i++) { for int (j=0; j < pattern_type; j++) if (i==j) cout << "\t" << "\$"; cout << endl; } case 2: for (int i=0; i < pattern_size; i++) { for int (j=0; j < pattern_type; j++) if (i==j) cout << "\$"; cout << endl; } case 3: //I have no clue default: } ``````

Can anyone give me some guidance? I'm lost.

Thank you!

a) you forgot break at the end of cases
b) `for ( int ( j=0; j < pattern_type pattern_size; j++)`
c) just print i spaces, and (pattern_size - i) '\$' after
Pattern type 1: `if (i==j)`
Pattern type 2: `if (j <= i)`
Pattern type 3: `if (j >= i)`
Here's what I have so far, with errors:

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677`` ``````#include 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'; //ans always y so continues to loop unless n while (ans=='y' || ans == 'Y') { cout << "Please enter a pattern type.\n"; cin >> pattern_type; cout << "Please enter a pattern size.\n"; cin >> pattern_size; cout << "Please enter y or Y to continue. Enter any other character to exi the program.\n"; cin >> ans; } return 0; } void PrintPattern(int pattern_type, int pattern_size) { //if pattern_type is not valid, ie: 1-3 if (pattern_type > 3 || pattern_type < 1) { cout << "Pattern type isn't valid!\n"; return false; } //if pattern_size is not valid, ie: 1-10 if (pattern_size > 10 || pattern_size < 1) { cout << "Pattern size isn't valid!\n"; return false; } 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" << "\$"; cout << endl; } break; case 2: for (int i=0; i < pattern_size; i++) { for int (j=0; j < pattern_size; j++) if (j <= i) 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 << "\$"; cout << endl; } break; }``````

Errors:

 ``12345678910`` ``````08_Lab_Test2.cpp: In function ‘void PrintPattern(int, int)’: 08_Lab_Test2.cpp:39:9: error: return-statement with a value, in function returning 'void' [-fpermissive] 08_Lab_Test2.cpp:45:9: error: return-statement with a value, in function returning 'void' [-fpermissive] 08_Lab_Test2.cpp:53:7: error: expected ‘(’ before ‘int’ 08_Lab_Test2.cpp:53:12: error: ‘j’ was not declared in this scope 08_Lab_Test2.cpp:62:7: error: expected ‘(’ before ‘int’ 08_Lab_Test2.cpp:62:12: error: ‘j’ was not declared in this scope 08_Lab_Test2.cpp:71:7: error: expected ‘(’ before ‘int’ 08_Lab_Test2.cpp:71:12: error: ‘j’ was not declared in this scope 08_Lab_Test2.cpp:77:1: error: expected ‘}’ at end of input``````

Ugh...
Look at the line mentioned in error, then look two posts above
Last edited on
 a) you forgot break at the end of cases b) for ( int ( j=0; j < pattern_type pattern_size; j++) c) just print i spaces, and (pattern_size - i) '\$' after

I completed A and B, though I don't think I understand step C.

The new line of errors is:

 ``123456`` ``````08_Lab_Test2.cpp: In function ‘void PrintPattern(int, int)’: 08_Lab_Test2.cpp:39:9: error: return-statement with a value, in function returning 'void' [-fpermissive] 08_Lab_Test2.cpp:45:9: error: return-statement with a value, in function returning 'void' [-fpermissive] 08_Lab_Test2.cpp:53:13: error: ‘j’ was not declared in this scope 08_Lab_Test2.cpp:62:13: error: ‘j’ was not declared in this scope 08_Lab_Test2.cpp:71:13: error: ‘j’ was not declared in this scope``````

I'm a bit lost as to why j was not declared. I know j is basically the pattern_type, right? And for errors on 39 and 45, I'm not completely sure what's going on. I removed the return, however, that doesn't solve my problem to begin the while loop once the error text is output.

My new function:

 ``123456789101112131415161718192021222324252627282930`` ``````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" << "\$"; cout << endl; } break; case 2: for (int i=0; i < pattern_size; i++) { for (int (j=0; j < pattern_size; j++) if (j <= i) 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 << "\$"; cout << endl; } break; }``````

 I'm a bit lost as to why j was not declared
`for (int (j=0;`
Because of excess parenthesis
Two lines above you did everything right. Why after writing two lines you suddenly forgot correct syntax for for-loops?
Last edited on
Ah, that was simple enough. Well I'm glad the program now compiles! Thanks for your help. Now I feel there are many other problems ahead of me in order to get this functioning properly.

A)Doesn't output anything
B)Doesn't handle error's (ie: pattern_type > 3 || pattern_type < 1 OR pattern_size > 10 || pattern_size < 1)

Hmm... Luckily I have until midnight to figure this out. I do appreciate the pointers you've given me thus far.
 Two lines above you did everything right. Why after writing two lines you suddenly forgot correct syntax for for-loops?

Beginner mistakes, that's all. Keep in mind, I've only had about 6-8 weeks of study in ANY programming language at an academic level. It's bound to happen, especially for me.
 pattern_type > 3 || pattern_type < 1

Make a default case in your switch:
 ``123456`` ``````case 3: //... break; default: std::cout << "Pattern type should be in [1; 3]"; }``````

 pattern_size > 10 || pattern_size < 1

make condition in the begiinning of function:
 ``1234`` ``````if(pattern_size > 10 || pattern_size < 1) { std::cout << "pattern size should be in [1; 10]" return; //Stops function prematurely }``````
Hmm, not working. I can still enter anything outside of the parameters specified. I'll put some more work into this project later on today. Once again, thanks for your help!
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879`` ``````#include 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'; //ans always y so continues to loop unless n while (ans=='y' || ans == 'Y') { cout << "Please enter a pattern type.\n"; cin >> pattern_type; cout << "Please enter a pattern size.\n"; cin >> 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" << "\$"; cout << endl; } break; case 2: for (int i=0; i < pattern_size; i++) { for (int j=0; j < pattern_size; j++) if (j <= i) 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 << "\$"; cout << endl; } break; default: cout << "Pattern type should be beween (1-3).\n"; } }``````

The only thing that "really" works is the while loop, which is good, but the void function seems to not really work as intended. I'm sure it's something simple, I'm still picking through it to see if it's something simple.
You are never calling your PrintPattern function. Place function call in line 24.
Ah, so something along the lines of

 ``1234567891011121314`` `````` while (ans=='y' || ans == 'Y') { cout << "Please enter a 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; } ``````
Yes
Looks good, but my function -- not so much. I'm glad I've gotten this far though! It doesn't seem to properly function if you enter an incorrect value.

I believe the incorrect output lied within your tip earlier:

 c) just print i spaces, and (pattern_size - i) '\$' after
after lines
 ``12`` ``````if (j >= i) cout << "\$";``````

 ``12`` ``````else cout << ' ';``````
case 1:
 ``1234`` ``````if (i == j) cout << '\$'; else cout << ' ';``````
 ``12345`` ``````case 1: if (i == j) cout << '\$'; else cout << ' ';``````

Thanks, I figured that one out. :) I feel like I should play Sudoku when learning programming. Programming is very mind challenging for me, I need to exercise my intellect.
Topic archived. No new replies allowed.