446,388 Members | 1,823 Online
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

 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 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
10 Replies

 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

 Expert Mod 2.5K+ P: 4,677 sonic, The same way it does in the first part. Expand|Select|Wrap|Line Numbers for (int Row = 0; Row <= HalfValue; Row++) {     for (int Star = 0; Star < Row; Star++) {         cout << "+";     }     cout << "\n"; }   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

 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   // This program displays a certain pattern of "+'s" depending on user's input.   #include    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       //Loop begins pattern of +'s     for (int Row = 0; Row <= HalfValue; Row++)     {         for (int Star = 0; Star < Row; Star++)             cout << "+";             cout << "\n";             }     //Loop completes pattern of +'s     for (int Row = HalfValue; Row != 0; Row--)     {             for (int Star = 0; Star < Row; Star++)             cout << "+";     }       cout << "\n";     }           return 0; }     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

 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

 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   // This program displays a certain pattern of "+'s" depending on user's input.   #include    using namespace std;   int main ()   {       int UserSel = 0;     int HalfValue = 0;         do     {         cout << "How many rows do you want in the pattern (even numbers only: 2-14)? ";         cin >> UserSel;           HalfValue = UserSel/2; // Value is where loop begins to decrement           //Loop begins pattern of +'s         for (int Row = 0; Row <= HalfValue; Row++)         {             for (int Star = 0; Star < Row; Star++)                 cout << "+";                 }         //Loop completes pattern of +'s         for (int Row = HalfValue; Row != 0; Row--)         {                 for (int Star = 0; Star < Row; Star++)                 cout << "+";         }     }         while (UserSel > 1 && UserSel <= 14 && (UserSel%2) == 0); //input validation           cout << "Invalid number of rows. Please try again.\n\n";       return 0; }     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

 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

 Expert Mod 2.5K+ P: 4,677 Sonic- I got it to work with this code: Expand|Select|Wrap|Line Numbers #include  using namespace std;   int main() {      int userSel = 0;    int halfValue = 0;    while ( (userSel < 2) || (userSel > 14) || ((userSel % 2) != 0) )    {       cout << "Enter the number of rows you would like in the pattern.\n";       cout << "[Input must be greater than 2, less than 14, and even.]\n";       cin >> userSel;    }      halfValue = userSel / 2;    for (int row = 0; row <= halfValue; row++) {       for (int star = 0; star < row; star++) {          cout << "+";       }       cout << endl;    }    for (int row2 = halfValue; row2 >= 0; row2--) {       for (int star2 = 0; star2 < row2; star2++) {          cout << "+";       }       cout << endl;    }      return 0; }   Nov 8 '06 #8

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

 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 #include  #include  using namespace std; void printStars(int count, int num);  int main() {     cout << "Enter the number of lines to print: ";     int x;     cin >> x;     int ind = 1;       printStars(ind, x);       system("PAUSE");     return 0; } void printStars(int count, int num) {      for (int i = 0; i < count; i++) cout << "*";      cout << endl;      if (count < num) printStars(count + 1, num);      for (int i = 0; i < count; i++) cout << "*";      cout << endl; }   /* Sample Enter the number of lines to print: 5 * ** *** **** ***** ***** **** *** ** * Press any key to continue . . . */ Nov 10 '06 #10

 100+ P: 357 hey!!! play with this one! ;) Expand|Select|Wrap|Line Numbers #include  main() {     int i, j;     for (i=1; i<=5; ++i){         for (j=0; j=1; --i){         for (j=0; j