Making a table with c++

Hi guys,
I need to make a table with c++
like this
1
2
3
4
5
6
=============================================
       White                Black 
=============================================
           1                    2
           3                    4
           5                    6   

I know how to make it using normal coding with c++, but the problem is that I need to make it using modular programming: by using more than one function.
Any help, please?
Last edited on
Here's a start:

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
#include <iostream>

using namespace std;

void printLine();
void increment(int &);

int main()
{
	int count = 1;
	printLine();
	cout << "White Black" << endl;
	printLine();
	increment(count);
	increment(count);
	increment(count);
	return 0;
}

void printLine()
{
	cout << "==========================" << endl;
}

void increment(int & number)
{
	cout << number << " ";
	number++;
	cout << number << endl;
	number++;
}
Do you have a block of code that occurs more than once?
Move one copy of the block into a separate function and replace all occurrences of the block with function call.
I know how to make it using normal coding with c++, but the problem is that I need to make it using modular programming: by using more than one function.

That's a backwards way of thinking about the problem. A part of modularity is writing many small functions that do a single, small, well-defined task. A program that is modular is easier to understand and modify, less error-prone, shorter, and likely quicker to write (once you get it) than a single monolithic blob of code.

I'd suggest posting working code and then worry about splitting bits off once you've got it.

This (refactoring) is an excellent exercise and something you'll find yourself doing constantly when working on real software. It's also the only reasonable starting point if you're stuck splitting the program up from the start.
Thank you so much
I think I need to return something if I am using a function other than the main one
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
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
	int startF, endF, stepF;
	cout << "Enter a the first fahrenheit value and make sure it is between -10 and 200: ";  	// taking inputs
	cin >> startF;

	while ( ( startF < -10 ) || ( startF > 200 ) )
		{ 
			cout << "INPUT INVALID!! PLEASE RE-ENTER A VALUE IN RANGE: ";
	        cin >> startF;
                cout << endl;
		}

	cout << "Enter the last fahrenheit value you wish to convert: ";
	cin >> endF;
        cout << endl;
	while ( endF <= startF )
		{ 
			cout << "END VALUE IN-VALID; PLEASE, RE-ENTER: ";
	        cin >> endF;
                cout << endl;
		}

	cout << "Enter the step fahrenheit value: ";
	cin >> stepF;
        cout << endl; 
	while ( ( stepF <= 0 )  || ( stepF > (endF - startF)) )
		{ 
			cout << "INPUT INVALID!! PLEASE ENTER A STEP VALUE: ";
	                cin >> stepF;
                        cout << endl;
		}
	for ( int c = 1; c <= 30; c++)
		cout << '-';
	cout << endl;

	cout << setw(19) << "Fahrenheit" << setw(19) << "Celsius" << endl;
	for ( int c = 1; c <= 30; c++)
		cout << '=';
	cout << endl;

	cout << setprecision(0);
	for ( int f = startF; f <= endF; f += stepF)
		cout << setw(15) << f << setw(15) << 5.0 / 9 * ( f - 32.0 ) << endl;

	for ( int c = 1; c <= 30; c++)
		cout << '*';
	cout << endl;

	
	system ("pause");
}

1
2
3
4
5
The output should look like this: 
                       Fahrenheit                            Celcius 
=========================================================
                              value                                 value 
                              value                                 value 
*************************************************************************
[/code]
Last edited on
I need to do the exact same thing but using modular programming and I don't know how can I start?
Also, when I use void on cpp.sh (c++ shell), it doesn't work
You have three blocks:
1
2
3
4
5
6
7
8
9
10
11
12
13
	for ( int c = 1; c <= 30; c++)
		cout << '-';
	cout << endl;


	for ( int c = 1; c <= 30; c++)
		cout << '=';
	cout << endl;


	for ( int c = 1; c <= 30; c++)
		cout << '*';
	cout << endl;

They differ in only one detail. Same function can do each of them, if you pass the detail to the function. The function does not need to return anything to the main.
Hello Jack Van Stone,

Here is a different approach making use of keskiverto's suggestion and the "iomanip" header file:

1
2
3
4
inline void PrintLine(char ch)
{
	std::cout << std::setfill(ch) << std::setw(MAXWIDTH) << ch << std::setfill(' ') << std::endl;
}

The call would be: PrintLine('-');. You change the character as needed.

I reworked the for loop as:
1
2
3
4
std::cout << std::fixed << std::showpoint << std::setprecision(2);  // <--- Needs done only once.

for (int f = startF; f <= endF; f += stepF)
	std::cout << std::setw(15) << f << std::setw(22) << 5.0 / 9 * (f - 32.0) << std::endl;

Changed the second "setw" for a better look.

This is the way the table printed for me:
------------------------------------------
         Fahrenheit            Celsius
==========================================
              0                -17.78
              5                -15.00
             10                -12.22
             15                 -9.44
             20                 -6.67
             25                 -3.89
             30                 -1.11
             35                  1.67
             40                  4.44
             45                  7.22
             50                 10.00
             55                 12.78
             60                 15.56
             65                 18.33
             70                 21.11
             75                 23.89
             80                 26.67
             85                 29.44
             90                 32.22
             95                 35.00
            100                 37.78
******************************************



 Press Enter to continue

The last line came from:
1
2
3
4
5
//  Replacement for "system("pause")".
// Sometimes the next line is needed this time it is not.
//std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue";
std::cin.get();


I put this above main constexpr int MAXWIDTH{ 42 }; // <--- Width of the lines.

Hope this helps,

Andy
Topic archived. No new replies allowed.