944,035 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1581
  • C++ RSS
Apr 20th, 2007
0

Help Needed on C++ assignment

Expand Post »
Hi,
Wondering if I could get some help. This program is supposed to :
-Input student data(firstname,lastname,student#, and a vector of Grades(up to 10 grades) with a grade and assignment name(notworking)) from a text file and store it in a vector
-Have a menu system for navigation
-add a student ---Working
-delete a student --- not working
-modify a student --- not working, dont know how to access student and modify the non-working struct vector of Grades, i need help with this
-quit (save vector in text file on quit--this is not working, im not using ofstream right)

Vectors/iterators must be used


Here is my bug ridden code, I know it's crappy, but any help, suggestions, comments, would be greatly appreciated.

Thanks, Dev

C++ Syntax (Toggle Plain Text)
  1.  
  2. //****************
  3. //STUDENT.H
  4. //***************
  5.  
  6. #ifndef _Student_HG_
  7. #define _Student_HG_
  8. #include <string>
  9. #include <vector>
  10.  
  11. using std::string;
  12.  
  13. class Student
  14. {
  15. public:
  16. Student( const string &, const string &, const string &, vector <Grade> &);//i should have an int and assign it to a vector instead of vector here?
  17. ~Student(); //Destructor
  18. void setFirstName( const string & );//set first name
  19. string getFirstName() const;
  20.  
  21. void setLastName( const string & );
  22. string getLastName() const;
  23.  
  24. void setStudentNumber( const string & );
  25. string getStudentNumber() const;
  26.  
  27. void printGrades( vector <Grade> &);
  28.  
  29. //void addGrade
  30. vector <Grade> vecGrade;
  31. private:
  32. string firstName;
  33. string lastName;
  34. string studentNumber;
  35.  
  36.  
  37.  
  38. };
  39. #endif
  40.  
  41.  
  42. //**************************
  43. //STUDENT.CPP
  44. //*********************
  45.  
  46. #include <iostream>
  47. using namespace std;
  48. #include "Student.h"
  49. //, const int &grade
  50.  
  51. struct Grade()
  52. {
  53. string assignmentName;
  54. int assignmentGrade;
  55. }
  56.  
  57. Student::Student( const string &first, const string &last, const string &snum , vector <Grade> &vG)
  58. : firstName( first ), lastName( last ), studentNumber( snum )
  59. {
  60. Grade tempGrade = new Grade;
  61. //start loop to input grades into vector<Grade> NOT WORKING
  62. /*tempGrade.assignmentName = ;
  63.   tempGrade.assignmentGrade = ;
  64.   vecGrade.push_back(tempGrade);*/
  65. }
  66.  
  67. Student::~Student()
  68. {
  69. }
  70.  
  71. void Student::setFirstName( const string &first)
  72. {
  73. firstName = first;
  74. }
  75.  
  76. string Student::getFirstName() const
  77. {
  78. return firstName;
  79. }
  80.  
  81. void Student::setLastName( const string &last)
  82. {
  83. lastName = last;
  84. }
  85.  
  86. string Student::getLastName() const
  87. {
  88. return lastName;
  89. }
  90.  
  91. void Student::setStudentNumber(const string &snum)
  92. {
  93. studentNumber = snum;
  94. }
  95.  
  96. string Student::getStudentNumber() const
  97. {
  98. return studentNumber;
  99. }
  100.  
  101. void Student::printGrades()
  102. {
  103. for (vector<Grade>::iterator it = vecGrade.begin();
  104. it != vecGrade.end(); it++)
  105. {
  106. cout << (*it).assignmentName << " "
  107. << (*it).assignemtnGrade << endl;//DONT KNOW what todo...This doesnt work
  108. }
  109. }
  110.  
  111.  
  112.  
  113. //**************
  114. //Main
  115. //***********
  116.  
  117.  
  118. #include <iostream>
  119. #include <iomanip>
  120. #include <vector>
  121. #include <string>
  122. #include <fstream>
  123. #include "Student.h"
  124.  
  125. using namespace std;
  126.  
  127.  
  128.  
  129.  
  130. int enterChoice()
  131. {
  132. cout << "\nEnter your choice" << endl
  133. << "1 - Add a student" << endl
  134. << "2 - Modify a student" << endl
  135. << "3 - Delete a student" << endl
  136. << "0 - Exit\n" << endl << endl << endl;
  137.  
  138. int menuChoice;
  139. cin >> menuChoice;
  140. return menuChoice;
  141. }
  142.  
  143. int enterModChoice()
  144. {
  145. cout << "\nEnter your choice" << endl
  146. << "1 - Change student's first name" << endl
  147. << "2 - Change student's last name" << endl
  148. << "3 - Change student's student number" << endl
  149. << "4 - change students assignment/grades" << endl
  150. << "0 - Exit\n" << endl << endl << endl;
  151.  
  152. int menuChoice;
  153. cin >> menuChoice;
  154. return menuChoice;
  155. }
  156.  
  157.  
  158.  
  159. void printOut(vector<Student> &vecStud)
  160. {
  161. cout << "Student" << setw(25) << "Grade" <<endl;
  162. for (vector<Student>::iterator it = vecStud.begin();
  163. it != vecStud.end(); it++)
  164. {
  165. cout << (*it).getFirstName() << " " <<
  166. (*it).getLastName() << " :" <<
  167. endl;
  168. }
  169.  
  170. }
  171.  
  172.  
  173. int main()
  174. {
  175. //Load up the student data text file into vector
  176. vector < Student > vecStudent;
  177. ifstream myFile("studentData.txt");
  178. string fname;
  179. string lname;
  180. string ssn;
  181.  
  182. while (myFile >> fname >> lname >> ssn)
  183. {
  184. Student tempStu(fname,lname,ssn);
  185. vecStudent.push_back(tempStu);
  186. }
  187. myFile.close;
  188.  
  189. printOut(vecStudent);
  190.  
  191.  
  192. int choice;
  193. int choice2;
  194. string result;
  195. while ( ( choice = enterChoice() ) != 0)
  196. {
  197.  
  198. switch ( choice)
  199. {
  200. case 1:
  201. {//add a student
  202.  
  203. cout << endl << endl << endl;
  204. cout << "Adding student...." << endl << endl << endl;
  205. cout << "What is the student's first name? : ";
  206. cin >> fname;
  207. cout << endl << "What is the student's last name? : ";
  208. cin >> lname;
  209. cout << endl << "What is the student's student number? : ";
  210. cin >> ssn;
  211. Student tempStu(fname,lname,ssn);
  212.  
  213.  
  214. vecStudent.push_back(tempStu);
  215.  
  216. //How many assignments/grades do you want to put in?
  217. //loop inputting grades into Student grade vector
  218.  
  219. printOut(vecStudent);
  220. break;
  221. }
  222. case 2:
  223. {
  224. //modify and student
  225. cout << "Modifing a student......" << endl << endl << endl;
  226. cout << "What is the student's student number\n who you want to modify?" << endl << endl;
  227. cin >> ssn;
  228.  
  229. //search for student
  230. for (vector<Student>::iterator it = vecStudent.begin();
  231. it != vecStudent.end(); it++)
  232. {
  233. //THIS DOESNT WORK*******
  234. if (!strcmp((*it).getStudentNumber, ssn))
  235. {
  236. cout << "Student found......" << endl;
  237.  
  238. //display menu for changing a student's stats
  239. while ( ( choice2 = enterModChoice() ) != 0)
  240. {
  241. switch ( choice2 )
  242. {
  243. case 1:
  244. //set/change students first name
  245. cout << "Enter student's new first name: ";
  246. cin >> result;
  247. (*it).setFirstName( result );
  248. break;
  249. case 2:
  250. //set/change student's last name
  251. cout << "Enter student's new last name: ";
  252. cin >> result;
  253. (*it).setLastName( result );
  254. break;
  255. case 3:
  256. //set/change student's student number
  257. cout << "Enter student's new student number: ";
  258. cin >> result;
  259. (*it).setStudentNumber( result );
  260. break;
  261. case 4:
  262. //print off assignment list (vector of struct Grade's inside Student) w/grades
  263. (*it).printGrades();
  264. //Bring up another menu for what assignment to change
  265.  
  266. //enter assignemnt # 1-10
  267. // change 1: name or 2: grade
  268. // if
  269. break;
  270. case 0:
  271.  
  272. break;
  273. default:
  274. cout << "You entered a wrong number" << endl;
  275. break;
  276. }
  277. }
  278. }
  279.  
  280.  
  281. else
  282. {
  283. cout << "Sorry, either the number was incorrectly inputted"
  284. << "\n or the student does not exist." << endl;
  285.  
  286. }
  287.  
  288. }
  289. break;
  290. }
  291. case 3:
  292. {
  293. //delete a student
  294. cout << "Deleting a student......" << endl << endl << endl;
  295. cout << "What is the student's student number\n who you want to delete?" << endl << endl;
  296. cin >> ssn;
  297.  
  298. for (vector<Student>::iterator it = vecStudent.begin();
  299. it != vecStudent.end(); it++)
  300. {
  301. //THIS DOESNT WORK
  302. if ( !strcmp((*it).getStudentNumber, ssn))
  303. {
  304. cout << "Sorry, either the number was incorrectly inputted"
  305. << "\n or the student does not exist." << endl;
  306. }
  307. else
  308. {
  309. cout << "Student found......" << endl;
  310. cout << "Deleting...." << endl;
  311.  
  312. //DOESNT WORK, i guess i have to delete it from the vector somehow aswell?
  313. delete (*it);
  314. }
  315. }
  316. break;
  317. }
  318. case 0:
  319. {
  320. //exit
  321.  
  322.  
  323. //save Student vector to file, outputting everything
  324.  
  325. ofstream myFile("studentData.txt");
  326. myFile.trunc;
  327.  
  328. for (vector<Student>::iterator it = vecStudent.begin();
  329. it != vecStudent.end(); it++)
  330. {
  331. myFile >> (*it).getFirstName() >> " " >>
  332. (*it).getLastName() >> " :" >> endl;
  333. //print out grades
  334. myFile >>(*it).printGrades() >> endl;
  335. }
  336. myFile.close;
  337.  
  338. return 0;
  339. }
  340. default:
  341. {//error msg,
  342. cout << "You have put in an incorrect choice" << endl;
  343. printOut();
  344. }
  345. break;
  346. }
  347.  
  348. }
  349.  
  350.  
  351.  
  352.  
  353.  
  354. return 0;
  355. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Devster is offline Offline
4 posts
since Apr 2007
Apr 20th, 2007
0

Re: Help Needed on C++ assignment

>Here is my bug ridden code
Bug-ridden? It won't even compile!

There's way too many for me to fix now, how about posting the error messages you receive so other people can help you find what is making them occur?
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Apr 20th, 2007
0

Re: Help Needed on C++ assignment

OKay , here is the udpated code, a bit more sorted out, just a few problems generating 21 errors.

First of all I'm not using the right fstream commands or something for output to a text file. Please help. (line 389-402)

Second, when doing a comparison of strings, like "if ( ((*it).getStudentNumber) == ssn)" doesnt work, how do i do this properly?
(line 268 etc)

Lastly, how do i properly delete a student from the vector of students? "delete (*it);" not working. (line 377)

Also, am i declaring the struct properly in the student.h?
Also, what am i forgetting with my printGrade function for student, it doesnt work, here is my list of errors the compiler throws at me.

1>c:\documents and settings\dev\my documents\c++\p4\main\main\main.cpp(147) : error C2784: 'bool std::operator ==(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'overloaded-function'
1> c:\program files\microsoft visual studio 8\vc\include\string(91) : see declaration of 'std::operator =='
1>c:\documents and settings\dev\my documents\c++\p4\main\main\main.cpp(147) : error C2784: 'bool std::operator ==(const _Elem *,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const _Elem *' from 'overloaded-function'
1> c:\program files\microsoft visual studio 8\vc\include\string(81) : see declaration of 'std::operator =='
1>c:\documents and settings\dev\my documents\c++\p4\main\main\main.cpp(147) : error C2784: 'bool std::operator ==(const std::basic_string<_Elem,_Traits,_Alloc> &,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'overloaded-function'
1> c:\program files\microsoft visual studio 8\vc\include\string(71) : see declaration of 'std::operator =='
1>c:\documents and settings\dev\my documents\c++\p4\main\main\main.cpp(147) : error C2784: 'bool std::operator ==(const std::vector<_Ty,_Alloc> &,const std::vector<_Ty,_Alloc> &)' : could not deduce template argument for 'const std::vector<_Ty,_Alloc> &' from 'overloaded-function'
1> c:\program files\microsoft visual studio 8\vc\include\vector(1259) : see declaration of 'std::operator =='
1>c:\documents and settings\dev\my documents\c++\p4\main\main\main.cpp(147) : error C2784: 'bool std::operator ==(const std::istream_iterator<_Ty,_Elem,_Traits,_Diff> &,const std::istream_iterator<_Ty,_Elem,_Traits,_Diff> &)' : could not deduce template argument for 'const std::istream_iterator<_Ty,_Elem,_Traits,_Diff> &' from 'overloaded-function'
1> c:\program files\microsoft visual studio 8\vc\include\iterator(266) : see declaration of 'std::operator =='
1>c:\documents and settings\dev\my documents\c++\p4\main\main\main.cpp(147) : error C2784: 'bool std::operator ==(const std::allocator<_Ty> &,const std::allocator<_Other> &) throw()' : could not deduce template argument for 'const std::allocator<_Ty> &' from 'overloaded-function'
1> c:\program files\microsoft visual studio 8\vc\include\xmemory(174) : see declaration of 'std::operator =='
1>c:\documents and settings\dev\my documents\c++\p4\main\main\main.cpp(147) : error C2784: 'bool std::operator ==(const std::istreambuf_iterator<_Elem,_Traits> &,const std::istreambuf_iterator<_Elem,_Traits> &)' : could not deduce template argument for 'const std::istreambuf_iterator<_Elem,_Traits> &' from 'overloaded-function'
1> c:\program files\microsoft visual studio 8\vc\include\xutility(2143) : see declaration of 'std::operator =='
1>c:\documents and settings\dev\my documents\c++\p4\main\main\main.cpp(147) : error C2784: 'bool std::operator ==(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'overloaded-function'
1> c:\program files\microsoft visual studio 8\vc\include\xutility(1826) : see declaration of 'std::operator =='
1>c:\documents and settings\dev\my documents\c++\p4\main\main\main.cpp(147) : error C2784: 'bool std::operator ==(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std::pair<_Ty1,_Ty2> &' from 'overloaded-function'
1> c:\program files\microsoft visual studio 8\vc\include\utility(60) : see declaration of 'std::operator =='
1>c:\documents and settings\dev\my documents\c++\p4\main\main\main.cpp(147) : error C2677: binary '==' : no global operator found which takes type 'std::string' (or there is no acceptable conversion)
1>c:\documents and settings\dev\my documents\c++\p4\main\main\main.cpp(245) : error C2784: 'bool std::operator ==(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'overloaded-function'
1> c:\program files\microsoft visual studio 8\vc\include\string(91) : see declaration of 'std::operator =='
1>c:\documents and settings\dev\my documents\c++\p4\main\main\main.cpp(245) : error C2784: 'bool std::operator ==(const _Elem *,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const _Elem *' from 'overloaded-function'
1> c:\program files\microsoft visual studio 8\vc\include\string(81) : see declaration of 'std::operator =='
1>c:\documents and settings\dev\my documents\c++\p4\main\main\main.cpp(245) : error C2784: 'bool std::operator ==(const std::basic_string<_Elem,_Traits,_Alloc> &,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'overloaded-function'
1> c:\program files\microsoft visual studio 8\vc\include\string(71) : see declaration of 'std::operator =='
1>c:\documents and settings\dev\my documents\c++\p4\main\main\main.cpp(245) : error C2784: 'bool std::operator ==(const std::vector<_Ty,_Alloc> &,const std::vector<_Ty,_Alloc> &)' : could not deduce template argument for 'const std::vector<_Ty,_Alloc> &' from 'overloaded-function'
1> c:\program files\microsoft visual studio 8\vc\include\vector(1259) : see declaration of 'std::operator =='
1>c:\documents and settings\dev\my documents\c++\p4\main\main\main.cpp(245) : error C2784: 'bool std::operator ==(const std::istream_iterator<_Ty,_Elem,_Traits,_Diff> &,const std::istream_iterator<_Ty,_Elem,_Traits,_Diff> &)' : could not deduce template argument for 'const std::istream_iterator<_Ty,_Elem,_Traits,_Diff> &' from 'overloaded-function'
1> c:\program files\microsoft visual studio 8\vc\include\iterator(266) : see declaration of 'std::operator =='
1>c:\documents and settings\dev\my documents\c++\p4\main\main\main.cpp(245) : error C2784: 'bool std::operator ==(const std::allocator<_Ty> &,const std::allocator<_Other> &) throw()' : could not deduce template argument for 'const std::allocator<_Ty> &' from 'overloaded-function'
1> c:\program files\microsoft visual studio 8\vc\include\xmemory(174) : see declaration of 'std::operator =='
1>c:\documents and settings\dev\my documents\c++\p4\main\main\main.cpp(245) : error C2784: 'bool std::operator ==(const std::istreambuf_iterator<_Elem,_Traits> &,const std::istreambuf_iterator<_Elem,_Traits> &)' : could not deduce template argument for 'const std::istreambuf_iterator<_Elem,_Traits> &' from 'overloaded-function'
1> c:\program files\microsoft visual studio 8\vc\include\xutility(2143) : see declaration of 'std::operator =='
1>c:\documents and settings\dev\my documents\c++\p4\main\main\main.cpp(245) : error C2784: 'bool std::operator ==(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'overloaded-function'
1> c:\program files\microsoft visual studio 8\vc\include\xutility(1826) : see declaration of 'std::operator =='
1>c:\documents and settings\dev\my documents\c++\p4\main\main\main.cpp(245) : error C2784: 'bool std::operator ==(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std::pair<_Ty1,_Ty2> &' from 'overloaded-function'
1> c:\program files\microsoft visual studio 8\vc\include\utility(60) : see declaration of 'std::operator =='
1>c:\documents and settings\dev\my documents\c++\p4\main\main\main.cpp(245) : error C2677: binary '==' : no global operator found which takes type 'std::string' (or there is no acceptable conversion)
1>c:\documents and settings\dev\my documents\c++\p4\main\main\main.cpp(256) : error C2440: 'delete' : cannot convert from 'Student' to 'void *'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>Student.cpp
1>Generating Code...
1>c:\documents and settings\dev\my documents\c++\p4\main\main\student.cpp(61) : warning C4715: 'Student::printGrades' : not all control paths return a value






C++ Syntax (Toggle Plain Text)
  1.  
  2. //******************
  3. //STUDENT.h
  4. //**************
  5. #ifndef _Student_HG_
  6. #define _Student_HG_
  7. #include <string>
  8. #include <vector>
  9.  
  10. using std::string;
  11.  
  12. class Student
  13. {
  14. public:
  15. Student( const string &, const string &, const string & );//i should have an int and assign it to a vector instead of vector here?
  16. ~Student(); //Destructor
  17. void setFirstName( const string & );//set first name
  18. string getFirstName() const;
  19.  
  20. void setLastName( const string & );
  21. string getLastName() const;
  22.  
  23. void setStudentNumber( const string & );
  24. string getStudentNumber() const;
  25.  
  26. string printGrades() ;
  27.  
  28. struct Grade
  29. {
  30. string assignmentName;
  31. int assignmentGrade;
  32. };
  33. //void addGrade
  34. //vector <Grade> vecGrade;
  35. std::vector<Grade> vecGrade;
  36.  
  37.  
  38. private:
  39. string firstName;
  40. string lastName;
  41. string studentNumber;
  42.  
  43.  
  44.  
  45. };
  46. #endif
  47.  
  48.  
  49.  
  50.  
  51. //*************
  52. //Student.cpp
  53. //**************
  54.  
  55. #include <iostream>
  56. #include <sstream>
  57. #include <string>
  58. using namespace std;
  59. #include "Student.h"
  60.  
  61. //, const int &grade
  62.  
  63. Student::Student( const string &first, const string &last, const string &snum )
  64. : firstName( first ), lastName( last ), studentNumber( snum )
  65. {
  66. }
  67.  
  68. Student::~Student()
  69. {
  70. }
  71.  
  72. void Student::setFirstName( const string &first)
  73. {
  74. firstName = first;
  75. }
  76.  
  77. string Student::getFirstName() const
  78. {
  79. return firstName;
  80. }
  81.  
  82. void Student::setLastName( const string &last)
  83. {
  84. lastName = last;
  85. }
  86.  
  87. string Student::getLastName() const
  88. {
  89. return lastName;
  90. }
  91.  
  92. void Student::setStudentNumber(const string &snum)
  93. {
  94. studentNumber = snum;
  95. }
  96.  
  97. string Student::getStudentNumber() const
  98. {
  99. return studentNumber;
  100. }
  101.  
  102. string Student::printGrades()
  103. {
  104. std::stringstream ss;
  105. string st;
  106. string s;
  107. for (vector<Grade>::iterator it = vecGrade.begin();
  108. it != vecGrade.end(); it++)
  109. {
  110. ss << (*it).assignmentGrade;
  111. ss >> st;
  112. s = (*it).assignmentName + " " + st + "\n";
  113. return s;
  114. }
  115. }
  116.  
  117.  
  118. //**************
  119. //Main.cpp
  120. //**********
  121.  
  122. #include <iostream>
  123. #include <iomanip>
  124. #include <vector>
  125. #include <string>
  126. #include <sstream>
  127. #include <fstream>
  128. #include "Student.h"
  129.  
  130. using namespace std;
  131.  
  132.  
  133.  
  134.  
  135. int enterChoice()
  136. {
  137. cout << "\nEnter your choice" << endl
  138. << "1 - Add a student" << endl
  139. << "2 - Modify a student" << endl
  140. << "3 - Delete a student" << endl
  141. << "0 - Exit\n" << endl << endl << endl;
  142.  
  143. int menuChoice;
  144. cin >> menuChoice;
  145. return menuChoice;
  146. }
  147.  
  148. int enterModChoice()
  149. {
  150. cout << "\nEnter your choice" << endl
  151. << "1 - Change student's first name" << endl
  152. << "2 - Change student's last name" << endl
  153. << "3 - Change student's student number" << endl
  154. << "4 - change students assignment/grades" << endl
  155. << "0 - Exit\n" << endl << endl << endl;
  156.  
  157. int menuChoice;
  158. cin >> menuChoice;
  159. return menuChoice;
  160. }
  161.  
  162.  
  163.  
  164. void printOut(vector<Student> &vecStud)
  165. {
  166. cout << "Student" << setw(25) << "Grade" <<endl;
  167. for (vector<Student>::iterator it = vecStud.begin();
  168. it != vecStud.end(); it++)
  169. {
  170. cout << (*it).getFirstName() << " " <<
  171. (*it).getLastName() << " :" <<
  172. endl;
  173. }
  174.  
  175. }
  176.  
  177.  
  178. int main()
  179. {
  180. //Load up the student data text file into vector
  181. vector < Student > vecStudent;
  182. ifstream myFile("studentData.txt");
  183. string fname;
  184. string lname;
  185. string ssn;
  186. int numMarks;
  187. string assName;
  188. int assGrade;
  189.  
  190. Student::Grade tempGrade;
  191.  
  192. while (myFile >> fname >> lname >> ssn)
  193. {
  194. Student tempStu(fname,lname,ssn);
  195.  
  196. while (myFile >> assName >> assGrade)
  197. {
  198. tempGrade.assignmentName = assName;
  199. tempGrade.assignmentGrade = assGrade;
  200. tempStu.vecGrade.push_back(tempGrade);
  201. }
  202. vecStudent.push_back(tempStu);
  203. }
  204.  
  205. start:
  206. printOut(vecStudent);
  207.  
  208.  
  209. int choice;
  210. int choice2;
  211. int c;
  212. string result;
  213. while ( ( choice = enterChoice() ) != 0)
  214. {
  215.  
  216. switch ( choice )
  217. {
  218. case 1:
  219. {//add a student
  220.  
  221. cout << endl << endl << endl;
  222. cout << "Adding student...." << endl << endl << endl;
  223. cout << "What is the student's first name? : ";
  224. cin >> fname;
  225. cout << endl << "What is the student's last name? : ";
  226. cin >> lname;
  227. cout << endl << "What is the student's student number? : ";
  228. cin >> ssn;
  229.  
  230. Student tempStu(fname,lname,ssn);
  231.  
  232. cout << endl << "How many assignments do you want to enter /w marks?" << endl;
  233. cin >> c;
  234. for (int x=0; x < c; x++)
  235. {
  236.  
  237. cout << endl << "What's the assignment's title?" << endl;
  238. cin >> assName;
  239. tempGrade.assignmentName = assName;
  240. cout << endl << "What's the assignment's grade?" << endl;
  241. cin >> assGrade;
  242. tempGrade.assignmentGrade = assGrade;
  243. tempStu.vecGrade.push_back(tempGrade);
  244.  
  245. }
  246.  
  247.  
  248. vecStudent.push_back(tempStu);
  249.  
  250. //How many assignments/grades do you want to put in?
  251. //loop inputting grades into Student grade vector
  252.  
  253. printOut(vecStudent);
  254. break;
  255. }
  256. case 2:
  257. {
  258. //modify and student
  259. cout << "Modifing a student......" << endl << endl << endl;
  260. cout << "What is the student's student number\n who you want to modify?" << endl << endl;
  261. cin >> ssn;
  262.  
  263. //search for student
  264. for (vector<Student>::iterator it = vecStudent.begin();
  265. it != vecStudent.end(); it++)
  266. {
  267. //THIS DOESNT WORK*******
  268. if ((*it).getStudentNumber == ssn)
  269. {
  270. cout << "Student found......" << endl;
  271.  
  272. //display menu for changing a student's stats
  273. while ( ( choice2 = enterModChoice() ) != 0)
  274. {
  275. switch ( choice2 )
  276. {
  277. case 1:
  278. //set/change students first name
  279. cout << "Enter student's new first name: ";
  280. cin >> result;
  281. (*it).setFirstName( result );
  282. break;
  283. case 2:
  284. //set/change student's last name
  285. cout << "Enter student's new last name: ";
  286. cin >> result;
  287. (*it).setLastName( result );
  288. break;
  289. case 3:
  290. //set/change student's student number
  291. cout << "Enter student's new student number: ";
  292. cin >> result;
  293. (*it).setStudentNumber( result );
  294. break;
  295. case 4:
  296. //print off assignment list (vector of struct Grade's inside Student) w/grades
  297. cout << (*it).printGrades();
  298. //Bring up another menu for what assignment to change
  299.  
  300. cout << "\nEnter your choice" << endl
  301. << "1 - Modify Grade Name/Mark" << endl
  302. << "0 - Exit\n" << endl << endl << endl;
  303. int cc;
  304. cin >> cc;
  305.  
  306. if (cc=0)
  307. break;
  308. else
  309. {
  310. cout << "which assignment do you want to change? (1-10);" << endl;
  311. cin >> cc;
  312.  
  313. cout << endl << "What do you want the new assignment name to be? :";
  314. cin >> assName;
  315. cout << endl << "what do you want the new assignment grade to be? :";
  316. cin >> assGrade;
  317.  
  318. vector<Student::Grade>::iterator itv = (*it).vecGrade.begin();
  319. itv += cc;
  320.  
  321. (*itv).assignmentName = assName;
  322. (*itv).assignmentGrade = assGrade;
  323. cout << (*it).printGrades();
  324. break;
  325. }
  326.  
  327.  
  328.  
  329. //enter assignemnt # 1-10
  330. // change 1: name or 2: grade
  331. // if
  332. break;
  333. case 0:
  334.  
  335. break;
  336. default:
  337. cout << "You entered a wrong number" << endl;
  338. goto start;
  339. break;
  340. }
  341. }
  342. }
  343.  
  344.  
  345. else
  346. {
  347. cout << "Sorry, either the number was incorrectly inputted"
  348. << "\n or the student does not exist." << endl;
  349. goto start;
  350. }
  351.  
  352. }
  353. break;
  354. }
  355. case 3:
  356. {
  357. //delete a student
  358. cout << "Deleting a student......" << endl << endl << endl;
  359. cout << "What is the student's student number\n who you want to delete?" << endl << endl;
  360. cin >> ssn;
  361.  
  362. for (vector<Student>::iterator it = vecStudent.begin();
  363. it != vecStudent.end(); it++)
  364. {
  365. //THIS DOESNT WORK, how do i compare the strings properly?
  366. if ( ((*it).getStudentNumber) == ssn)
  367. {
  368. cout << "Sorry, either the number was incorrectly inputted"
  369. << "\n or the student does not exist." << endl;
  370. }
  371. else
  372. {
  373. cout << "Student found......" << endl;
  374. cout << "Deleting...." << endl;
  375.  
  376. //DOESNT WORK, i guess i have to delete it from the vector somehow aswell?
  377. delete (*it);
  378. }
  379. }
  380. break;
  381. }
  382. case 0:
  383. {
  384. //exit
  385.  
  386.  
  387. //save Student vector to file, outputting everything
  388.  
  389. fstream myFile("studentData.txt",ios::out);
  390. myFile.trunc;
  391.  
  392. for (vector<Student>::iterator it = vecStudent.begin();
  393. it != vecStudent.end(); it++)
  394. {
  395.  
  396. myFile << (*it).getFirstName() << " " <<
  397. (*it).getLastName() << " " << (*it).getStudentNumber() << endl;
  398. //print out grades
  399. myFile << (*it).printGrades() << endl;
  400.  
  401. }
  402. myFile.close();
  403.  
  404. return 0;
  405. }
  406. default:
  407. {//error msg,
  408. cout << "You have put in an incorrect choice" << endl;
  409. goto start;
  410. }
  411. break;
  412. }
  413.  
  414. }
  415.  
  416.  
  417.  
  418.  
  419.  
  420. return 0;
  421. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Devster is offline Offline
4 posts
since Apr 2007
Apr 20th, 2007
0

Re: Help Needed on C++ assignment

Good job, you've definitely fixed a number of syntax errors already.

Basically 2 things I saw:

C++ Syntax (Toggle Plain Text)
  1. if ((*it).getStudentNumber) == ssn)
Since getStudentNumber is a function, you'll have to put () after the function name. Otherwise you're comparing function pointers, which is something entirely different.

Using delete to deallocate a vector? If you need to empty it, you can always use vector::clear, but the destructor of std::vector automatically takes care of this, so no worries here.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Apr 21st, 2007
0

Re: Help Needed on C++ assignment

But if you still want to use delete (which would be a good thing), consider dynamically allocating the vector using the new operator.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 720
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Apr 21st, 2007
0

Re: Help Needed on C++ assignment

Okay, Got everything error free , thanks alot, however there is some runtime difficulties.

First, and hopefully only, when I load up the student and the grades, it doesnt work :p it worked initially, but now when it displays the student when it first loads and displays the students, its not workin. So any help, much appreciated....

//*******************
//studentData.txt example
//*******************
Devin Grove 222-234-567
history111 85
calculus222 99

Kath Puza 345-678-665
history111 85
calculus222 99

Tom Geonger 325-235-256
history111 85
calculus222 99
history111 85

Dude Willis 123-157-643
history111 85
calculus222 99

Oscar Myer 305-246-789
history111 85
calculus222 99

Julia Wicchaa 242-657-543
history111 85
calculus222 99
calculus222 99

 
//***********
//student.h
 
#ifndef _Student_HG_
#define _Student_HG_
#include<string>
#include<vector>
using std::string;
class Student 
{
public:
Student( const string &, const string &, const string & );//i should have an int and assign it to a vector instead of vector here?
~Student(); //Destructor
void setFirstName( const string & );//set first name
string getFirstName() const;
void setLastName( const string & );
string getLastName() const;
void setStudentNumber( const string & );
string getStudentNumber() const;
string printGrades() ;
 
struct Grade
{
string assignmentName;
int assignmentGrade;
};
//void addGrade
//vector <Grade> vecGrade;
std::vector<Grade> vecGrade;
 
private:
string firstName;
string lastName;
string studentNumber;
 
 
};
#endif



//*************
//Student.cpp
//*************
#include<iostream>
#include<sstream>
#include<string>
usingnamespace std;
#include"Student.h"
//, const int &grade
Student::Student( const string &first, const string &last, const string &snum )
: firstName( first ), lastName( last ), studentNumber( snum )
{
}
Student::~Student()
{
}
void Student::setFirstName( const string &first)
{
firstName = first;
}
string Student::getFirstName() const
{
return firstName;
}
void Student::setLastName( const string &last)
{
lastName = last;
}
string Student::getLastName() const
{
return lastName;
}
void Student::setStudentNumber(const string &snum)
{
studentNumber = snum;
}
string Student::getStudentNumber() const
{
return studentNumber;
}
string Student::printGrades()
{
std::stringstream ss;
string st;
string s;
for (vector<Grade>::iterator it = vecGrade.begin();
it != vecGrade.end(); it++)
{
ss << (*it).assignmentGrade;
ss >> st;
s = (*it).assignmentName + " " + st + "\n";
return s;
}
 
 
//*************************
//main.cpp
//**************************
#include<iostream>
#include<iomanip>
#include<vector>
#include<string>
#include<sstream>
#include<fstream>
#include"Student.h"
usingnamespace std;
 
 
 
int enterChoice()
{
cout << "\nEnter your choice" << endl
<< "1 - Add a student" << endl
<< "2 - Modify a student" << endl
<< "3 - Delete a student" << endl
<< "0 - Exit\n" << endl << endl << endl;
 
int menuChoice;
cin >> menuChoice;
return menuChoice;
}
int enterModChoice()
{
cout << "\nEnter your choice" << endl
<< "1 - Change student's first name" << endl
<< "2 - Change student's last name" << endl
<< "3 - Change student's student number" << endl
<< "4 - change students assignment/grades" << endl
<< "0 - Exit\n" << endl << endl << endl;
 
int menuChoice;
cin >> menuChoice;
return menuChoice;
}
 
 
void printOut(vector<Student> &vecStud)
{
cout << "Student" << setw(25) << "Grade" <<endl;
for (vector<Student>::iterator it = vecStud.begin();
it != vecStud.end(); it++)
{
cout << (*it).getFirstName() << " " <<
(*it).getLastName() << " :" <<
endl;
}
}
 
int main()
{
//Load up the student data text file into vector
vector < Student > vecStudent;
ifstream myFile("studentData.txt");
string fname;
string lname;
string ssn;
int numMarks;
string assName;
int assGrade;
 
Student::Grade tempGrade;
while (myFile >> fname >> lname >> ssn)
{
Student tempStu(fname,lname,ssn);
 
while (myFile >> assName >> assGrade)
{
tempGrade.assignmentName = assName;
tempGrade.assignmentGrade = assGrade;
tempStu.vecGrade.push_back(tempGrade);
}
vecStudent.push_back(tempStu);
}
start:
printOut(vecStudent);
 
int choice;
int choice2;
int c;
string result;
while ( ( choice = enterChoice() ) != 0)
{
 
switch ( choice )
{
case 1:
{//add a student

cout << endl << endl << endl;
cout << "Adding student...." << endl << endl << endl;
cout << "What is the student's first name? : ";
cin >> fname;
cout << endl << "What is the student's last name? : ";
cin >> lname;
cout << endl << "What is the student's student number? : ";
cin >> ssn;
Student tempStu(fname,lname,ssn);
cout << endl << "How many assignments do you want to enter /w marks?" << endl;
cin >> c;
for (int x=0; x < c; x++)
{
cout << endl << "What's the assignment's title?" << endl;
cin >> assName;
tempGrade.assignmentName = assName;
cout << endl << "What's the assignment's grade?" << endl;
cin >> assGrade;
tempGrade.assignmentGrade = assGrade;
tempStu.vecGrade.push_back(tempGrade);
}
 
vecStudent.push_back(tempStu);
//How many assignments/grades do you want to put in?
//loop inputting grades into Student grade vector
printOut(vecStudent);
break;
}
case 2:
{
//modify and student
cout << "Modifing a student......" << endl << endl << endl;
cout << "What is the student's student number\n who you want to modify?" << endl << endl;
cin >> ssn;
 
//search for student
for (vector<Student>::iterator it = vecStudent.begin();
it != vecStudent.end(); it++)
{
//THIS DOESNT WORK*******
if ((*it).getStudentNumber() == ssn)
{
cout << "Student found......" << endl;
 
//display menu for changing a student's stats
while ( ( choice2 = enterModChoice() ) != 0)
{
switch ( choice2 )
{
case 1:
//set/change students first name
cout << "Enter student's new first name: ";
cin >> result;
(*it).setFirstName( result );
break;
case 2:
//set/change student's last name
cout << "Enter student's new last name: ";
cin >> result;
(*it).setLastName( result );
break;
case 3:
//set/change student's student number
cout << "Enter student's new student number: ";
cin >> result;
(*it).setStudentNumber( result );
break;
case 4:
//print off assignment list (vector of struct Grade's inside Student) w/grades
cout << (*it).printGrades();
//Bring up another menu for what assignment to change

cout << "\nEnter your choice" << endl
<< "1 - Modify Grade Name/Mark" << endl
<< "0 - Exit\n" << endl << endl << endl;
int cc;
cin >> cc;
if (cc=0)
break;
else
{
cout << "which assignment do you want to change? (1-10);" << endl;
cin >> cc;
cout << endl << "What do you want the new assignment name to be? :";
cin >> assName;
cout << endl << "what do you want the new assignment grade to be? :";
cin >> assGrade;
 
vector<Student::Grade>::iterator itv = (*it).vecGrade.begin();
itv += cc;
 
(*itv).assignmentName = assName;
(*itv).assignmentGrade = assGrade;
cout << (*it).printGrades();
break;
}
 
 
//enter assignemnt # 1-10
// change 1: name or 2: grade
// if 
break;
case 0:
 
break;
default:
cout << "You entered a wrong number" << endl;
goto start;
break;
}
}
}
 
else
{
cout << "Sorry, either the number was incorrectly inputted"
<< "\n or the student does not exist." << endl;
goto start;
}
}
break;
}
case 3:
{
//delete a student
cout << "Deleting a student......" << endl << endl << endl;
cout << "What is the student's student number\n who you want to delete?" << endl << endl;
cin >> ssn;
 
for (vector<Student>::iterator it = vecStudent.begin();
it != vecStudent.end(); it++)
{
//THIS DOESNT WORK, how do i compare the strings properly?
if ( (*it).getStudentNumber() == ssn)
{
cout << "Sorry, either the number was incorrectly inputted"
<< "\n or the student does not exist." << endl;
}
else
{
cout << "Student found......" << endl;
cout << "Deleting...." << endl;
 
//DOESNT WORK, i guess i have to delete it from the vector somehow aswell?
vecStudent.erase(it, it+1);
//delete (*it);
}
}
break;
}
case 0:
{
//exit

//save Student vector to file, outputting everything

fstream myFile("studentData.txt",ios::out);
myFile.trunc;
 
for (vector<Student>::iterator it = vecStudent.begin();
it != vecStudent.end(); it++)
{
 
myFile << (*it).getFirstName() << " " <<
(*it).getLastName() << " " << (*it).getStudentNumber() << endl;
//print out grades
myFile << (*it).printGrades() << endl;
 
}
myFile.close();
return 0;
}
default:
{//error msg, 
cout << "You have put in an incorrect choice" << endl;
goto start;
}
break;
}
}
 
 
 
return 0;
}
 
 
 
}
 
 

Last edited by Devster; Apr 21st, 2007 at 12:26 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Devster is offline Offline
4 posts
since Apr 2007
Apr 21st, 2007
0

Re: Help Needed on C++ assignment

double post
Last edited by Devster; Apr 21st, 2007 at 12:30 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Devster is offline Offline
4 posts
since Apr 2007
Apr 21st, 2007
0

Re: Help Needed on C++ assignment

//THIS DOESNT WORK, how do i compare the strings properly?
C++ Syntax (Toggle Plain Text)
  1. if ( (*it).getStudentNumber() == ssn)
  2. {
  3. cout << "Sorry, either the number was incorrectly inputted"
  4. << "\n or the student does not exist." << endl;
  5. }
  6. else
  7. {
  8. cout << "Student found......" << endl;
  9. cout << "Deleting...." << endl;
  10. }

Erm, don't you think you have your logic mixed up?
Last edited by ~s.o.s~; Apr 21st, 2007 at 5:47 am. Reason: Added code tags, LEARN to use them.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Getting a VC++ Express.NET app to work on other machines
Next Thread in C++ Forum Timeline: Counting the frequency of random numbers





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC