No idea understanding the logic/ how to complete the program

Hi there, I am having trouble understanding the logic behind this particular program.



Here is what my understand so far and my plan:

1. Open the file called "People.txt".
2. Program must read from the file.
3. Must have loop from 1900 to 2000.

My "People.txt" File contains something like this:

**
****
*****
**********
****************
**************************




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 #include <iostream>
#include <fstream>
using namespace std;

int main()
{
	ifstream inputFile;
	inputFile.open("People.txt");

	int asterisk;
	int star;
	int pop;

	for (int i = 1900; i <= 2000; i += 20)
	{
		cout << "For: " << i << endl;
	}



	return 0;
}
Your for loop does not access the file at all.

Here is some information on ways to read specific lines of a file: https://stackoverflow.com/questions/32766817/go-to-a-specific-line-in-file-and-read-it

If you are not worried about runtime at all, and just want simplicity of the program you could do something like:
1
2
3
4
5
6
7
8
9
10
string text;
int line;

while(getline(inputFile, line) && line < 2000) 
{
    line++
    if(line <2000 && line >1900){
         cout << line;
    }
}




Last edited on
Topic archived. No new replies allowed.