Do while loop repeats infinitely

Pages: 12
I'm creating a calendar program and am trying to make it so that it will repeat the question if you don't enter a valid year, but if I don't enter a valid year, it repeats the message forever and won't let me input an answer. help would be appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include "conio.h"
#include <string>
using namespace std;

int main(){
    int year = 0
    int validyear = 0

do {
    cout << "What year is it?" << endl;
    cin >> year;
} while (year <= 0)

if (year>0)
    {validyear==1}
_getch();
}

  
Last edited on
You didn't add a ; at the end of line 7, 8, 13, and 16.

Try this loop:

1
2
3
4
5
6
while (1)
{
	cout << "What year is it?" << endl;
	cin >> year;
	if (year > 0) break;
}
I compiled your program and it ran fine. It repeated the question when I typed 0 or less, and went on when I typed 1 or more. You have some syntax errors here though.
1
2
3
4
5
6
int year = 0; //needed ";"
int validyear = 0; //same

} while (year <= 0); //needed ";"

validyear = 1; //needed ";" and == is a comparison operator. = is the assignment operator 
(There weren't any syntax errors, I just forgot I could copy paste and typed it out manually, screwing up the syntax.)

I tried that fix, but it still repeats infinitely for me. If I type in an invalid year, it repeats the message...

What year is it?
What year is it?
What year is it?
What year is it?
What year is it?
What year is it?
What year is it?
What year is it?....

...Never giving me a chance to type in an answer.

Last edited on
Did you try the loop I gave?
Yes.
Is your code similar to what you posted? I ran it without any problems.
It's exactly the same, is my net book the problem?
It shouldn't be. Try putting _getch() inside your loop after cin >> year; If it still repeats, then I have no idea.
It stopped printing out the message infinitely, but if I press any key it just prints out the message again, I can't type in an answer.
Well... Sorry, I tried. Perhaps cin.clear(); before cin >> year; would help.
Still doesn't work, sorry.
Are you putting in a letter or something else that would cause cin to go into an error state?
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
#include <iostream>
using namespace std;

int main () {

	bool tochno = false;

	int year;
	int valid=0;

	do {	
	cout<<"Enter year: ";
	cin>>year;
	if (year != 0)
		tochno = true;
		
	}
		while (year <= 0 );
		{
		valid ++;
		}
		cout<<"Valid years: "<<valid<<endl;

		cin.get(); cin.get();
		return 0;
}



Here's the code mate... :))
That doesn't work either. Again, is the problem my netbook?, or is it that I use windows XP?
Last edited on
I don't know but it works fine to me. What compiler do you use?
He is probably using Visual Studio, I just tested with MSVC2010 and the code loops infinitely, but with GCC all "seems" to be working.
Tested on Windows 7 x86.
Last edited on
This will loop infinitely if cin>>year fails. Besides, you have a block in the middle of nowhere that doesn't really do anything useful.

Try this:
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
#include <iostream>
using namespace std;

int main () {

	bool tochno = false;

	int year=0;
	int valid=0;

	do {	
	cout<<"Enter year: ";
	if(!(cin>>year))
        {
              cin.clear();
              cin.ignore(cin.rdbuf()->in_avail());
        }
	if (year != 0)
		tochno = true;
		
	} while (year <= 0 );
	
        valid ++;
	cout<<"Valid years: "<<valid<<endl;

	cin.ignore(cin.rdbuf()->in_avail()+1);
	return 0;
}


This does the same thing as the original code, with the difference of not being buggy. Then again it may not do what you want to do, considering I have no idea what you tried to achieve.
Last edited on
Probably he is entering a letter to test as invalid input.
But thing I was trying to do was stop you from entering b.c years or letters, I noticed when I typed in a word as the year it was converted to a negative number, so I was trying to tell it not to accept negative numbers, solving both problems.
Pages: 12