___________________________________________________________________________
Question: an hourglass made of numbers 1-9 only, 0 to exit.
How to limit the number to 9 only?
The main function part only displays "Invalid.." when I press 0. I want it to display "Thank you"
Sample Run
Enter an integer (1-9 only, 0 to exit): 10
You have entered an invalid integer, please try again.
Sample Run
Enter an integer (1-9 only, 0 to exit): 0
I hope you enjoyed using this program.
___________________________________________________________________________
#include <iostream>
using namespace std;
void pattern(int rows_num)
{
int i, j, k;
// for loop for printing
// upper half
for (i = 1; i <= rows_num; i++) {
// printing i spaces at the beginning of each row
for (k = 1; k < i; k++)
cout << " ";
// printing i to rows value
// at the end of each row
for (j = i; j <= rows_num; j++)
cout << j << " ";
cout << endl;
}
// for loop for printing lower half
for (i = rows_num - 1; i >= 1; i--) {
// printing i spaces at the
// beginning of each row
for (k = 1; k < i; k++)
cout << " ";
// printing i to rows value
// at the end of each row
for (j = i; j <= rows_num; j++)
cout << j << " ";
cout << endl;
}
}
int main()
{
int rows_num;
cout << "Enter an integer (1-9 only, 0 to exit) : ";
cin >> rows_num;
pattern(rows_num);
if (rows_num = 0)
{
cout << "I hope you enjoyed using this program. " << endl;
}
else
{
cout << "You have entered an invalid integer, please try again. " << endl;
}
}
Question: I want this program to run number 1 to 9 only, but when i entered 10, it still run for me. I want user enters 10 to display "Invalid interger"
#include <iostream>
using namespace std;
void asgm(int num)
{
int i, j, k;
for (i = 1; i <= num; i++) // for loop for printing, upper half
{
for (k = 1; k < i; k++) // printing i spaces at the beginning of each row
cout << " ";
for (j = i; j <= num; j++) // printing i to rows value at the end of each row
cout << j << " ";
cout << endl;
}
for (i = num - 1; i >= 1; i--) // for loop for printing lower half
{
for (k = 1; k < i; k++) // printing i spaces at the beginning of each row
cout << " ";
for (j = i; j <= num; j++) // printing i to rows value at the end of each row
cout << j << " ";
cout << endl;
}
}
int main()
{
int num;
cout << "Enter an integer (1-9 only, 0 to exit) : ";
cin >> num;
asgm(num);
if (num >= 1 && num <= 9)
{
cout << " " << endl;
}
else if (num == 0)
{
cout << "I hope you enjoyed using this program. " << endl;
}
else
{
cout << "You have entered an invalid integer, please try again. " << endl;
}
return 0;
}
Yeah.. it worked, i entered character and symbol will display the correct output
But when i entered 0 i want it to display 'enjoy' as exit, not invalid
int main()
{
int num;
cout << "Enter an integer (1-9 only, 0 to exit) : ";
cin >> num;
if (num >= 1 && num <= 9){
asgm(num);
{
cout << " " << endl;
}
}
else {
char ch=cin.get();
if (ch>= '0' && ch<= '9')
{
cout << "I hope you enjoyed using this program. " << endl;
}
else {
cout << "You have entered an invalid integer, please try again. " << endl;
}
return 0;
}
}