Line Output. (homework)

Yes this is homework but I just need some direction please

I am looking to pause my 31 line file at line 24 and wait for the user to hit enter. I have tried some for loops and it displays one at a time. Looking for a way to make my counter that I am passing do this but I cannot get the logic down. Please help. Thank you.

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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;

void readfile(string f, ifstream &infil, int size);
int GetNumLines(string f);
void readLast10(string f, ifstream &infil, int size);
void withLine(string f, ifstream &infil, int size);


int main()
{
	
	ifstream infile;
	string clsname, filen;
	int response, size;
	cout << "Enter file name : ";
	cin >> filen;
	cout << "1. readfile\n2.read last 10\n3. with line#\n99 exit\n"
		<<"\nEnter 1, 2, 3, 99 : ";
	cin >> response;
	cout << endl;
	
	while(response != 99)
	{
		if(response == 1)
		{
		size = GetNumLines(filen);
		readfile(filen, infile, size);
		}
		
		
		else if(response == 2)
			
			{
			size = GetNumLines(filen);
			readLast10(filen, infile, size);
			}
		
		
		
		else if(response==3)
			{
			size = GetNumLines(filen);
			withLine(filen, infile, size);
			}
		

		
		cout << "1. Display All Records in file\n2. Display last 10 records 10\n3. Display all records with line numbers\n99 exit\n"
			<<"\nEnter 1, 2, 3, 99 : ";
		cin >> response;
	}

	system("pause");
	return 0;
	
}




void readfile(string f, ifstream &infil, int size)
{
	
		{
		infil.open(f, ios::in);
		getline(infil, f, '\n');
		  
		while(infil)
		{
			cout << f << endl;
			getline(infil, f, '\n');
			
		}
		 infil.close();
		}
		cout<< endl;
		cout<< endl;
}




int GetNumLines(string f)
	{
   ifstream in;
   in.open(f.c_str());
   string tmp;
   int count = 0;

   while(in)
   {
      getline(in, tmp, '\n');
      count++;
   }
  
   in.close();

   return count-1; 


		cout<< endl;
		cout<< endl;
}





void readLast10(string f, ifstream &infil, int size)
{
   ifstream in;
   int skip = size - 10;
   string str;

   in.open(f.c_str());
   
   for(int i=0; i<skip; i++)
   {
      getline(in, str, '\n'); // Gets the line, no output
   }

   while(in)  // Get remaining lines
   {
      getline(in, str, '\n'); // Get the line
      cout << str << endl;    // Output
   }

   in.close();


	
}




void withLine(string f, ifstream &infil, int size)
{
	
	int linenumber = 0;
	infil.open(f, ios::in);
	getline(infil, f, '\n');
		  
	while(infil)
	{
	cout << ++ linenumber << ": " << f << endl;
	getline(infil, f, '\n');
	}
	infil.close();

	cout<< endl;
	cout<< endl;
}
Keep track of how many lines you have printed out, and if you have printed out 24, then use cin.ignore(unsigned(-1), '\n') to wait for them to press enter.
Last edited on
Thank you! I will use that logic!

Thanks for the direction
Topic archived. No new replies allowed.