Help with for/while loop

This is my first post - sorry if its not in the right spot.

I need to modify the following program (using a for and/or while loop) so it displays 20 lines of data, clears the screen, and then displays the next 20 lines of data until all the data has been displayed.
--------------------------------------------------------------------------------

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
// This program displays a list of numbers and
// their squares.
#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
	int startnum;
	int maxnum;

	cout << "This program will square the numbers between a starting and
 ending number\nindicated by the user.\n\n";

	cout << "Please enter the starting number: ";
	cin >> startnum;
	cout << endl;

	while (startnum < 1 || startnum >100)
	{
		cout << "Error! Starting number must be between 1-100. Please 
enter the starting number again: ";
		cin >> startnum;
	}

	cout << "Please enter the ending number: ";
	cin >> maxnum;

	int num = startnum;

	cout << "====================================================\n";
	cout << "Number: \t\tSquare:\n";
	for (num = startnum; num <= maxnum; num++)
		cout << num << "\t\t" << (num * num) << endl;
	

	cout << "\n\n\nPress any key to close this window...";

	_getch();
	return 0;
}
Last edited on
I thought it was the brackets but I'm not sure.
Last edited on
logana- the brackets actually work fine. That part of the code is being used to validate the input of the starting number (startnum). I'm struggling with displaying 20 lines of output at a time. For example, if startnum is 1 and maxnum is 45, the program displays all the lines of output at once...
Can't use <conio.h> in my Linux machine, anyway I hope the following code could be of any help:
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
// This program displays a list of numbers and
// their squares.
#include <iostream>
#include <limits>

using namespace std;

int main()
{
    cout << "This program will square the numbers between a starting "
            "and ending number\nindicated by the user.\n\n";

    cout << "Please enter the starting number: ";
    int startnum;
    cin >> startnum;
    cout << endl;

    while (startnum < 1 || startnum >100)
    {
        cout << "Error! Starting number must be between 1-100. "
                "Please enter the starting number again: ";
        cin >> startnum;
    }

    cout << "Please enter the ending number: ";
    int maxnum;
    cin >> maxnum;

    cout << "====================================================\n";
    cout << "Number: \t\tSquare:\n";
    // displays 20 lines of data, clears the screen, and then displays 
    // the next 20 lines of data until all the data has been displayed
    int num = startnum;
    while(num <= maxnum)
    {
        for(int i = 0; num <= maxnum && i < 20; i++)
        {
            cout << num << "\t\t" << (num * num) << endl;
            num++;
        }
        cout << "\nPress ENTER to continue..." << endl;
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        // missing operation: clear the screen
        // available in conio.h?
    }

    cout << "\n\n\nPress ENTER to close this window." << endl;
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

    return 0;
}


Happy coding!
Topic archived. No new replies allowed.