Hello. I'm pretty new to debugging with visual c++. I've tried reading some tutorials but none of them seem to cover what my program is doing. i enter the command arguements for my code which is the file ./input.txt. I then get the following error.

1>c:\documents and settings\jennifer\my documents\visual studio 2008\projects\binarytree\binarytree\input.txt(1) : error C2059: syntax error : 'constant'

here is my code for the program

#include <iostream>
#include <vector>
#include <fstream>

using namespace std;

int reconstruct(vector<int> const& inorder, vector<int> const& preorder, vector<pair<int, int> >& result, int l, int r, int& index)
{
    if (l > r || index >= preorder.size())
    {
        if (l > r)
            --index;
        return 0;
    }

  if (l == r)
        return inorder[l];

    int m;
    for (m = l; m <= r; ++m)
        if (inorder[m] == preorder[index])
            break;

    int root = preorder[index];
    // searches for root's childs
    int left = reconstruct(inorder, preorder, result, l, m - 1, ++index);
    int right = reconstruct(inorder, preorder, result, m + 1, r, ++index);
    result[root] = make_pair(left, right);
    return root;
}

// makes result in level traversal
void makeResult(int root, vector<pair<int, int> > const& tree,
                                 vector<vector<int> >& result, int height)

{
  if (root == 0)
        return;

    result[height].push_back(root);
    makeResult(tree[root].first, tree, result, height + 1);
    makeResult(tree[root].second, tree, result, height + 1);
}

int main(int argc, char* argv[])
{
  ifstream input(argv[1]);
    if (!input)
    {
        cout << "Can't open input file: " << argv[1] << "\n";
        return 1;
    }

    int n;
    input >> n;

    vector<int> inorder(n), preorder(n);
    for (int i = 0; i < n; ++i)
        input >> inorder[i];
    for (int i = 0; i < n; ++i)
        input >> preorder[i];

    int index = 0;
    vector<pair<int, int> > tree(n + 1);
    vector<vector<int> >    result(n);

    // recontsruct tree
    int root = reconstruct(inorder, preorder, tree, 0, n - 1, index);

    // make level traversal output
  makeResult(root, tree, result, 0);
    // output final result level by level
    for (int i = 0; i < n; ++i)
{
        vector<int>& level = result[i];
        // output all nodes at level i
        for (int j = 0; j < level.size(); ++j)
        {
            cout << level[j] << " " << tree[level[j]].first << " " << tree[level[j]].second << "\n";
        }
    }
    return 0;
}

and input.txt looks like this

10 
1     
2 
6 
5 
3 
4 
8 
10 
7 
9 
3 
2 
1 
5 
6 
4 
7 
10 
8 
9

The thing that's confusing me is how you're getting a syntax error from a data-file!!

How are you running the program and passing the parameters? Are you running it from the command line? or from inside the VS2008 IDE?

I've just compiled and tested your code in g++ / codeblocks on Linux and ran your program from both the command line and from the IDE and it accepts the data file and runs without error.

I don't have a windows machine in front of me so I can't fire up VS and test it. I'm at home ATM and although we do have a windows pc in our very cold kitchen; Brrrr, I'm not going out there! heh heh!

Have you tried running it from the command line?
1. Open up a command window
2. Navigate to the folder with your .exe in it and enter the following line:
yourprogram.exe input.txt
(obviously replace yourprogram.exe with the name of your .exe!)

Otherwise, if you're trying to run the program from the VS2008 IDE, Somewhere in the project properties page on the debug tab/page, you can set any command line parameters.
If this is what you're already doing, perhaps try changing the value to input.txt instead of using ./input.txt.

Other than that, I'm not really sure what the problem can be!
The fact that the error mentions a syntax error leads me to believe that the compiler is attempting to compile your text file as a part of the project, rather than taking it as a parameter and loading its contents into your .exe at runtime.
Any chance you could build your program and then post the full log from the compilation?
If the error you've posted is appearing at compile time, it may be worth ensuring that the file hasn't been mistakenly included in the project and treated by the compiler as a source file.

Cheers for now,
Jas.

Edited 7 Years Ago by JasonHippy: smelling pistakes and typso

This article has been dead for over six months. Start a new discussion instead.