You are here:

C++/Tabular Columns


I am student in high school learning c++.I would like to
know if we can include tables in the form of spread sheets in c++ ? If yes , HOW ?  

The simple answer is no, you cannot. There is no direct support in standard C++ for any particular specialised data formats.

Even if you could include a spreadsheet, what would you do with it?

Notice here that I am not going to assume that there is only one sort of spreadsheet application or file format on the planet.

However, there are various ways in which you could import or access such data. Here are a few possibilities:

1/ You export the data from a spreadsheet program that is in a format that is both documented (i.e. not proprietary to any application - an example of a closed proprietary format would be Microsoft Excel binary xls data file format) and, possibly, easy to write code in C++ to parse. You write or use pre-existing code to read the contents of the data file.

2/ The spreadsheet application vendor provides an SDK (software development kit) that allows you to make use of their application documents from your own applications. An alternative is that the spreadsheet application uses an open format to store its data and you write or use software that understands this format.

3/ The application itself can be used as a component and controlled using a remote automation interface. In this case the application would need to be available (i.e. installed) for your program to make use of it as a component.

4/ You write or use a spreadsheet component that does what you wish.

When I say things like "use a component" or "use pre-existing software" this can be in many forms but will usually be some combination of code you include in your program and libraries you install and link with your code to create your programs. Some are free, some you have to buy licenses for. Not all such existing software is available for all system platforms (a combination of C++ development system and operating system). Some will be pre-built, and others, especially open source software, may need to be built before they can be used.

Of course the above assumes you want to have some full-on spreadsheet functionality. If all you wish to do is to group collections of data together then I suggest you start by looking into the various collection types available in the C++ standard library (in the part often referred to as the Standard Template Library or STL). These include vectors (single dimensional arrays), lists, maps, set, etc. Each has different pros and cons so are useful in different situations. Information can be found in any _good_ C++ book. A good C++ library tutorial and reference is "The C++ Standard Library a Tutorial and Reference" by Nicolai M. Josuttis. There are various places where you can find information online, such as (specifically the STL reference at, (be warned, some of the items in this library are non-standard so may not be in your C++ implementation), and

I suspect however that you will be taught at some point about the plain vanilla C/C++ built in array types. These are the most obvious ways to represent a table of data - as a two dimensional array. You specify an array by placing a set of dimensions in [] after the name of a variable, for example:

   int tenInts[10];

Or, as another example a two dimensional table of 3 rows of 4 columns of ints:

   int threeByFourInts[3][4];

Note that these arrays are fixed in size at compile time so the numbers in the [] have to be constant integer values.

You can however use variables as indexes to the individual elements of the array. In C++ (as in C) array indexes are positive integers starting at 0. Hence for tenInts the first element has index 0 (tenants[0]) and the last index 9 (tenants[9]). That is the valid range of indexes for any array dimension is 0 to one less than the size of the dimension.

Each element stores one instance of the type given on the left size of the array variable name. The following loop sets to zero the elements of the first array (also called a vector as it is a single dimensional array):

   for ( int i(0); i < 10; ++i )
       tenInts[i] = 0;

Which brings up a point: the value 10 is used in two separate places. Once to give the size of the array and again to specify the limits of the loop. Are these related? Well yes, they need to be the same. If we change one then we should change the other. However it will not always be obvious if a value like 10 used in different places refers to the same thing. Suppose we have another loop that displays some information:

   for ( int x(0); x < 10; ++x )
       std::cout << "x=" x << " x squared=" << x*x << '\n';

Are we calculating and displaying the squares of the values 0 to 9 because the vector tenInts has a size of 10 or not?

The second problem is that if someone changes the size of tenInts they may forget to check for other uses based on the size of the array.

So it is a good idea to name and initialise a constant for such values, and use this name in places where we need the array size, or loop extents etc.:

   unsigned int const NumberOfInts( 10 );
   int const BeginSquaredValues( 0 );
   int const EndSquaredValues( 10 );

   int tenInts[NumberOfInts];

   for ( unsigned int i(0); i < NumberOfInts; ++i )
       tenInts[i] = 0;

   for ( int x(BeginSquaredValues); x < EndSquaredValues; ++x )
       std::cout << "x=" x << " x squared=" << x*x << '\n';

Note that I am now using an unsigned int for the size of the array because arrays have to have a positive size. For consistency the type of the loop variable in the initialisation loop is also an unsigned int. I also added two constants for the beginning and ending of the range of values we wish squares displayed for. These are signed integers (you can square -12 for example), and the end value represents one past the end. This is a common way to specify ranges in C++.

Now if we wish to change the size of the array we only have to change the value initialising the NumberOfInts constant:

   unsigned int const NumberOfInts( 200 );

And both the array size and the number of elements initialised to zero in the loop will be updated (when the program is re-built of course!), making sure the two stay in step with each other. Of course the name tenInts is now not very descriptive - another point worth learning: be careful how you choose the names of variables!

Now this use of elements can be extended to the two dimensional table like array (which I shall now call table after my point on names of variables):

   unsigned int const NumberOfTableRows( 3 );
   unsigned int const NumberOfTableCols( 4 );

   int table[NumberOfTableRows][NumberOfTableCols];

   for ( unsigned int row(0); row < NumberOfTableRows; ++row )
       for ( unsigned int col(0); col < NumberOfTableCols; ++col )
         table[col][row] = 0;

The above uses what is called loop nesting. The inner col loop is nested inside the outer row loop. Each time around the row loop the col loop iterates from its start condition to its end condition. In this case each time around the row loop the col loop iterates through values of col from 0 to 3. Hence the elements of table are accessed in the following order:


This has been a very brief introduction to built in arrays. I suggest you refer to a good C++ reference for full details. The site I mentioned earlier has a useful tutorial section at with an array topic you might like to look at. You might find some useful information and C++ answers in the C++ FAQ at

I also suggest you use an internet search engine to locate other C++ online information, for example "C++ array tutorial" entered into Google returned many results which even looked like they were useful!

Finally: Learning how to write software is something that takes time. Learning the ins and outs of a programming language takes time. Learning the ins and outs of a large language like C++ takes even more time. So do not despair if you do not seem to be getting it all at once. It takes time for it all to soak in.

You might also like to ask your teacher about anything you do not understand. They know what they have taught you already and are much more familiar with you as a person than anyone like myself who is remote and only sees you through what you post in your questions can ever be.

Knowing what you know, how you thing etc. is important when trying to explain something as people think in different ways. You might like to remember that people think differently if how something is explained in class does not make sense to you. Ask questions to clarify. Make your teacher earn their pay!


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at


©2017 All rights reserved.

[an error occurred while processing this directive]