By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,388 Members | 1,823 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,388 IT Pros & Developers. It's quick & easy.

Help with Looping Structure

sonic
P: 40
I have a programming assignment I've been working on for class. Basically the program creates a design of "+'s" based on user's input of how many rows he or she would like displayed. Here is a basic example of what the output may look like if someone requested there be 6 rows of design:

+
++
+++
+++
++
+

I can't get the decrementing loop, after it has hit mid section to take away the "+'s". Also, I must check for input validation. The number of rows the user has to choose from are 2-14, excluding odds. My input validation correctly singles out any numbers not in range but does not stop odd numbers from being entered. Here is my code. Can anyone help?

// This program displays a certain pattern of "+'s" depending on user's input.

#include <iostream>

using namespace std;

int main ()

{

int UserSel = 0;
int HalfValue = 0;

cout << "How many rows do you want in the pattern (even numbers only: 2-14)? ";
cin >> UserSel;

// Input Validation
if (UserSel < 2 || UserSel > 14 || UserSel%2 != 0)
cout << "\nInvalid number of rows. Please retry.\n\n";

else
{
HalfValue = UserSel/2; // Value is where loop begins to decrement
//Code begins pattern of +'s
for (int Row = 0; Row <= HalfValue; Row++)
{
for (int Star = 0; Star < Row; Star++)
{
cout << "+";
}
cout << "\n";
}
//Code brings +'s back left to complete pattern
for (int Row = HalfValue; Row != 0; Row--)
cout << "+";
}

cout << "\n";




return 0;
}
Nov 7 '06 #1
Share this Question
Share on Google+
10 Replies


sonic
P: 40
I'm not sure how to write the code after my comment // Code brings +'s back left to complete pattern. Thank you for any who might be able to help!
Nov 7 '06 #2

sicarie
Expert Mod 2.5K+
P: 4,677
sonic,

The same way it does in the first part.

Expand|Select|Wrap|Line Numbers
  1. for (int Row = 0; Row <= HalfValue; Row++) {
  2.     for (int Star = 0; Star < Row; Star++) {
  3.         cout << "+";
  4.     }
  5.     cout << "\n";
  6. }
  7.  
So you start out going from 0 to HalfValue, so you get half of the +'s going up.
Then you go from 0 to the number of each row, so you have that many +'s printing out on all of them.

What you want to do is go from HalfValue down to 0, so you get all the +'s going down (which is written for you), and then inside that go from 0 to the number of each row, so you have that many +'s printing out on all of them.

If you read that REALLY carefully, you'll see something about that statement ;)
Nov 7 '06 #3

sonic
P: 40
sicarie, thanks for your help. I think I've done what you explained but program still won't print out diminishing "+'s" to finish the pattern. What I thought would be an easy program has turned out mind-boggling me. Here's my new code:

Expand|Select|Wrap|Line Numbers
  1.  
  2. // This program displays a certain pattern of "+'s" depending on user's input.
  3.  
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. int main ()
  9.  
  10. {
  11.  
  12.     int UserSel = 0;
  13.     int HalfValue = 0;
  14.  
  15.     cout << "How many rows do you want in the pattern (even numbers only: 2-14)? ";
  16.     cin >> UserSel;
  17.  
  18.     // Input Validation
  19.     if (UserSel < 2 || UserSel > 14 || (UserSel%2) != 0)
  20.         cout << "\nInvalid number of rows. Please retry.\n\n";
  21.  
  22.     else
  23.     {
  24.     HalfValue = UserSel/2; // Value is where loop begins to decrement
  25.  
  26.     //Loop begins pattern of +'s
  27.     for (int Row = 0; Row <= HalfValue; Row++)
  28.     {
  29.         for (int Star = 0; Star < Row; Star++)
  30.             cout << "+";
  31.             cout << "\n";        
  32.     }
  33.     //Loop completes pattern of +'s
  34.     for (int Row = HalfValue; Row != 0; Row--)
  35.     {    
  36.         for (int Star = 0; Star < Row; Star++)
  37.             cout << "+";
  38.     }
  39.  
  40.     cout << "\n";
  41.     }
  42.  
  43.  
  44.  
  45.     return 0;
  46. }
  47.  
  48.  
Still having problem with my error checking. Input validation will not weed out negative values entered. Is that the proper way of testing? Thanks for help.
Nov 7 '06 #4

sicarie
Expert Mod 2.5K+
P: 4,677
sonic,

You're almost there!

The input:
I would try putting the 'cout >> "enter a number...";' and the cin immediately after, inside a while loop. That way it will keep looping until you get the input you want:

do {

// your program
}
while ( /* the condition */ )

The other thing was your || - your condition. By using || you are saying "If you meet any one of these, you can go ahead" when you really want to constrict it to ALL of those conditions. (I think you want the && there)

The only other thing I could see (within the first glance) was the cout << "\n"; inside your second for loop (when you're still incrementing the rows and stars). I think you want to move that back outside one loop, like you had it at first.
Nov 7 '06 #5

sonic
P: 40
I completed the suggestions you made in your last post. I'm still having problems with the program not finishing the design. Also, my input validation statement still doesn't error catch odd numbers being entered by user.

Supposing the user enters a 8, here is what the output should look like:

+
++
+++
++++
++++
+++
++
+

This is the result of my program's output (assuming user enters 8):

+
++
+++
++++
++++
Press any key to continue. . .

Somehow my program stops at it first iteration through second set of for loops.
Sorry, we have been so long on this one topic. Here is my code after making the changes you recommended in earlier posts:

Expand|Select|Wrap|Line Numbers
  1.  
  2. // This program displays a certain pattern of "+'s" depending on user's input.
  3.  
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. int main ()
  9.  
  10. {
  11.  
  12.     int UserSel = 0;
  13.     int HalfValue = 0;
  14.  
  15.  
  16.     do
  17.     {
  18.         cout << "How many rows do you want in the pattern (even numbers only: 2-14)? ";
  19.         cin >> UserSel;
  20.  
  21.         HalfValue = UserSel/2; // Value is where loop begins to decrement
  22.  
  23.         //Loop begins pattern of +'s
  24.         for (int Row = 0; Row <= HalfValue; Row++)
  25.         {
  26.             for (int Star = 0; Star < Row; Star++)
  27.                 cout << "+";        
  28.         }
  29.         //Loop completes pattern of +'s
  30.         for (int Row = HalfValue; Row != 0; Row--)
  31.         {    
  32.             for (int Star = 0; Star < Row; Star++)
  33.                 cout << "+";
  34.         }
  35.     }
  36.         while (UserSel > 1 && UserSel <= 14 && (UserSel%2) == 0); //input validation
  37.  
  38.  
  39.  
  40.     cout << "Invalid number of rows. Please try again.\n\n";
  41.  
  42.     return 0;
  43. }
  44.  
  45.  
Thank you for your continued help. Have already had to turn the program in, but would like to get it working so I can understand it for myself.
Nov 8 '06 #6

sicarie
Expert Mod 2.5K+
P: 4,677
Sonic-

I got it to work on my other box. I can't get it to work right now, I don't know why, I'll put up what I came up with tomorrow morning. Sorry I can't get it right now, there were $15 table-tappers tonight...
Nov 8 '06 #7

sicarie
Expert Mod 2.5K+
P: 4,677
Sonic-

I got it to work with this code:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5.  
  6.    int userSel = 0;
  7.    int halfValue = 0;
  8.    while ( (userSel < 2) || (userSel > 14) || ((userSel % 2) != 0) )
  9.    {
  10.       cout << "Enter the number of rows you would like in the pattern.\n";
  11.       cout << "[Input must be greater than 2, less than 14, and even.]\n";
  12.       cin >> userSel;
  13.    }
  14.  
  15.    halfValue = userSel / 2;
  16.    for (int row = 0; row <= halfValue; row++) {
  17.       for (int star = 0; star < row; star++) {
  18.          cout << "+";
  19.       }
  20.       cout << endl;
  21.    }
  22.    for (int row2 = halfValue; row2 >= 0; row2--) {
  23.       for (int star2 = 0; star2 < row2; star2++) {
  24.          cout << "+";
  25.       }
  26.       cout << endl;
  27.    }
  28.  
  29.    return 0;
  30. }
  31.  
Nov 8 '06 #8

sonic
P: 40
Thanks sicarie for your help earlier this week on my program! The posts were very helpful.
Nov 10 '06 #9

Ganon11
Expert 2.5K+
P: 3,652
This would have been so much easier using recursion...:P

If you're interested, try this:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. void printStars(int count, int num); 
  5. int main() {
  6.     cout << "Enter the number of lines to print: ";
  7.     int x;
  8.     cin >> x;
  9.     int ind = 1;
  10.  
  11.     printStars(ind, x);
  12.  
  13.     system("PAUSE");
  14.     return 0;
  15. }
  16. void printStars(int count, int num) {
  17.      for (int i = 0; i < count; i++) cout << "*";
  18.      cout << endl;
  19.      if (count < num) printStars(count + 1, num);
  20.      for (int i = 0; i < count; i++) cout << "*";
  21.      cout << endl;
  22. }
  23.  
  24. /* Sample
  25. Enter the number of lines to print: 5
  26. *
  27. **
  28. ***
  29. ****
  30. *****
  31. *****
  32. ****
  33. ***
  34. **
  35. *
  36. Press any key to continue . . .
  37. */
Nov 10 '06 #10

momotaro
100+
P: 357
hey!!!
play with this one! ;)
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. main()
  3. {
  4.     int i, j;
  5.     for (i=1; i<=5; ++i){
  6.         for (j=0; j<i; ++j){
  7.             printf("*");
  8.         }
  9.         printf("\n");
  10.     }
  11.        for (i=4; i>=1; --i){
  12.         for (j=0; j<i; ++j){
  13.             printf("*");
  14.         }
  15.         printf("\n");
  16.     }
  17. }
Nov 12 '06 #11

Post your reply

Sign in to post your reply or Sign up for a free account.