Stuck in an infinite loop

Hey guys/gals,

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;

// 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 < 0 || month > 12)
{
cout << "Please select a number between 1-12" << endl;
cout << "Enter your number here ==>" << endl;

cin >> month;

}



cout <<endl <<"The Date: "<<month<<"/"<<day<<"/"<<year
<<" Falls on a: "<< Days[d]<<endl;
return 0;
}

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.

Any suggestions?
Initialize month to be something?

EDIT: Actually after reading "real" problem: http://cplusplus.com/reference/iostream/ios/good/
Last edited on
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?
To be honest eraggo, I'm not sure what reading that tells me. I've been programming for like 3 weeks now.
1
2
3
4
5
6
7
8
9
10
11
12
13
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();
}

You still need the loop though.
Last edited on
So you're saying I still need to keep the while statement in the program then?
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.
Last edited on
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.
Can't really help you with the code, but just a tiny point. I think you want while (month < 1 || month >12) rather than while (month < 0 || month >12)
You still need to ask for the month in the loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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;
}
Last edited on
I'm still getting an infinite loop when a letter is entered.... This is giving me a headache lol
Can we see the full code, now (with code tags [code]code goes here[/code])? Here's what happens when I run the following program.

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
#include <iostream>
using namespace 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 . . .
Last edited on
Here is what I have so far

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
#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; 
    
    // 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

I tried to run it and it failed thanks to the use of uninitialized variables here:

1
2
3
4
5
6
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;


month , year , and day each haven't been initialized.
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

1
2
int month > 0;
int month < 12


I don't know if it matters but I'm using xcode on my mac to build and run the program.
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
1
2
int foo = 7;
double bar = -75.97;
These are not
1
2
int month > 0;
int month < 12;

Read this: http://www.cplusplus.com/doc/tutorial/variables/

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.
FYI, you all want a do-while loop...
1
2
3
4
5
do {
	...
	cin >> month;
	...
} while( month < 0 || month >= 12 ); //or [1, 12] in place of [0,12)  
I am not sure if the following code is the best solution, but it works for me. If somebody knows a better one please let us know.

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
#include <iostream>
#include <string>
#include <conio.h>

 
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; 
    
    // 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 ==> "; 
    
    do{
		char monthChar[2]={'0', '0'};
		for(;;){
			monthChar[0]=getch();
			cout << monthChar[0];
        
			if(monthChar[0]>='1' && monthChar[0]<='9'){
				break;
			}else{
                cout << endl << "You have entered an invalid number!" << endl;
                cout << "Please enter a value between 1-12" << endl;
			}
		}
    
        monthChar[1]=getch();
        cout << monthChar[1];
        
        if(monthChar[1]>='1' && monthChar[1]<='9'){
			month = 10*(monthChar[0] - '0') + (monthChar[1] - '0');
        }else if(monthChar[1]==13){//if Carriage Return
			month = monthChar[0] - '0';
        }else{
			month = 0; //to make it execute the loop again
			cout << endl << "You have entered an invalid number!" << endl;
			cout << "Please enter a value between 1-12" << endl;
        }
        
    }while( month < 1 || month > 12 );
    
    cout <<endl <<"The Date: "<<month<<"/"<<day<<"/"<<year
    <<" Falls on a: "<< Days[d]<<endl;
    getch();
    return 0;
}
So did you make it work or still need help?
Topic archived. No new replies allowed.