By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,142 Members | 1,937 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,142 IT Pros & Developers. It's quick & easy.

error C2228: left of '.grade' must have class/struct/union

P: 2
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <iomanip>
  5. using namespace std;
  6. const int NO_OF_STUDENTS=20;
  7. struct studentType
  8. {
  9.     string studentFName;
  10.     string studentLName;
  11.     int testScore;
  12.     char grade;
  13. };
  14. void getData(studentType sList[], int NO_OF_STUDENTS);
  15. void calculateGrade(studentType sList[], int NO_OF_STUDENTS);
  16. void printResult(const studnetType sList[], int NO_OF_STUDENTS);
  17. int main()
  18. {
  19.     studentType sList[NO_OF_STUDENTS];
  20.     getData(sList, NO_OF_STUDENTS);
  21.     calculateGrade(sList, NO_OF_STUDENTS);
  22.     printResult(sList, NO_OF_STUDENTS);
  23.     return 0;
  24. }
  25.  
  26. void getData(const studentType sList[], int listSize)
  27. {
  28.     ifstream inData;
  29.     ofstream outData;
  30.     inData.open("C:\\student.txt");
  31.     for (int i=0;i<NO_OF_STUDENTS;i++)
  32.         inData>>sList[i].studentFName>>sList[i].studentLName>>sList[i].testScore;
  33.     outData.close();
  34. }
  35.  
  36. void calculateGrade(studentType sList[], int listSize)
  37. {
  38.     for (int i=0;i<NO_OF_STUDENTS;i++)
  39.     {
  40.         switch(sList[i].testScore/10)
  41.         {
  42.         case 0:
  43.         case 1:
  44.         case 2:
  45.         case 3:
  46.         case 4:
  47.         case 5:
  48.             sList[i].grade='F';
  49.             break;
  50.         case 6:
  51.             sList[i].grade='D';
  52.             break;
  53.         case 7:
  54.             sList[i].grade='C';
  55.             break;
  56.         case 8:
  57.             sList[i].grade='B';
  58.             break;
  59.         case 9:
  60.         case 10:
  61.             sList[i].grade='A';
  62.             break;
  63.         }//closes switch
  64.     }//closes for loop
  65. }//closes function
  66.  
  67. void printResult(const studnetType sList[], int listSize) (THIS IS WHERE I'M HAVING TROUBLE
  68. {
  69.     for (int d=0;d<NO_OF_STUDENTS;d++)
  70.     {
  71.         cout<<sList[d].studentFName<<sList[d].studentLName<<sList[d].testScore<<sList[d].grade<<endl;
  72.     }
  73. }
  74.  

1>------ Build started: Project: project5, Configuration: Debug Win32 ------
1>Compiling...
1>project5.cpp
1>c:\documents and settings\yese\my documents\visual studio 2005\projects\project5\project5.cpp(16) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\documents and settings\yese\my documents\visual studio 2005\projects\project5\project5.cpp(16) : error C2146: syntax error : missing ',' before identifier 'sList'
1>c:\documents and settings\yese\my documents\visual studio 2005\projects\project5\project5.cpp(22) : error C2660: 'printResult' : function does not take 2 arguments
1>c:\documents and settings\yese\my documents\visual studio 2005\projects\project5\project5.cpp(32) : error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'const std::string' (or there is no acceptable conversion)
1> c:\program files\microsoft visual studio 8\vc\include\istream(1137): could be 'std::basic_istream<_Elem,_Traits> &std::operator >><std::char_traits<char>>(std::basic_istream<_Ele m,_Traits> &,signed char *)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 8\vc\include\istream(1139): or 'std::basic_istream<_Elem,_Traits> &std::operator >><std::char_traits<char>>(std::basic_istream<_Ele m,_Traits> &,signed char &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 8\vc\include\istream(1141): or 'std::basic_istream<_Elem,_Traits> &std::operator >><std::char_traits<char>>(std::basic_istream<_Ele m,_Traits> &,unsigned char *)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 8\vc\include\istream(1143): or 'std::basic_istream<_Elem,_Traits> &std::operator >><std::char_traits<char>>(std::basic_istream<_Ele m,_Traits> &,unsigned char &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 8\vc\include\string(593): or 'std::basic_istream<_Elem,_Traits> &std::operator >><char,std::char_traits<char>,std::allocator<_Ty> >(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Ax> &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ty=char,
1> _Ax=std::allocator<char>
1> ]
1> c:\program files\microsoft visual studio 8\vc\include\istream(155): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(std::basic_istream<_Elem,_Traits> &(__cdecl *)(std::basic_istream<_Elem,_Traits> &))'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 8\vc\include\istream(161): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(std::basic_ios<_Elem,_Traits> &(__cdecl *)(std::basic_ios<_Elem,_Traits> &))'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 8\vc\include\istream(168): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(std::ios_base &(__cdecl *)(std::ios_base &))'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 8\vc\include\istream(175): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(std::_Bool &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 8\vc\include\istream(194): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(short &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 8\vc\include\istream(228): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(unsigned short &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 8\vc\include\istream(247): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(int &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 8\vc\include\istream(273): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(unsigned int &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 8\vc\include\istream(291): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(long &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 8\vc\include\istream(309): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(__w64 unsigned long &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 8\vc\include\istream(329): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(__int64 &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 8\vc\include\istream(348): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(unsigned __int64 &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 8\vc\include\istream(367): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(float &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 8\vc\include\istream(386): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(double &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 8\vc\include\istream(404): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(long double &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 8\vc\include\istream(422): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(void *&)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 8\vc\include\istream(441): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(std::basic_streambuf<_Elem,_Traits> *)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> while trying to match the argument list '(std::ifstream, const std::string)'
1>c:\documents and settings\yese\my documents\visual studio 2005\projects\project5\project5.cpp(67) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\documents and settings\yese\my documents\visual studio 2005\projects\project5\project5.cpp(67) : error C2146: syntax error : missing ',' before identifier 'sList'
1>c:\documents and settings\yese\my documents\visual studio 2005\projects\project5\project5.cpp(71) : error C2065: 'sList' : undeclared identifier
1>c:\documents and settings\yese\my documents\visual studio 2005\projects\project5\project5.cpp(71) : error C2228: left of '.studentFName' must have class/struct/union
1>c:\documents and settings\yese\my documents\visual studio 2005\projects\project5\project5.cpp(71) : error C2228: left of '.studentLName' must have class/struct/union
1>c:\documents and settings\yese\my documents\visual studio 2005\projects\project5\project5.cpp(71) : error C2228: left of '.testScore' must have class/struct/union
1>c:\documents and settings\yese\my documents\visual studio 2005\projects\project5\project5.cpp(71) : error C2228: left of '.grade' must have class/struct/union
1>Build log was saved at "file://c:\Documents and Settings\Yese\My Documents\Visual Studio 2005\Projects\project5\Debug\BuildLog.htm"
1>project5 - 11 error(s), 0 warning(s)



i'm supposed to display the list in the last function...can anyone please help?
Dec 7 '07 #1
Share this Question
Share on Google+
2 Replies


gpraghuram
Expert 100+
P: 1,275
HI,
Whenever you define a struuct you shuld use struct keyword while declaring variables of that type or else you shuld do a typedef.

Expand|Select|Wrap|Line Numbers
  1. //your have to change these
  2. void getData(struct studentType sList[], int NO_OF_STUDENTS);
  3. void calculateGrade(struct studentType sList[], int NO_OF_STUDENTS);
  4. void printResult(const struct studnetType sList[], int NO_OF_STUDENTS);
  5.  
  6. //or change the declaraion like
  7. typedef struct _studnetType
  8. {
  9. //
  10. }studnetType ;
  11. //then you can have the code as it is.
  12.  

Raghuram
Dec 7 '07 #2

P: 2
HI,
Whenever you define a struuct you shuld use struct keyword while declaring variables of that type or else you shuld do a typedef.

Expand|Select|Wrap|Line Numbers
  1. //your have to change these
  2. void getData(struct studentType sList[], int NO_OF_STUDENTS);
  3. void calculateGrade(struct studentType sList[], int NO_OF_STUDENTS);
  4. void printResult(const struct studnetType sList[], int NO_OF_STUDENTS);
  5.  
  6. //or change the declaraion like
  7. typedef struct _studnetType
  8. {
  9. //
  10. }studnetType ;
  11. //then you can have the code as it is.
  12.  

Raghuram


no, it wasn't that at all...i realized i spelled student incorrectly in that function (studnet)...that solved all my errors. haha, i feel so dumb. thanks though!
Dec 7 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.