How to set cin.ignore

I'm trying to create a multiplication table program, but I'm having issues setting it up. Now, the multiplication table is working fine, it accepts all integers, but now I need to use cin.ignore so it ignores !#%^@, but I have no idea how to start typing up a code like that and combine it with this code. I also need to make it loop back to the question once it answers it, how do I do that? Any suggestions?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

using namespace std;

int main()
{
    int number;
    
    cout << "Please enter an integer number.";
    cin >> number;
    
  for (int i=0; i<number; i++)
  {
      for (int j=0; j<number; j++)
      {
          cout << (i+1)* (j+1) << " ";
      }
      cout << endl;
  }
  return 0;
}
Last edited on
do you mean that if the user enters characters that aren't number you want ti ignore it and the ask for another input?
Yes, that's what I'm looking for, but also if the person gets the answer they wanted, it asks again.
Is that your homework, StartUpReboot?

A very common strategy to clean up the std::cin buffer is:

1
2
std::cin.clear();
std::cin.ignore(1000, '\n');


where 1000 is an arbitrary number to say "a lot of char".
If you add the header <limits>, you can also write:
1
2
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n')


Topic archived. No new replies allowed.