I'm taking a C++ class with a not-so in depth professor. He's given us this assignment...
Quote:
Write a C++ program which will ask the user to enter an integer from 1 to 15 and displays a number pyramid (implement input validation). For example, if the input is 4 the output will be as follows:
[should be centered]
1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
The user input represents the total number of lines in the pyramid. Each number occupies three spaces. You must use nested loops to implement your program (Hint: Each line has three parts). You must prompt the user for the number of lines in the main() and then call a function called printPiramid() from the main that has the following prototype: void printPyramid(int numLines);
You should follow good programming practices (Program header, meaningful variable names, and adequate comments, proper indentation) while writing your program. If you do not implement the pyramid drawing function printPyramid(), you will not get credit for this assignment.

...and at first I was thinking I used the pyramid function to print the actual pyramid, but then when I was compiling it it said the function type was void, which I guess I should've realized on my own. Anyway, I'm not really sure what I should be doing from here.

Code:
#include <iostream>
void printPyramid(int numLines);
int main (){
   std::cout << "Please enter an integer between 1 and 15." << std::endl;
   int numLines;
   std::cin >> numLines;
   std::cout << std::endl;
   
   if (numLines < 1 || numLines > 15){
      std::cout << "Incorrect value. Integer must be between 1 and 15." << std::endl;
      std::cin >> numLines;
      std::cout << std::endl;
   } else {
      void printPyramid(numLines);
   }
   return 0;
}

void printPyramid(int numLines){
      for (int i = 1; i <= numLines; i++){
         for (int j = 1; j <= i; j++) {
            std::cout << " " << j << " ";
         }
         std::cout << std::endl;
      }
}


I mostly need to know what to do with this void function, and then how to also print the numbers out in ascending order on the left of the center column of 1's.
Without looking too deeply at your code to see any other problems, I can see you're calling printPyramid wrong--you only want the "void" for declaration. So, turn this:

Code:
   } else {
      void printPyramid(numLines);
   }

Into this:

Code:
   } else {
      printPyramid(numLines);
   }


Hope that helps. If not, feel free to ask more questions.
Oh, ok! Yeah, that made the program actually run, but it won't print the pyramid out.
emilyfay wrote:
Oh, ok! Yeah, that made the program actually run, but it won't print the pyramid out.
Does it print anything at all? Nothing, or an incorrect pyramid? Also, welcome to Cemetech, and please Introduce Yourself when you get a chance.
Just the pyramid won't print. And thanks, I will!
Ok, I ran it and got this:

Code:
[shaun@madigan programming]$ ./a.out
Please enter an integer between 1 and 15.
5

 1
 1  2
 1  2  3
 1  2  3  4
 1  2  3  4  5

So something is printing Smile As a hint I suggest thinking about adding another for loop.
Ok ok, I've got it working now except for this dealio.

Code:
Please enter an integer between 1 and 15.
3
 3  2  1
 3  2  1  2
 3  2  1  2  3

How can I make those extra digits disappear?
Here's just the printPyramid function.

Code:
void printPyramid(int numLines){
      for (int i = 1; i <= numLines; i++){
         for (int k = numLines; k > 1; k--) {
            std::cout << " " << k << " ";
         }
         for (int j = 1; j <= i; j++) {
            std::cout << " " << j << " ";
         }
         std::cout << std::endl;
      }
}
Also not related to the actual pyramid, but your input validation is correct only once. I can input an incorrect number twice and the program will quit.
When you print the first set of numbers, in between the spaces there's something there that you don't need to print Wink
lafferjm wrote:
Also not related to the actual pyramid, but your input validation is correct only once. I can input an incorrect number twice and the program will quit.

Oh, thanks! I went ahead and fixed that.

chickendude wrote:
When you print the first set of numbers, in between the spaces there's something there that you don't need to print Wink

If you mean the k, I do need it so it prints out the numbers on the left?
Think carefully about the bounds on your inner k loop. Think about what you want to be printing in each k loop, and how that compares to the j loop immediately following it. Remember, the k loop should basically be a backwards version of the j loop, just not duplicating the "center" number.

Code:
for (int k = numLines; k > 1; k--) {
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement