Hi everyone, So my program needs to print out an inverted pyramid of numbers. I have a working code but I don't think i am allowed to use if and else statements to complete it. Is there anyway to write the code without the use of if and else. The assignment says to use a while loop to repeatedly prompt the user. In this case, n must be odd and positive. I have tried a few ways and cant get anywhere. Thanks.
Does that mean that once a pyramid has been printed, the programs asks for a new number for a new pyramid? How should one stop such outmost loop?
An input of integer has three possible outcomes:
1. Failed input. Stream in error state. Could be due to EOF or non-digits in input.
2. An integer
2a Invalid
2b Valid; odd and positive
There is usually some testing in the form of if. It does depend on how you need to handle each case.
One scenario is to continue only until the input is not odd and positive:
1 2 3 4 5 6 7 8 9
int value = 0;
std::cout << "Enter an odd, positive integer:\n";
while ( std::cin >> value && 0 < value && 1 == (value%2) ) {
// print the pryramid with value
// next round
std::cout << "\nEnter an odd, positive integer:\n";
}
std::cout << "Input was not an odd, positive integer. Goodbye!\n";