subscript require array or pointer

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

Join Date: Nov 2007
Posts: 219
Reputation: nurulshidanoni is an unknown quantity at this point 
Solved Threads: 0
nurulshidanoni's Avatar
nurulshidanoni nurulshidanoni is offline Offline
Posting Whiz in Training

subscript require array or pointer

 
0
  #1
Apr 16th, 2008
Can anybody help me whis this error? in red colour...

error C2109: subscript requires array or pointer type
error C2109: subscript requires array or pointer type
: error C2109: subscript requires array or pointer type
error C2664: 'void __cdecl std::swap(int &,int &)' : cannot convert parameter 1 from 'int' to 'int &'
A reference that is not to 'const' cannot be bound to


  1. #include <iostream> // std::cout
  2. #include <fstream>
  3. #include <iomanip>
  4. #include <string> // std::string
  5. #include <vector> // std::vector<>
  6. #include <algorithm> //std::for each()
  7. using namespace std; // import "std" namespace into global namespace
  8.  
  9. struct exam
  10. {
  11. int examid;
  12. vector <int> total;
  13. };
  14.  
  15. void SwapMembers (int items[], int index1, int index2)
  16. {
  17. int temp;
  18. temp=items[index1];
  19. items[index1]=items[index2];
  20. items[index2]=temp;
  21. }
  22.  
  23. int main()
  24. {
  25. ifstream stream1("STA83SOLUTION.txt");
  26. if ( !stream1.is_open())
  27. {
  28. cout << "While opening a file an error is encountered" << endl;
  29. }
  30. else
  31. {
  32. cout << "Fail Di buka....." << endl;
  33. }
  34. vector <exam> exams;
  35. exam aExam;
  36. int tempExamID;
  37. int tempTotal;
  38. stream1 >> tempExamID >> tempTotal;
  39. aExam.examid = tempExamID;
  40. aExam.total.push_back(tempTotal); // add this exam code to current student's vector of exam codes
  41. while (stream1 >> tempExamID >> tempTotal)
  42. {
  43. if(tempExamID != aExam.examid)
  44. {
  45. exams.push_back(aExam); // no more exam codes for this student. Add aStudent to students vector
  46. aExam.total.clear();
  47. aExam.examid = tempExamID;
  48. }
  49. aExam.total.push_back(tempTotal); // add this exam code to current student's vector of exam codes
  50. }
  51. exams.push_back(aExam); // no more exam codes for this student. Add aStudent to students vector
  52. stream1.close(); // We have read the entire file, so time to close it.
  53. {
  54. ofstream myfile;
  55. myfile.open("411.txt");
  56. int temp;
  57. if (myfile.is_open())
  58. {
  59. for (size_t i = 0; i < exams.size(); i++)
  60. {
  61. for (size_t j = 0; j<exams.at(i).total.size(); j++)
  62. cout<<"\n"<<i+1<<":"<<" "<< exams.at (i).total.at(j)<<"\t"; // output list of exam codes for this student
  63. }
  64. }
  65.  
  66. cin.get();
  67. return 0;
  68.  
  69. for (size_t i = 0; i < exams.size(); i++)
  70. for (size_t j = 0; j<exams.at(i).total.size(); j++)
  71.  
  72. {
  73.  
  74. int min = i;
  75. for (size_t j = 0; j<exams.at(i).total.size(); j++)
  76. [B][COLOR="Red"]if(exams.at (i).total.at(j)[min]<exams.at (i).total.at(j)[j])[/COLOR][/B]
  77. min = j;
  78. swap(exams.at (i).total.at(j)[min],exams.at (i).total.at(j));
  79. }
  80. for (size_t j = 0; j<exams.at(i).total.size(); j++)
  81. cout << exams.at (i).total.at(j)<<" "<< endl;
  82. }
  83. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 445
Reputation: Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough 
Solved Threads: 69
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro in Training

Re: subscript require array or pointer

 
0
  #2
Apr 16th, 2008
There are some missing '{' brackets after line 69,75,80. Can you try after fixing them. and if that's just a typo then plz paste the corrected version.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 219
Reputation: nurulshidanoni is an unknown quantity at this point 
Solved Threads: 0
nurulshidanoni's Avatar
nurulshidanoni nurulshidanoni is offline Offline
Posting Whiz in Training

Re: subscript require array or pointer

 
0
  #3
Apr 16th, 2008
I have done a correction what you had told but it seems still error again..
error C2143: syntax error : missing ')' before '}'
67) : error C2780: 'void __cdecl std::swap(_Ty &,_Ty &)' : expects 2 arguments - 0 provided
see declaration of 'swap'
syntax error : missing ';' before '}'
: fatal error C1004: unexpected end of file found
412.cpp
: error C2109: subscript requires array or pointer type
subscript requires array or pointer type
subscript requires array or pointer type
: error C2664: 'void __cdecl std::swap(int &,int &)' : cannot convert parameter 1 from 'int' to 'int &'
A reference that is not to 'const' cannot be bound to a non-lvalue
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\412.cpp(82) : error C2374: 'j' : redefinition; multiple initialization
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\412.cpp(76) : see declaration of 'j'
Error executing cl.exe.



  1. #include <iostream> // std::cout
  2. #include <fstream>
  3. #include <iomanip>
  4. #include <string> // std::string
  5. #include <vector> // std::vector<>
  6. #include <algorithm> //std::for each()
  7. using namespace std; // import "std" namespace into global namespace
  8.  
  9. struct exam
  10. {
  11. int examid;
  12. vector <int> total;
  13. };
  14.  
  15. void SwapMembers (int items[], int index1, int index2)
  16. {
  17. int temp;
  18. temp=items[index1];
  19. items[index1]=items[index2];
  20. items[index2]=temp;
  21. }
  22.  
  23. int main()
  24. {
  25. ifstream stream1("STA83SOLUTION.txt");
  26. if ( !stream1.is_open())
  27. {
  28. cout << "While opening a file an error is encountered" << endl;
  29. }
  30. else
  31. {
  32. cout << "Fail Di buka....." << endl;
  33. }
  34. vector <exam> exams;
  35. exam aExam;
  36. int tempExamID;
  37. int tempTotal;
  38. stream1 >> tempExamID >> tempTotal;
  39. aExam.examid = tempExamID;
  40. aExam.total.push_back(tempTotal); // add this exam code to current student's vector of exam codes
  41. while (stream1 >> tempExamID >> tempTotal)
  42. {
  43. if(tempExamID != aExam.examid)
  44. {
  45. exams.push_back(aExam); // no more exam codes for this student. Add aStudent to students vector
  46. aExam.total.clear();
  47. aExam.examid = tempExamID;
  48. }
  49. aExam.total.push_back(tempTotal); // add this exam code to current student's vector of exam codes
  50. }
  51. exams.push_back(aExam); // no more exam codes for this student. Add aStudent to students vector
  52. stream1.close(); // We have read the entire file, so time to close it.
  53. {
  54. ofstream myfile;
  55. myfile.open("411.txt");
  56. int temp;
  57. if (myfile.is_open())
  58. {
  59. for (size_t i = 0; i < exams.size(); i++)
  60. {
  61. for (size_t j = 0; j<exams.at(i).total.size(); j++)
  62. cout<<"\n"<<i+1<<":"<<" "<< exams.at (i).total.at(j)<<"\t"; // output list of exam codes for this student
  63. }
  64. }
  65.  
  66. cin.get();
  67. return 0;
  68.  
  69. for (size_t i = 0; i < exams.size(); i++)
  70. {
  71. for (size_t j = 0; j<exams.at(i).total.size(); j++)
  72.  
  73. {
  74.  
  75. int min = i;
  76. for (size_t j = 0; j<exams.at(i).total.size(); j++)
  77. {
  78. if(exams.at (i).total.at(j)[min]<exams.at (i).total.at(j)[j])
  79. min = j;
  80. swap(exams.at (i).total.at(j)[min],exams.at (i).total.at(j));
  81. }
  82. for (size_t j = 0; j<exams.at(i).total.size(); j++)
  83. {
  84. cout << exams.at (i).total.at(j)<<" "<< endl;
  85. }
  86. }
  87. }
  88. }
  89. }
Last edited by nurulshidanoni; Apr 16th, 2008 at 1:29 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,678
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: subscript require array or pointer

 
0
  #4
Apr 16th, 2008
swap(exams.at (i).total.at(j)[min],exams.at (i).total.at(j)); What's the [min] doing?
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 219
Reputation: nurulshidanoni is an unknown quantity at this point 
Solved Threads: 0
nurulshidanoni's Avatar
nurulshidanoni nurulshidanoni is offline Offline
Posting Whiz in Training

Re: subscript require array or pointer

 
0
  #5
Apr 16th, 2008
min means to get the minimum
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,678
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: subscript require array or pointer

 
0
  #6
Apr 16th, 2008
you set min to be the value of j, so that statement is equivalent to exams.at (i).total[j][j] , but .total is not a two dimensional array, is it? That's what the error C2109 is about, you're putting an excess array index on this expression.
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 219
Reputation: nurulshidanoni is an unknown quantity at this point 
Solved Threads: 0
nurulshidanoni's Avatar
nurulshidanoni nurulshidanoni is offline Offline
Posting Whiz in Training

Re: subscript require array or pointer

 
0
  #7
Apr 16th, 2008
I want to sort the number in descending order...but how to declare a exams.at (i).total.at(j)" in if statement?



  1. #include <iostream> // std::cout
  2. #include <fstream>
  3. #include <iomanip>
  4. #include <string> // std::string
  5. #include <vector> // std::vector<>
  6. #include <algorithm> //std::for each()
  7. using namespace std; // import "std" namespace into global namespace
  8.  
  9. struct exam
  10. {
  11. int examid;
  12. vector <int> total;
  13. };
  14.  
  15. void SwapMembers (int items[], int index1, int index2)
  16. {
  17. int temp;
  18. temp=items[index1];
  19. items[index1]=items[index2];
  20. items[index2]=temp;
  21. }
  22.  
  23. int main()
  24. {
  25. ifstream stream1("STA83SOLUTION.txt");
  26. if ( !stream1.is_open())
  27. {
  28. cout << "While opening a file an error is encountered" << endl;
  29. }
  30. else
  31. {
  32. cout << "Fail Di buka....." << endl;
  33. }
  34. vector <exam> exams;
  35. exam aExam;
  36. int tempExamID;
  37. int tempTotal;
  38. stream1 >> tempExamID >> tempTotal;
  39. aExam.examid = tempExamID;
  40. aExam.total.push_back(tempTotal); // add this exam code to current student's vector of exam codes
  41. while (stream1 >> tempExamID >> tempTotal)
  42. {
  43. if(tempExamID != aExam.examid)
  44. {
  45. exams.push_back(aExam); // no more exam codes for this student. Add aStudent to students vector
  46. aExam.total.clear();
  47. aExam.examid = tempExamID;
  48. }
  49. aExam.total.push_back(tempTotal); // add this exam code to current student's vector of exam codes
  50. }
  51. exams.push_back(aExam); // no more exam codes for this student. Add aStudent to students vector
  52. stream1.close(); // We have read the entire file, so time to close it.
  53. {
  54. ofstream myfile;
  55. myfile.open("411.txt");
  56.  
  57. if (myfile.is_open())
  58. {
  59. for (size_t i = 0; i < exams.size(); i++)
  60. {
  61. for (size_t j = 0; j<exams.at(i).total.size(); j++)
  62. cout<<"\n"<<i+1<<":"<<" "<< exams.at (i).total.at(j)<<"\t"; // output list of exam codes for this student
  63. if( j< j-1)
  64. swap(j,j+1);
  65. cout<<" "<< exams.at (i).total.at(j);
  66. }
  67. }
  68.  
  69. cin.get();
  70. return 0;
  71. }
  72. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 445
Reputation: Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough 
Solved Threads: 69
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro in Training

Re: subscript require array or pointer

 
0
  #8
Apr 16th, 2008
Originally Posted by nurulshidanoni View Post
I want to sort the number in descending order...but how to declare a exams.at (i).total.at(j)" in if statement?
> you mean

if((exams.at (i).total.at(j)) < somthing)
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 219
Reputation: nurulshidanoni is an unknown quantity at this point 
Solved Threads: 0
nurulshidanoni's Avatar
nurulshidanoni nurulshidanoni is offline Offline
Posting Whiz in Training

Re: subscript require array or pointer

 
0
  #9
Apr 16th, 2008
yess.....the outpu is like this...

1: 25
2: 20
3: 46
4: 56
5: 12
6: 22

and i want to sort in descending order
4: 56
3: 46
1: 25
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 445
Reputation: Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough 
Solved Threads: 69
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro in Training

Re: subscript require array or pointer

 
0
  #10
Apr 16th, 2008
you can use any of the sorting algorithms. Try bubble sort, it's one of the easiest and since the number of elements is not too large you dont need to worry about performance.

before that you need to fix some other errors in your code too. within nested for loops that counter you use should have different names. all your nested loops have 'j'.
Last edited by Agni; Apr 16th, 2008 at 3:38 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC