I am writing a simple program that will take a number (range 1-366) and output the month that that number is contained in. I am pretty much finished, but am having the issue where you have to put in the input TWICE for it to give an output.
For instance, i type in "32", and nothing happens. When I type in "32" again, then the desired output shows.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#ifndef INTRANGE_H
#define INTRANGE_H
class IntRange
{
private:
int input;
int lowest;
int highest;
public:
IntRange (int, int);
int getInt();
};
#endif
#include <iostream>
#include "IntRange.h"
usingnamespace std;
int main()
{
IntRange input (1, 367); // keeps numbers in yearly range
char a=0;
int day; // holds user input day
cout<<"This program converts days entered to the month of the year.\n";
cout<< "Please enter a day of the year. Keep in mind that this year is a LEAP YEAR.\n";
cout<< "To exit, enter 'exit'\n";
day=input.getInt();
while (a==0)
{
if (day >= 1 && day <= 31)
cout << "This day falls into the month of January.\n";
if (day >= 32 && day <= 60)
cout << "This day falls into the month of February.\n";
day= input.getInt();
}
return 0;
}
// input.cpp : Allows user to input one day of the year,
// and blocks anything out of that range
#include "IntRange.h"
#include <iostream>
usingnamespace std;
IntRange::IntRange(int=1, int=366)
{
lowest= 1; // lowest number allowed in the year range
highest= 366; // highest number in a year range
}
int IntRange::getInt()
{
cin>>input;
if(!( cin>>input))
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout<<"Invalid input. Try again.\n";
cin>>input;
cin.ignore();
}
while (input < lowest || input > highest) //loop to ensure the number entered is within range
{
cout<< "That is not a number of the year.\n";
cout<< "Enter a value from " << lowest;
cout<<" to " << highest <<".\n";
cin.ignore(numeric_limits<int>::max(), '\n');
cin>>input;
cin.ignore();
}
return input;
}