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:
#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;
}
} elseif (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;
}
#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' ;
}
#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;
}
} elseif (b > a) {
int i = a;
while (i >= a && i <= b) {
cout << i << endl;
++i;
}
}
return 0;
}
#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/swapfor( int i = a ; i <= b ; ++i ) std::cout << i << ' ' ;
std::cout << '\n' ;
}