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?
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.
#include <iostream>
#include <iomanip>
usingnamespace 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
*************************************************************************
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
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.
// 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 constexprint MAXWIDTH{ 42 }; // <--- Width of the lines.