Having a program use cerr when protocol is broken

I wrote a program that determines the range between two integers. I would like to set something up along the lines of

 
cerr << "Either one or both of the numbers you entered were not integers" << endl;

after the program checks if a and b are integers.

Unfortunately, I don't know how to incorporate this into the code I have written. I imagine it isn't a difficult task, but I am only on my third week of self studying so I am no where close to having mastered all the basic tools. The code I have written is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>

// using std namespace for cout,cin,cerr,endl so std:: is un-necessary
using std::cout;
using std::endl;
using std::cin;
using std::cerr;

int main(int argc, char *argv[]) {
  // prompt the user to enter two integers
  cout << "Enter two integers:" << endl;
  int a, b = 0;  // initialize a,b,i as int
  cin >> a >> b;
  if (a > b) {
    int i = b;
    while (i >= b && i <= a) {
      cout << i << endl;
      ++i;
    }
  } else if (b > a) {
    int i = a;
    while (i >= a && i <= b) {
      cout << i << endl;
      ++i;
    }
  } else {
    // prompt user that there is no range when a = b
    cout << "You entered the same number so there is no range" << endl;
    return -1;  // failure
  }
  return 0;
}
Last edited on
Assume a user inputs the numbers 5.8 6.2. Now a = 5 and b = 6 because variable a and b are both declared as integers.
@konstances how is it that you think your response answered or helped the question I am asking?
Maybe i didnt understand your question then.Was your question how to check if input values are integers?
I don't know how to setup a check if the values entered are integers. I would like to set that up and then use

 
cerr << "statement" << endl;

if the numbers entered are not integers.
How about this, change your variable type to char, then check if the value are number using isnum() in <ctype.h>
@LendraDwi I understand your suggestion but don't (only been teaching myself for 3 weeks). Can you provide an example for your suggestion?

Thanks.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <string>
#include <sstream>

int read_int()
{
    std::string str ;
    std::cout << "please enter an integer: " ;
    std::cin >> str ; // read formatted input into a string

    // http://en.cppreference.com/w/cpp/io/basic_istringstream
    std::istringstream stm(str) ; // create an input stream to read from the string
    int value ;
    char crud ;

    // if an int could be read from the stream, and there is nothing left to read after that
    // '1234', '+1234', '-1234' are fine; '12.34', '123t', '123456789876543210' are not
    if( ( stm >> value ) && !( stm >> crud ) ) return value ; // return the int

    // there was an error in input
    std::cerr << "error in input: you did not enter an integer\n" ;

    // http://en.cppreference.com/w/cpp/io/basic_istream/ignore
    std::cin.ignore( 1000, '\n' ) ; // throw away the junk in the input buffer

    return read_int() ; // and try again
}

int main()
{
    int value = read_int() ;
    std::cout << value << '\n' ;
}
@JLBorges, Thanks.

Everything worked. What I wrote was (which may be entirely too verbose but hey I got it to work):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <string>
#include <sstream>

// using std namespace for cout,cin,cerr,endl,string,istringstream so std:: is un-necessary
using std::cout;
using std::endl;
using std::cin;
using std::cerr;
using std::string;
using std::istringstream;

int read_inta() {
  // prompt the user for the first integer
  cout << "Enter in first integer:" << endl;
  string a;  // initialize a as empty strings
  cin >> a;
  // create an input stream to read from the string
  istringstream stma(a);
  int agood;
  char abad;
  // check that a is an int and not char
  if ((stma >> agood) && !(stma >> abad)) {
    return agood;
  } else {
    cerr << "Your first character was not an interger" << endl;
  }
  // throw away junk in the input buffer
  cin.ignore(1000, '\n');
  return read_inta();
}

int read_intb() {
  // prompt the user for the second integer
  cout << "Enter in second integer:" << endl;
  string b;  // initialize b as an empty strings
  cin >> b;
  // create an input stream to read from the string
  istringstream stmb(b);
  int bgood;
  char bbad;
  // check that b is an int and not char
  if ((stmb >> bgood) && !(stmb >> bbad)) {
    return bgood;
  } else {
    cerr << "Your second character was not an interger" << endl;
  }
  // throw away junk in the input buffer
  cin.ignore(1000, '\n');
  return read_intb();
}

int main(int argc, char *argv[]) {
  int a = read_inta();
  int b = read_intb();
  cout << "The range of integers betweend " << a << " and " << b
       << " are/is:" << endl;
  // check to determine if a or b is greater
  if (a > b) {
    int i = b;
    while (i >= b && i <= a) {
      cout << i << endl;
      ++i;
    }
  } else if (b > a) {
    int i = a;
    while (i >= a && i <= b) {
      cout << i << endl;
      ++i;
    }
  }
  return 0;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>

int read_int( std::string prompt = "please enter an integer" )
{
    std::string str ;
    std::cout << prompt << ": " ;
    std::cin >> str ; // read formatted input into a string

    // http://en.cppreference.com/w/cpp/io/basic_istringstream
    std::istringstream stm(str) ; // create an input stream to read from the string
    int value ;
    char crud ;

    // if an int could be read from the stream, and there is nothing left to read after that
    // '1234', '+1234', '-1234' are fine; '12.34', '123t', '123456789876543210' are not
    if( ( stm >> value ) && !( stm >> crud ) ) return value ; // return the int

    // there was an error in input
    std::cerr << "error in input: you did not enter an integer\n" ;

    // http://en.cppreference.com/w/cpp/io/basic_istream/ignore
    std::cin.ignore( 1000, '\n' ) ; // throw away the junk in the input buffer

    return read_int(prompt) ; // and try again
}

int main()
{
    int a = read_int( "enter the first integer" ) ;
    int b = read_int( "enter the second integer" ) ;

    if( b < a ) std::swap(a,b) ; // http://en.cppreference.com/w/cpp/algorithm/swap

    for( int i = a ; i <= b ; ++i ) std::cout << i << ' ' ;
    std::cout << '\n' ;
}
Topic archived. No new replies allowed.