| | |
Visual c++ code error
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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.
here is my code for the program
and input.txt looks like this
C++ Syntax (Toggle Plain Text)
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
C++ Syntax (Toggle Plain Text)
#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
C++ Syntax (Toggle Plain Text)
10 1 2 6 5 3 4 8 10 7 9 3 2 1 5 6 4 7 10 8 9
~I reject your reality and substitute my own~
0
#2 Nov 10th, 2009
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.
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.
Last edited by JasonHippy; Nov 10th, 2009 at 7:09 pm. Reason: smelling pistakes and typso
If you're into metal, check out my new band at:
http://www.myspace.com/kinasis
Now booking gigs for 2010....
http://www.myspace.com/kinasis
Now booking gigs for 2010....
![]() |
Similar Threads
- Lua in Visual C++ 2008 (C++)
- Mail () function code error need help to check (PHP)
- Please help with my code error (PHP)
- Bluetooth development using platform SDK - error code 10050 (C++)
- Check my code, There is 1 error, cant find it (C++)
- "The Sims" visual C++ runtime error, what shall i do? (Windows Software)
- Visual Runtime Error, Sound Problem, Disabled Norton and more! (Windows NT / 2000 / XP)
- redefinition error please help (C++)
- reading a file into code (Java)
Other Threads in the C++ Forum
- Previous Thread: C++ Music with 'Beep ()' function
- Next Thread: Confused about loop initializations
Views: 288 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll download dynamic encryption error file forms fstream function functions game givemetehcodez google graph gui iamthwee ifstream input int integer java lib library linkedlist linker linux loop looping loops map math matrix memory microsoft newbie news number output parameter pointer problem program programming project python random read recursion recursive reference return sort stream string strings struct studio system template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





