new with structs

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • charmeda103
    New Member
    • Oct 2008
    • 36

    new with structs

    I am writing a program that deals with structs.
    i need help with structs this is my first time dealing with them. can you give me some examples that would be helpful.
    my program is supposed to have to read data from two input files.
    any inforamtion on stucts will be helpful.

    thank
    you
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Structs are just a useful way to store related pieces of information together. For instance, let's consider the following very simple idea:

    You are a teacher, and you want to store some basic information about your students - their name, their age, and their current grade (out of 100). You know you can code it like this:

    Code:
    string name;
    int age;
    double grade;
    This would be fine if you had just 1 student, but you have closer to 30. OK, how about this approach?

    Code:
    string nameOfStud1, nameOfStud2, nameOfStud3; // ...and so on...
    int ageOfStud1, ageOfStud2, ageOfStud3; // ...and so on...
    double gradeOfStud1, gradeOfStud2, gradeOfStud3; // ...and so on...
    Very messy - not at all what you wanted. So how to simplify this? One answer is to use arrays:

    Code:
    string names[30];
    int ages[30];
    double grades[30];
    Not very satisfactory - yes, names[5], ages[5], and grades[5] all correspond to the 6th student, but what if you wanted to print out the student's names in alphabetical order, and had to sort that array to do so? Lots of messiness involved still.

    The real answer is to use a simple struct:

    Code:
    struct Student {
       string name;
       int age;
       double grade;
    };
    
    Student myClass[30];
    What is Student? It's just a fancy name for collecting three related pieces of data - a name, age, and grade - into the same structure. You can now (correctly) think of these pieces of data as a whole - a Student.

    On the technical side, you must now refer to the data members in the following manner:

    Code:
    Student myStudent;
    myStudent.name = "Ganon11";
    myStudent.age = 130;
    myStudent.grade = 100.0;
    which is a trivial addition when compared to the massive confusion you've avoided.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      Only one thing to add ... how to work with pointers to structures.
      Code:
      Student *pStudent;
      pStudent->name = "Ganon11";
      pStudent->age = 130;
      pStudent->grade = 100.0;
      Just apply the rules already given to handle structures that contain other structures or that contain pointers to other structures.

      After you become a structural expert you should google the tem 'incomplete structure'.

      Ganon11: Perhaps you feel that old today, but I'm sure you're usually quite spry. You're certainly confident you'll do well in this hypothetical class!

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        Originally posted by donbock
        Only one thing to add ... how to work with pointers to structures.
        Code:
        Student *pStudent;
        pStudent->name = "Ganon11";
        pStudent->age = 130;
        pStudent->grade = 100.0;
        Make sure you allocate some memory for that struct, or you'll get a whole bunch of segmentation faults:

        Code:
        Student *pStudent = new Student;
        Originally posted by donbock
        Ganon11: Perhaps you feel that old today, but I'm sure you're usually quite spry. You're certainly confident you'll do well in this hypothetical class!
        The rest of the students are young'uns - 20s and 30s all of em. Whippersnappers - back in my day, I had to walk uphill both ways in a snowstorm to access the Usenet.

        Comment

        • charmeda103
          New Member
          • Oct 2008
          • 36

          #5
          can you use structs in void functions or do they have to be declared before main.
          how would u use structs has a void function

          Comment

          • donbock
            Recognized Expert Top Contributor
            • Mar 2008
            • 2427

            #6
            Originally posted by charmeda103
            can you use structs in void functions or do they have to be declared before main.
            how would u use structs has a void function
            By "void function" do you mean a function with no return value or a function with no arguments?

            Comment

            • charmeda103
              New Member
              • Oct 2008
              • 36

              #7
              a void function like

              void ReadStudentName (string,name,.. ......etc)

              {
              more code here
              }
              no return value necessary

              Comment

              • charmeda103
                New Member
                • Oct 2008
                • 36

                #8
                I just started my program does it look like i am doing it right.

                Code:
                #include<iostream>
                #include<iomanip>
                #include<cmath>
                #include<string>
                #include<fstream>
                
                
                
                using namespace std;
                
                struct schoolStats
                {
                	string schoolName;
                	int numberWins;
                	int numberLosses;
                	int numberPointsTheTeamScored;
                	int numberPointsScoreAganistTheTeam;
                	int teamOverallWins;
                	int teamOverallLosses;
                	int numberPointsTheTeamScoredInAll;
                	int numberPointsTheTeamScoredAganist;
                }
                
                
                
                
                int main()
                {
                
                	ifstream inFile1;
                	inFile1.open("G:\\formerstats.txt");
                	
                	while(!inFile1.eof())
                	{
                	 
                
                
                
                
                
                
                	}
                
                
                
                
                
                
                
                
                
                
                
                
                	return 0;
                }

                Comment

                • Ganon11
                  Recognized Expert Specialist
                  • Oct 2006
                  • 3651

                  #9
                  Looks like you've got a great start.

                  Comment

                  • charmeda103
                    New Member
                    • Oct 2008
                    • 36

                    #10
                    ok now i am getting a bunch of errors help!

                    here are the messages:
                    error C2628: 'schoolStats' followed by 'void' is illegal (did you forget a ';'?)
                    syntax error : missing ',' before identifier 'string'
                    error C2065: 'schoolName' : undeclared identifier
                    error C2065: 'numberWins' : undeclared identifier
                    error C2065: 'numberPointsTh eTeamScored' : undeclared identifier
                    error C2065: 'numberPointsSc oreAganistTheTe am' : undeclared identifier
                    error C2065: 'teamOverallWin s' : undeclared identifier
                    error C2065: 'teamOverallLos ses' : undeclared identifier
                    error C2065: 'numberPointsTh eTeamScoredInAl l' : undeclared identifier
                    error C2065: 'numberPointsTh eTeamScoredAgan ist' : undeclared identifier
                    error C2146: syntax error : missing ',' before identifier 'string'
                    : error C2556: 'void SchoolStats(std ::ifstream &,std::strin g &,int &,int &,int &,int &,int &,int &,int &)' : overloaded function differs only by return type from 'schoolStats SchoolStats(std ::ifstream &,std::strin g &,int &,int &,int &,int &,int &,int &,int &)'
                    see declaration of 'SchoolStats'
                    error C2371: 'SchoolStats' : redefinition; different basic types
                    see declaration of 'SchoolStats'

                    Code:
                    #include<iostream>
                    #include<iomanip>
                    #include<cmath>
                    #include<string>
                    #include<fstream>
                    
                    
                    
                    using namespace std;
                    
                    struct schoolStats
                    {
                    	string schoolName;
                    	int numberWins;
                    	int numberLosses;
                    	int numberPointsTheTeamScored;
                    	int numberPointsScoreAganistTheTeam;
                    	int teamOverallWins;
                    	int teamOverallLosses;
                    	int numberPointsTheTeamScoredInAll;
                    	int numberPointsTheTeamScoredAganist;
                    }
                    
                    void SchoolStats(ifstream& inFile1 string&, int&, int&, int&, int&, int&, int&, int&);
                    
                    
                    
                    int main()
                    {
                    
                    	ifstream inFile1;
                    	inFile1.open("G:\\formerstats.txt");
                    	
                    	while(!inFile1.eof())
                    	{
                    	  SchoolStats(inFile1, schoolName, numberWins, numberPointsTheTeamScored, numberPointsScoreAganistTheTeam, teamOverallWins, teamOverallLosses, numberPointsTheTeamScoredInAll, numberPointsTheTeamScoredAganist);
                    
                    
                    
                    
                    
                    
                    
                    	}
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    	return 0;
                    }
                    void SchoolStats(ifstream& inFile1 string& schoolName, int&numberWins, int&numberPointsTheTeamScored, int&numberPointsScoreAganistTheTeam, int&teamOverallWins, int&teamOverallLosses, int&numberPointsTheTeamScoredInAll, int&numberPointsTheTeamScoredAganist)
                    {
                    	 schoolStats stats[10];
                    		 stats[1].schoolName;
                    		 stats[1].numberWins;
                    		 stats[1].numberPointsTheTeamScored;
                    		 stats[1].numberPointsScoreAganistTheTeam;
                    		 stats[1].teamOverallWins;
                    		 stats[1].teamOverallLosses;
                    		 stats[1].numberPointsTheTeamScoredInAll;
                    		 stats[1].numberPointsTheTeamScoredAganist;
                    
                    	
                    }

                    Comment

                    • boxfish
                      Recognized Expert Contributor
                      • Mar 2008
                      • 469

                      #11
                      You need a semicolon after the closing brace of your struct.
                      I hope this solves some of the errors.

                      Comment

                      • charmeda103
                        New Member
                        • Oct 2008
                        • 36

                        #12
                        please i am having a hard time with this program. can you help me fix these errors.

                        error C2146: syntax error : missing ',' before identifier 'string'
                        error C2065: 'schoolName' : undeclared identifier
                        error C2065: 'numberWins' : undeclared identifier
                        error C2065: 'numberPointsTh eTeamScored' : undeclared identifier
                        error C2065: 'numberPointsSc oreAganistTheTe am' : undeclared identifier
                        error C2065: 'teamOverallWin s' : undeclared identifier
                        error C2065: 'teamOverallLos ses' : undeclared identifier
                        error C2065: 'numberPointsTh eTeamScoredInAl l' : undeclared identifier
                        error C2065: 'numberPointsTh eTeamScoredAgan ist' : undeclared identifier

                        Code:
                        
                        #include<iostream>
                        #include<iomanip>
                        #include<cmath>
                        #include<string>
                        #include<fstream>
                        
                        
                        
                        using namespace std;
                        
                        struct schoolStats
                        {
                        	string schoolName;
                        	int numberWins;
                        	int numberLosses;
                        	int numberPointsTheTeamScored;
                        	int numberPointsScoreAganistTheTeam;
                        	int teamOverallWins;
                        	int teamOverallLosses;
                        	int numberPointsTheTeamScoredInAll;
                        	int numberPointsTheTeamScoredAganist;
                        };
                        
                        void SchoolStats(ifstream& inFile1 string&, int&, int&, int&, int&, int&, int&, int&);
                        
                        
                        
                        int main()
                        {
                        
                        	ifstream inFile1;
                        	inFile1.open("G:\\formerstats.txt");
                        	
                        	while(!inFile1.eof())
                        	{
                        	  SchoolStats(inFile1, schoolName, numberWins, numberPointsTheTeamScored, numberPointsScoreAganistTheTeam, teamOverallWins, teamOverallLosses, numberPointsTheTeamScoredInAll, numberPointsTheTeamScoredAganist);
                        
                        	}
                        
                        
                        	return 0;
                        }
                        void SchoolStats(ifstream& inFile1, string& schoolName, int&numberWins, int&numberPointsTheTeamScored, int&numberPointsScoreAganistTheTeam, int&teamOverallWins, int&teamOverallLosses, int&numberPointsTheTeamScoredInAll, int&numberPointsTheTeamScoredAganist)
                        {
                        	 schoolStats stats[10];
                        	 
                        	cout<<stats[1].schoolName;
                        	cout<<stats[1].numberWins;
                        	cout<<stats[1].numberPointsTheTeamScored;
                        	cout<<stats[1].numberPointsScoreAganistTheTeam;
                        	cout<<stats[1].teamOverallWins;
                        	cout<<stats[1].teamOverallLosses;
                        	cout<< stats[1].numberPointsTheTeamScoredInAll;
                            cout<<stats[1].numberPointsTheTeamScoredAganist;
                        
                        	
                        }

                        Comment

                        • boxfish
                          Recognized Expert Contributor
                          • Mar 2008
                          • 469

                          #13
                          The first error means what it says; you're missing a comma between the first two parameters in the declaration of your SchoolStats function. As for the other errors, they're problems with the arguments you're passing to SchoolStats. I'm not sure what you were trying to do there. What is the SchoolStats function supposed to do?

                          Comment

                          • charmeda103
                            New Member
                            • Oct 2008
                            • 36

                            #14
                            i changed the program a bit its supposed to read information from a file.



                            here is the error message. i cant figure out how to fix it.



                            error C2065: 'schoolName' : undeclared identifier
                            error C2065: 'basketball' : undeclared identifier
                            error C2228: left of '.schoolName' must have class/struct/union

                            How do i declare this




                            Code:
                            
                            
                            #include<iostream>
                            #include<iomanip>
                            #include<cmath>
                            #include<string>
                            #include<fstream>
                            
                            
                            
                            using namespace std;
                            
                            void SchoolStats(ifstream& inFile1, string&);
                            
                            struct basketballStats
                            {
                            	string schoolName;
                            	int numberWins;
                            	int numberLosses;
                            	int numberPointsTheTeamScored;
                            	int numberPointsScoreAganistTheTeam;
                            	int teamOverallWins;
                            	int teamOverallLosses;
                            	int numberPointsTheTeamScoredInAll;
                            	int numberPointsTheTeamScoredAganist;
                            };
                            
                             
                            
                            
                            int main()
                            {
                            
                            	ifstream inFile1;
                            	inFile1.open("G:\\formerstats.txt");
                            	
                            	while(!inFile1.eof())
                            	{
                            
                            		  SchoolStats( inFile1,schoolName);
                            		  cout<<basketballStats.schoolName;
                            
                            
                            	}
                            
                            	return 0;
                            }
                            void SchoolStats(ifstream& inFile1, string& schoolName)
                            {
                            	 
                            	 basketballStats mystats[10];
                            	
                            		
                            	cout<<basketball[1].schoolName="BW";
                            	
                            
                            	
                            }

                            Comment

                            • Ganon11
                              Recognized Expert Specialist
                              • Oct 2006
                              • 3651

                              #15
                              Originally posted by charmeda103
                              Code:
                              void SchoolStats(ifstream& inFile1, string& schoolName)
                              {
                              	 
                              	 basketballStats mystats[10];
                              	
                              		
                              	cout<<basketball[1].schoolName="BW";
                              	
                              
                              	
                              }
                              Let's look at what you've done here. You've declared an array of basketballStats struct called mystats. Then you are trying to output from basketball[1]. What is basketball? You never declared it! You're not using the array you just created, like you should be. Plus, you're trying to assign to a variable in the same statement as your output.

                              You should be manipulating mystats[0], mystats[1], etc...up to mystats[9].

                              Comment

                              Working...