infinite loop

I am getting an infinite loop for some reason. It should just read until the end of the line but for some reason it reads the entire file then hits an infinite loop on the last character.

input file:

()
{}[]()
<<>><>
{[[[[]]]]}

This is just the function that I am calling and I just need to read in one line at a time so I'm just trying to get it to read the first line for now.
The way I have the code written it should stop when it reaches a new line but it doesn't.

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
void readFile(stack &stck)
{
	fstream infile;
	char temp = 'a';
	infile.open("first.txt");

	cout << "Input string:" << endl;
	infile >> temp;
	while (temp != '\n')
	{
		
		if (temp == '(' || temp == ')' || temp == '[' || temp == ']' || temp == '{' || temp == '}' || temp == '<' || temp == '>')
		{
			if (temp == '(' || /*temp == ')' ||*/ temp == '[' || /*temp == ']' ||*/ temp == '{' || /*temp == '}' ||*/ temp == '<' /*|| temp == '>'*/)
			{
				cout << temp;
				stck.push(temp);
			}
			else if (temp == ')' || temp == ']' || temp == '}' || temp == '>')
			{
				cout << temp;

				if (temp == ')' && stck.top() == '(')
				{
					stck.pop();
				}
				else if (temp == ']' && stck.top() == '[')
				{
					stck.pop();
				}
				else if (temp == '}' && stck.top() == '{')
				{
					stck.pop();
				}
				else if (temp == '>' && stck.top() == '<')
				{
					stck.pop();
				}
				else
				{
					stck.push(temp);
				}
			}
			infile >> temp;
		}
       }
}
Try using while ( infile >> temp )
Thanks for the input but I figured it out I need temp = infile.get
Topic archived. No new replies allowed.