Hi there.

I am trying to create C++ code that allows User Input in selecting a variety of fields, then it will calculate many different angles and show the users the results, as well as a graph.

However, it has been suggested to us by our lecturer that it may be a good idea to write the code to these calculations etc in C++, then input the results into Excel.

Does anyone have any idea how to do this? Literally looking for a way for the user to fill in the required values on C++ and then to be AUTOMATICALLY taken to the excel file to show the results in the table and graph format.

If this is not possible, is there a way to display the results in the table and graph format through C++?

Thanks very much in advance

Recommended Answers

All 10 Replies

The simplest way is to generate a CSV file and have Excel import it. The CSV file format is very simple, each field is separated by either a comma or a tab. If the text for a given field can contain commas then use tab to deliminate the fields. Excel can work with either so it doesn't matter which you use. Each line of the CSV file is a row in he Excel spreadsheet.

Will this also work for XCode 4 (I have a Macbook Pro)? And will it still display boxes for user input/output, because I get the idea of using colons etc to display in Excel, but does the code sitll work as normal?


What code do you mean? All I suggested is to write a standard text file, doesn't matter what compiler and/or IDE you use the create the program.

And will it still display boxes for user input/output,

Excel? Yes. Once the data is import Excel displays it just as if you had typed it in from the keyboard. Excel will remove the character used as field separators. If you don't know how this works then you should just create a small text file by hand (using Notepad for example) and import it into Excel.

I have done it using Notepad for a basic idea of what happens. Here is my code at the moment.

#include <iostream>
#include <cmath>

using namespace std; //Declare Language

int main(int argc, const char * argv[])
    char L;
    int N;
    double X;
    double Y;
    char ans1 = 'R';
    while (toupper(ans1) == 'R')
        cout<<"\n\n\nHi, and welcome to our group project.";
        cout<<"\n\nDo you know what is your group is?";
        cout<<" If you don't, please find out now.";
        char ans = 'Y';
        while (toupper(ans) == 'Y')
            cout<<"\n\nNow that you know the group, please enter the letter of your group to continue: ";

            if (L == 'C' or L == 'c')
                cout<<"\nCorrect. Now please enter the group number: ";
                if (N == 3)
                    cout<<"\nThat is correct, now we can begin the project";
                    cout<<"\n\nPlease chose your centre point.";
                    cout<<"\n\nPlease enter your X-Axis Value: ";
                    cout<<"\nPlease enter your Y-Axis value: ";
                    cout<<"\n\nThis is the angle B. The formula is: COS(B) = X/((X^2+Y^2)^0.5)";
                    cout<<"\n\nAnswer = "<<acos(X/pow((pow(X, 2))+(pow(Y, 2)), 0.5));

            if (L != 'C' or L != 'c' or N!=3)
                cout<<"\n\nThis is not your group. Please restart the program and chose the correct group!";
            cout<<"\n\nTo restart the program, press Y, to reset the program, or quit, press R: ";
        cout<<"\n\nTo reset, press R again. To quit, press any other key: ";
    return 0;

It isn't finished yet but the idea is that we will get many different values for angle B, as well as another angle Theta. Once we have a list of all these values we would want it to show on a graph and table in Excel, BUT at the start of the program still ask for user input etc. on the console. Once the results were calculated, it would then move to excel to show the results. That make any sense? I'm just not sure how to code it properly with this aim.

Thanks again.

Once the results were calculated, it would then move to excel to show the results

I think that you should try to move away from the idea that your program should automatically open Excel to display the results. There are a number of issues with this approach. For example, I don't have Excel on my computer, so what are you going to do then? Or maybe I do have Excel, but it's in a different path to where you think it's going to be? Or maybe I don't like Excel and want to graph the results in Gnuplot, or Origin, or Matlab or my own custom graphing program that I made once?

The point is that the easiest, most flexible thing to do is as Ancient Dragon said and just output a text file in CSV (commas or tabs) format. This is simple to code and the result is more flexible.

To output a tab-separated file, you just need to instantiate a std::ofstream object and use it in the same way that you would std::cout:

#include <iostream>
#include <fstream>

int main()
    // Create a file stream object and check that it opened correctly
    std::ofstream outFile( "temp.tsv" );
    if ( ! outFile.is_open() )
        std::cerr << "Failed to open output file" << std::endl;
        return -1;

    // Write some stuff to the file
    outFile << "1\t2" << std::endl;
    outFile << "2\t4" << std::endl;
    outFile << "3\t6" << std::endl;
    outFile << "4\t8" << std::endl;

    // close the file

    return 0;

Temp.tsv will be written into the same directory as the executable, if you want it in another directory then you just have to put the path to the place where you want to write the file in the name too (e.g. to write to the parent directory open with the name "../Temp.tsv", or an absolute path "/home/bob/Temp.tsv"). If you open temp.tsv in Excel, you'll see two columns of numbers as you'd expect. You can also plot this kind of file in something like Gnuplot.

Hope that helps.

Another way to accomplish it is to not use any external programs to display the data, if you use one of the .NET languages (CLR/C++, VB.NET, and C# you have access to a DataGridView control which is very much like an Excel form. After writing the data to a CSV file you can tell the DataGridView to display it. I've done it in VB.NET but not in the other two languages but it should be similar. I think (not sure) that vb.net can also do some charts to show the data graphically.

Okay, thanks for all that! Writting in CSV format seems to be the best option. I will try and give it a go, but do any of you know/can you recommend a good site to learn how to write in CSV format? Although I have the examples above, I am still not entirelly sure how to go about this, having never done anything like it before. Or alternatively, do you have any examples you can give me/show me a tiny but of my code in CSV format? Sorry if that sounds bad, just the best way I learn is seeing examples, so having a little bit of my code (as I know what it does) is CSV format would be a good reference point for me to continue.

THanks Again

The CSV format just means Comma-Separated Values. A typical CSV file would look like this:

x, x2, x3
1.0, 1.0, 1.0,
2.0, 4.0, 8.0,
3.0, 9.0, 27.0
4.0, 16.0, 64.0

It's that simple. Just the values, separated with commas. Excel can also import any other similar thing, like values separated by spaces or tabs. Here's some simple code to generate a CSV:


int main() {
  std::ofstream file_out("my_test.csv");

  file_out << "x, x2, x3" << std:endl;
  for(double x = 1.0; x < 10.1; x += 1.0)
    file_out << x << ", " << (x * x) << ", " << (x * x * x) << std::endl;

  return 0;

Is it possible to write code that will automatically open the file in excel after the program has been run? How about also getting excel to draw a graph using the data in the file?

Is it possible to write code that will automatically open the file in excel

Yes, there are several functions that could be used, one of them is ShellExecute()

How about also getting excel to draw a graph using the data in the file?

I don't know, it's not a c++ question.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.