I'm in my first semester of a C++ class and our first program we have to design is a program to tell the day of any date enter after the 1600's. I'm stuck in an infinite loop and I'm having trouble trying to fix it. If someone could tell me just how to fix the infinite loop I know I can finish up the program by my self. Here is my code.
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
string Days[7]={"Sunday","Monday","Tuesday","Wednesday","Thursday",
"Friday","Saturday"};
int a,month,year,y,day,m,d;
a = (14-month)/12;
y=year-a;
m = month+12*a-2;
d = (day+y+y/4-y/100+y/400+(31*m/12))%7;
The part in bold is where my problem is. If I enter thing that isn't a number I get stuck in the infinite loop. This is my first program ever and it is a little rough so I apologize in advance.
He wants us to use the shell program he provided for us. the code I wrote is from "// My program" til the end of the bold lettering. Let me ask you this instead. What I was trying to do was, if the user entered anything that wasn't 1-12 I wanted the program to say "Error blah blah blah please enter a number between 1-12". Is there an easier way to do so without have to do a while statement?
cin >> month;
if( !cin.good() )
{
//user typed in something that's not an int
cout << "Error blah blah blah please enter a number between 1-12" << endl;
//clear the error flags. Hopefully, the user will behave next time
cin.clear();
//sync up the stream in case the user entered white space
cin.sync();
}
Yes. If the logic is "have the user keep entering a number until the number is between 0 and 11, inclusive", then the keep entering a number until... requires a loop.
Ok I understand that. Where would you suggest I place the while statement? This is what I tried
cin >> month;
while (month < 0 || month >12)
{
if( !cin.good() )
{
//user typed in something that's not an int
cout << "Error blah blah blah please enter a number between 1-12" << endl;
//clear the error flags. Hopefully, the user will behave next time
cin.clear();
//sync up the stream in case the user entered white space
cin.sync();
} }
Now any number between 1-12 it accepts but now instead of an infinite loop it won't do anything now.
month = -1; //who knows what month will be if the user enters something that's not an int
cin >> month;
while (month < 1 || month > 12) //see post above. Though 0-11 makes sense as a zero-indexed month
{
if(!cin.good())
{
cout << "Error blah blah blah please enter a number between 1-12" << endl;
cin.clear();
cin.sync();
}
cout << "Please select a number between 1-12" << endl;
cout << "Enter your number here ==>" << endl;
cin >> month;
}
#include <iostream>
usingnamespace std;
int main()
{
int month = - 1; //who knows what month will be if the user enters something that's not an int
cin >> month;
while (month < 1 || month > 12) //see post above. Though 0-11 makes sense as a zero-indexed month
{
if(!cin.good())
{
cout << "Error blah blah blah please enter a number between 1-12" << endl;
cin.clear();
cin.sync();
}
cout << "Please select a number between 1-12" << endl;
cout << "Enter your number here ==>" << endl;
cin >> month;
}
return 0;
}
hello?
Error blah blah blah please enter a number between 1-12
Please select a number between 1-12
Enter your number here ==>
your number here
Error blah blah blah please enter a number between 1-12
Please select a number between 1-12
Enter your number here ==>
17
Please select a number between 1-12
Enter your number here ==>
-1
Please select a number between 1-12
Enter your number here ==>
y
Error blah blah blah please enter a number between 1-12
Please select a number between 1-12
Enter your number here ==>
7
Press any key to continue . . .
#include <iostream>
#include <string>
usingnamespace std;
int main(void)
{
string Days[7]={"Sunday","Monday","Tuesday","Wednesday","Thursday",
"Friday","Saturday"};
int a,month,year,y,day,m,d;
a = (14-month)/12;
y=year-a;
m = month+12*a-2;
d = (day+y+y/4-y/100+y/400+(31*m/12))%7;
// My program
cout << "Please select the number of the month desired" << endl;
cout << "January = 1" << endl;
cout << "February = 2" << endl;
cout << "March = 3" << endl;
cout << "April = 4" << endl;
cout << "May = 5" << endl;
cout << "June = 6" << endl;
cout << "July = 7" << endl;
cout << "August = 8" << endl;
cout << "September = 9" << endl;
cout << "October = 10" << endl;
cout << "November = 11" << endl;
cout << "December = 12" << endl;
cout << "Enter your number here ==>" << endl;
cin >> month;
while (month < 1 || month > 12)
{
// Code from the internet
//******************************************************************************************
if( !cin.good() )
{
//user typed in something that's not an int
cout << "You have entered an invalid number!" << endl;
//clear the error flags. Hopefully, the user will behave next time
cin.clear();
//sync up the stream in case the user entered white space
cin.sync();
}
//**************************************************************************************
cout << "You have entered an invalid number" << endl;
cout << "Please enter a value between 1-12" << endl;
cin >> month;
}
cout <<endl <<"The Date: "<<month<<"/"<<day<<"/"<<year
<<" Falls on a: "<< Days[d]<<endl;
return 0;
}
Like I said, any number I type in the program does a single loop and I can keep entering invalid numbers until I get tired of it. When I throw in anything but a letter it goes crazy and spams this
You have entered an invalid number
Please enter a value between 1-12
You have entered an invalid number!
You have entered an invalid number
Please enter a value between 1-12
You have entered an invalid number!
You have entered an invalid number
Please enter a value between 1-12
You have entered an invalid number!
You have entered an invalid number
Please enter a value between 1-12
You have entered an invalid number!
You have entered an invalid number
Please enter a value between 1-12
You have entered an invalid number!
You have entered an invalid number
Please enter a value between 1-12
You have entered an invalid number!
You have entered an invalid number
Please enter a value between 1-12
You have entered an invalid number!
You have entered an invalid number
Please enter a value between 1-12
Ok that makes sense. First of all (using your previous post) I moved line 3-6 to the bottom of the program, that fixed it up a little bit. As for initializing the variables, I get confused there. I know that month must be between 1-12, year must be greater than 1582 for his equation to work, and also that the day must be between 1-31. So would it be something like this
To initialize a variable, assign a value to it.
To assign a value to a variable, use the assignment operator.
The assignment operator is "=".
These are assignments
Whatever is in the while loop will get repeated over and over. Your while loop is missing something that shacktar's had: cin >> month;. If the input is bad, you need to give the user another chance.