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