asterisk pyramid problem

I am having trouble with one of the asterisk problems
what I need help with is the output of invalid number if you input a negative number. I feel the solution is easy I just can't figure it out. I think it has to do with the looping system i am using. a while loop :/

Write a program that will prompt for and read a number of rows, nrows (a positive integer) and then generate a pattern of nrows rows of asterisks as shown in the example run, below. Note: The first row contains nrows-1 blanks followed by 1 asterisk, the second row contains nrows-2 blanks followed by 3 asterisks, etc., until the last row contains no blanks followed by 2*nrows – 1 asterisks. Include a do loop to validate the user input of nrows – if it is not positive, the program should generate an error message and force the user to re-enter the value until it is positive.
Here is an example of what output should look like from running your program (user input is shown in bold):
Enter number of rows (>0): -4
Invalid entry – try again!
Enter a positive integer: 4
---*
--***
-*****
*******
This is what I have so far

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
while ( true )
{
const char asterisk = '*';

std::cout << "Enter a non-negative number: ";

size_t n = 0;
std::cin >> n;

if ( n<=0 ) break;

std::cout << std::endl;

for ( size_t i = 0; i < n; i++ )
{
std::cout << std::setw( n - i ) << std::setfill( ' ' ) << asterisk;
std::cout << std::setw( 2 * i + 1 ) << std::setfill( asterisk ) << '\n';
}


std::cout << std::endl;

}
while (false)
{
cout<<"invalid number";
}
}
Last edited on
> Include a do loop to validate the user input
>> i am using. a while loop :/

> Enter a positive integer: 4
>> std::cout << "Enter a non-negative number: ";
explain that to me
you were told to do X, but you go and do Y, ¿will you ever listen to our advice?


draw a flowchart of your code, let's see if at least you understand why your solution is incorrect.
The thing is supposed to reject the users input when they put in a negative number. not just spit tons of asterisks. wdym draw a flow chart of my code. I thought it only worked with the std:: before the cout but I got rid of it and it still works. I still don't fully understand the point of the std::
how do I incorporate the do loop into my program? could you please just solve it in a c++ shell then I can figure out what I had wrong and ask questions on things I still don't understand?
Last edited on
> wdym draw a flow chart of my code.
https://ibb.co/6Fwhq7y
at the left is what you have, at the right is what you need.
1
2
3
4
do{
   std::cout << "Enter number: "; 
   std::cin >> n;
}while(n<=0); //note the semicolon 
Ok so I replaced things here and there and I put the do then the while things but it then said there had to be a while before the do. I'm at a loss of what to do again...

#include <iostream>
#include <iomanip>
using namespace std;
int main(int n,int i)
{//said it had to have a while before this. If it does need one what do I put in the parenthesis

do{
const char asterisk = '*';

cout << "Enter a non-negative number: ";

size_t n = 0;
std::cin >> n;

while (n<= 0);
{std::cout<<"Invalid number - Try again!"<<std::endl;
return true;
}
while (n>0);
{ std::cout << std::setw( n - i ) << std::setfill( ' ' ) << asterisk;
std::cout << std::setw( 2 * i + 1 ) << std::setfill( asterisk ) << '\n';
}
}
}
So I did some editing to my code. I tried seeing if I could just do if elses and stuff but they won't work. And now it has more problems:
#include <iostream>
#include <iomanip>
using namespace std;
int main(int n, char**argv,int i)
{
const char asterisk = '*';

cout << "Enter a non-negative number: ";

size_t n = 0;//for some reason again it says that this needs to be declared.
std::cin >> n;

if (n < 0|| n ==0)//I tried doing (n<=0) but it wouldn't work
std::cout<<"Invalid number - Try again!"<<std::endl;
return true;
else (n>0)//I dont know why but when I put else it always says there is no if or if I put if else or else if there is always a problem. But it I just put if it works but at the same time it doesn't. It will do one and the other at the same time. I think because there is no else.
std::cout << std::setw( n - i ) << std::setfill( ' ' ) << asterisk;
std::cout << std::setw( 2 * i + 1 ) << std::setfill( asterisk ) << '\n';
}
Last edited on
http://cpp.sh/9yrv
ok this is what I have now but how do I make it work for when I put in 1 row and 2 rows and not spam when I put a negative number. At least it says Invalid number now when I put a negative integer.
Nvm I solved it guys.
Here is the solved program:
http://cpp.sh/8ts6j
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>
using namespace std;

void draw( int S, int A = 1 )
{
   if ( S >= 0 ) { cout << string( S, ' ' ) + string( A, '*' ) + '\n';   draw( S - 1, A + 2 ); }
}

int main()
{
   int N{};
   while( N <= 0 ) { cout << "Enter N: ";   cin >> N; }
   draw( N - 1 );
}
Last edited on
Registered users can post here. Sign in or register to post.