Question

___________________________________________________________________________
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;
}
}
Last edited on
Maybe like this:
1
2
3
4
5
6
7
8
9
10
11
12
if (rows_num >= 1 && rows_num <= 9)
{
  // print
}
else if (rows_num == 0)
{
  // exit message
}
else
{
  // invalid input
}

Remember that comparison operator is ==
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;
}
And when user enter symbol or character, i want the display show "Invalid", but this program shows me "I hope you enjoy"
Read here what happens when you read a symbol or character into an int.
https://stackoverflow.com/questions/51001849/pass-a-char-to-an-int-in-using-stdcin-in-c
I want it to display "invalid" when user key in character or symbol, but it displays ' enjoy '

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 {
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;
}
}
You should read the answers on the page Thomas linked, they explain how to check for valid input.
For example,
1
2
3
    int i1;
    std::cin >> i1 ;  // std::cin fails here, in the the case of '|'
    if(std::cin.fail()) { std::cout << "Failed"; }
You can use isdigit():

http://www.cplusplus.com/reference/cctype/isdigit/

You'll take it in as a char and check if the character is a digit.
Last edited on
actually, i havent learn isdigit so i cant use this method. Is there another way?
Maybe like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

using namespace std;

int main()
{
    char ch = cin.get();
    if(ch >= '0' && ch <= '9') {
      cout << "valid\n";
    }
    else {
      cout << "Invalid\n";
    }
}


Are you supposed to check the input?
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;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

using namespace std;

int main()
{
    char ch = cin.get();
    if(ch >= '1' && ch <= '9')
    {
      cout << "valid\n";
    }
    else if (ch == '0')
    {
        cout << "enjoy";
    }
    else
    {
      cout << "Invalid\n";
    }
}
Thank you~~
Topic archived. No new replies allowed.