Visual c++ code error

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2008
Posts: 3
Reputation: evilsithgirl is an unknown quantity at this point 
Solved Threads: 0
evilsithgirl's Avatar
evilsithgirl evilsithgirl is offline Offline
Newbie Poster

Visual c++ code error

 
0
  #1
29 Days Ago
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.  
  2. 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



  1. #include <iostream>
  2. #include <vector>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. int reconstruct(vector<int> const& inorder, vector<int> const& preorder, vector<pair<int, int> >& result, int l, int r, int& index)
  8. {
  9. if (l > r || index >= preorder.size())
  10. {
  11. if (l > r)
  12. --index;
  13. return 0;
  14. }
  15.  
  16. if (l == r)
  17. return inorder[l];
  18.  
  19. int m;
  20. for (m = l; m <= r; ++m)
  21. if (inorder[m] == preorder[index])
  22. break;
  23.  
  24. int root = preorder[index];
  25. // searches for root's childs
  26. int left = reconstruct(inorder, preorder, result, l, m - 1, ++index);
  27. int right = reconstruct(inorder, preorder, result, m + 1, r, ++index);
  28. result[root] = make_pair(left, right);
  29. return root;
  30. }
  31.  
  32. // makes result in level traversal
  33. void makeResult(int root, vector<pair<int, int> > const& tree,
  34. vector<vector<int> >& result, int height)
  35.  
  36. {
  37. if (root == 0)
  38. return;
  39.  
  40. result[height].push_back(root);
  41. makeResult(tree[root].first, tree, result, height + 1);
  42. makeResult(tree[root].second, tree, result, height + 1);
  43. }
  44.  
  45. int main(int argc, char* argv[])
  46. {
  47. ifstream input(argv[1]);
  48. if (!input)
  49. {
  50. cout << "Can't open input file: " << argv[1] << "\n";
  51. return 1;
  52. }
  53.  
  54. int n;
  55. input >> n;
  56.  
  57. vector<int> inorder(n), preorder(n);
  58. for (int i = 0; i < n; ++i)
  59. input >> inorder[i];
  60. for (int i = 0; i < n; ++i)
  61. input >> preorder[i];
  62.  
  63. int index = 0;
  64. vector<pair<int, int> > tree(n + 1);
  65. vector<vector<int> > result(n);
  66.  
  67. // recontsruct tree
  68. int root = reconstruct(inorder, preorder, tree, 0, n - 1, index);
  69.  
  70. // make level traversal output
  71. makeResult(root, tree, result, 0);
  72. // output final result level by level
  73. for (int i = 0; i < n; ++i)
  74. {
  75. vector<int>& level = result[i];
  76. // output all nodes at level i
  77. for (int j = 0; j < level.size(); ++j)
  78. {
  79. cout << level[j] << " " << tree[level[j]].first << " " << tree[level[j]].second << "\n";
  80. }
  81. }
  82. return 0;
  83. }

and input.txt looks like this

  1. 10
  2. 1
  3. 2
  4. 6
  5. 5
  6. 3
  7. 4
  8. 8
  9. 10
  10. 7
  11. 9
  12. 3
  13. 2
  14. 1
  15. 5
  16. 6
  17. 4
  18. 7
  19. 10
  20. 8
  21. 9
~I reject your reality and substitute my own~
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 331
Reputation: JasonHippy is just really nice JasonHippy is just really nice JasonHippy is just really nice JasonHippy is just really nice 
Solved Threads: 59
JasonHippy's Avatar
JasonHippy JasonHippy is offline Offline
Posting Whiz
 
0
  #2
28 Days Ago
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.
Last edited by JasonHippy; 28 Days Ago at 7:09 pm. Reason: smelling pistakes and typso
There are 10 types of people in this world.....
Those who understand binary .....
And those who don't!
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC