File input counting lines/loop for validation

Hey everybody, i'm semi-new to c++ and have a assignment im working on for college and am having trouble with two things specifically. My assignment is this:

Write a program that takes, as its input, a C++ program and does the following:

Counts the number of lines, excluding completely blank lines.
Counts the number of open braces. {
Counts the number of close braces. }

Your program should request the name of a .CPP file from the user, looping if the name is not valid. Once it encounters a valid name, it should process the file as described.

The output should be three lines: the number of lines, the number of open braces, and the number of close braces.

I feel like i have most everything right except for two things. I keep getting an infinite loop when the user types in a file name that does not exist and i try to ask to input another file name. Also i don't know how to search for blank lines and not to count them. My prof said something about using getline(), but i'm still kind of fuzzy on how to use it. Any help would be greatly appreciated! Here is my code:
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 <fstream>
#include <string>
using namespace std;

int main()
{
	ifstream inputFile; // Input file stream
	string filename; // Create name for user to input file name
	int openBraces = 0, closedBraces = 0, lines = 0; // counters
	char ch;
	bool flag = true;

	// Ask for filename
	cout << "Enter the filename: ";
	cin  >> filename;

	//Try to open the file
	do
	{
		inputFile.open(filename.c_str());
		if(!inputFile)
		{
			cout << "Could not open file. \nPlease Enter another file name: " << endl;
		}

		else
		{
			flag = false;
		}
	} while(flag == true); //Getting infinite loop here!

	//If the file opens check each charecter for { or } or \n
	if(inputFile)
	{
		do
		{
			ch = inputFile.get();
			//If { is found increase openbraces
			if (ch == '{')
			{
				openBraces++;
			}

			else if (ch == '}')
			{
				closedBraces++;
			}

			else if (ch == '\n')
			{
				lines++;
			}

		}while(!inputFile.eof());

		inputFile.close();

		cout << "The number of open braces are: " << openBraces << endl;;
		cout << "The number of closed braces are: " << closedBraces << endl;
		cout << "The number of lines is: " << lines + 1 << endl;
	}

	else
	{
		cout << "Error opening the file.\n";
	}
	return 0;
}
Problem #1 - Infinite loop. Lines 15-16 should be inside your do while loop (after line 20). If your open fails, you loop back, but you don't prompt for a new file name.

getline will read a line of input at a time.
http://www.cplusplus.com/reference/string/string/getline/
You will need two loops. One to read lines and one to iterate through the line.
Thank you very much! Wow i completely missed the first one i don't know how haha.
Topic archived. No new replies allowed.