Unable to catch particular user input error

win 10
VS 2015 prof.

I am working on a program that invites the user to input a date:
m for month, d for day and y for year.

These input values will be used in the program to do a series of calculations.

I am at the stage where the input is checked for errors (I found 7 possible errors).

And verifying that everything was OK i found an 8th possible error and that is the subject of this post.

To avoid laying everything out, let's look at the input for the month.

In main() you have:

int m;
cout << "Input the number for the month and press the ENTER key: ";
cin >> m;
.
.
.
suppose the user instead of inputting, for ex., 10 inputs 1o

I don't know how to catch that type of error!!

closed account (48bpfSEw)
http://stackoverflow.com/questions/11374617/the-easiest-way-to-read-formatted-input-in-c

answer of perreal:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <sstream>

int main(int argc, char **argv) {
  std::string str = ":12341+414112=absca";
  std::stringstream ss(str);
  int v1, v2; 
  char col, op, eq; 
  std::string var;
  ss >> col >> v1 >> op >> v2 >> eq >> var;
  std::cout << v1 << " " << v2 << " " << var << std::endl;
  return 0;
}
suppose the user instead of inputting, for ex., 10 inputs 1o

If you're want to catch the user entering alphas, you're going to have to read the input as a string and do your own numeric editing, then convert to an int.
Topic archived. No new replies allowed.