How to Create Tables

Hi I just made this simple program and I want to create some tables so that the result does not appears so chaotic..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    ofstream out("table.txt");
    for(int a=1;a<11;a++)
    {
        for(int k=1;k<11;k++)
        {
            out<<a<<'x'<<k<<'='<<a*k<<endl;
        }
    }
    ifstream in("table.txt");
    out.close();
    return 0;
}


also if anyone can tell me how do I load a file in order to make it appear on the console as well as the file... because when I run the program I have to go look for the file instead of the file opening itself...
Last edited on
What do you mean by load a file? Like show the text from a .txt file.
Yes, that or opening the actual file
To display as a table:

1
2
3
4
5
6
7
8
9
10
ofstream out("table.txt");
for (int i = 1; i < 11; i++) {

    for (int j = 1; j < 11; j++) {
        out << i <<'x' << j << '=' << i * j << endl;
	cout << setw(4) << right << j * i;
    }

    cout << '\n' << endl;
}
Last edited on
Ok you can find toturials on youtube but here is some sample code.

You will need: #include <fstream>

The code to output it:
1
2
3
4
5
6
fstream textfile;
	string A;
	textfile.open("YOUR TEXT FILE'S LOCATION");
	textfile >> A; //Saves the textfile to string A;
	cout << A; //If you don't want it to be saved as a data type just cout textfile
	textfile.close();
when I use setw i got a message saying:

'setw' was not declared in this scope

how do I declare it?
If you would google for the function you would see it requires iomanip header. So add #include <iomanip> at the start of your code.
Topic archived. No new replies allowed.