Having difficulty creating and using classes.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • samimmu
    New Member
    • Mar 2007
    • 32

    Having difficulty creating and using classes.

    actually i have a lot of problems with code down. i actually i made this code to let create a file for several student, i tried by my name but unfortunately i got alot of errors i don't know how can i debug them. if anyone of you know how to complete the program by making the user to enter so many student and their marks and let the user to make some modification or edit his or her names, i want at least 100 student to enter their names, ids,marks, and display the name, the id,the avarage marks for each student in files,this is the code,
    i wish i can get a complete program. please help me and thank you, it is really important, i am really thankful for all of you,have a great time guys




    Code:
    #include<iostream>
    #include<fstream>
    using namespace std;
    const int SIZE=100;
     class Info{
           private:
                   string id;
                   double mark;
                  
                   
           public:
               Info();
               void display();
                void getData(); 
                void setMark(double); // change the value.
                double getMark(); // assest the value
                 string getName();
    
    };
    
    Info:: Info(){
           id=" ";
           mark=0;
           }
           void Info:: getData(){
                cout<<" please enter the student id:"<<endl;
                getline(cin,id);
                cout<<" please enter the student mark:"<<endl;
                cin>> mark;
                std:: cin.ignore();
                }
     void Info:: display(){
       cout<<id<< "\t"<< mark << endl;
       
    }   
    void Info::setMark(double m){
         mark=m;
         }
         double Info::getMark(){
                return mark;
                }
               void save(Info s[], int SIZE){
                     ofstream myfile;
                     myfile.open("mark.sami");
                     for( int i=0; i<SIZE; i++)
                     myfile<< s[i].getName <<"\t"<<s[i].mark<<endl<<endl;;
                myfile.close();
                }
                 void load(){
                      string line;
                      ifstream myfile("mark.sami");
                      if(myfile.is_open())
                      {
                                          while(! myfile.eof())
                                          {
                                                  getline(myfile,line);
                                                  cout<<line<< endl;
                                                  }
                                                  myfile.close();
                                                  }
                                                  else cout<< " Unable to open file"<<endl;
                                                  }    
    
     int main()
     {
        Info std[SIZE];
        for(int i=0; i<SIZE; i++)
         std[i].getData();
         cout<<" \nName\tmark"<<endl;
         cout<<"=========================="<<endl;
        // load();
       for(int i=0; i<SIZE; i++)
        std[i].display();
        save(std,SIZE);
    
         return 0;
         }
    Last edited by sicarie; Apr 23 '07, 05:30 PM. Reason: Changed title, added code tags.
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    What are the errors you are getting? Can you copy and paste them here as well?

    Comment

    • samimmu
      New Member
      • Mar 2007
      • 32

      #3
      if u don't mind, u can copy and paste the code and compile it, actually i didn't know how to create a file for each students, i think the errors is ,"Double Marks is private" i really don't know what does it mean, i tried to change to other types but still the same errors, still so weak in designing a programs because i have not learn classes in deep

      Comment

      • sicarie
        Recognized Expert Specialist
        • Nov 2006
        • 4677

        #4
        Originally posted by samimmu
        if u don't mind, u can copy and paste the code and compile it, actually i didn't know how to create a file for each students, i think the errors is ,"Double Marks is private" i really don't know what does it mean, i tried to change to other types but still the same errors, still so weak in designing a programs because i have not learn classes in deep
        I can, but I would like you to look at the errors - my goal here is not just to fix your program, but help you be able to diagnose a similar issue in the future, hopefully make you a better and more independent programmer (and then you can come back and maybe even help me!).

        Comment

        • nirJianWu
          New Member
          • Apr 2007
          • 7

          #5
          there're some errors just because you forgot to include a header file <string>

          for example, when you compile your code, you'll get a error hint that getline() function does not take one argument, but you do offer it two arguments, with a ifstream myfile and a string line.

          why does compiler consider you just give it only one argument?

          It's because that you declare the variable "line" by using "string line" but you don't tell compiler what the string is by including <string>.

          By the by, I use VS2005, maybe some other compilers can recognise it without <string> header file... I don't make a test.

          Moreover, please pay attention to this line
          "myfile<< s[i].getName <<"\t"<<s[i].mark<<endl<<en dl;;"

          you wanna get the s[i]'s name via a function named getName, so when you call that function, you should write it as "s[i].getName()". Do not forget the parentheses, without them the compiler would take getName a variable of s[i], other than a member function.

          Another problem is, you wanna get s[i]'s mark via a directly visit, however, the member mark in your Info class is defined private, which means you cannot visit it directly or it's invisable to you, the only way you get it is using the interface you defined in your Info class, "getMark()" .

          Comment

          • samimmu
            New Member
            • Mar 2007
            • 32

            #6
            Originally posted by sicarie
            I can, but I would like you to look at the errors - my goal here is not just to fix your program, but help you be able to diagnose a similar issue in the future, hopefully make you a better and more independent programmer (and then you can come back and maybe even help me!).
            thank you Sicarie. actually i am a new programmer and i have learned classes in deep, i just read from book and that question i posted is my exercise, i tried it out and did the best i could but i got the bugs actually i don't how to use " double mark") it is private, i don't know how to deal with it. hopefully u can help me out, thank you so much.

            Comment

            • samimmu
              New Member
              • Mar 2007
              • 32

              #7
              thank thank thank you so much. it is really working, thank you so much, but one more thing if u don't mind, how can i get the avarage months for each students and make a file for each a students, your help is highly appreciated, thank you so much, but in the program it can not create a file, how i do that?

              Comment

              • samimmu
                New Member
                • Mar 2007
                • 32

                #8
                thank you so much, but u know i still have a problem of creating a file for each student and display the average marks for each one in file .i wish u help out to figure this problem.
                Originally posted by nirJianWu
                there're some errors just because you forgot to include a header file <string>

                for example, when you compile your code, you'll get a error hint that getline() function does not take one argument, but you do offer it two arguments, with a ifstream myfile and a string line.

                why does compiler consider you just give it only one argument?

                It's because that you declare the variable "line" by using "string line" but you don't tell compiler what the string is by including <string>.

                By the by, I use VS2005, maybe some other compilers can recognise it without <string> header file... I don't make a test.

                Moreover, please pay attention to this line
                "myfile<< s[i].getName <<"\t"<<s[i].mark<<endl<<en dl;;"

                you wanna get the s[i]'s name via a function named getName, so when you call that function, you should write it as "s[i].getName()". Do not forget the parentheses, without them the compiler would take getName a variable of s[i], other than a member function.

                Another problem is, you wanna get s[i]'s mark via a directly visit, however, the member mark in your Info class is defined private, which means you cannot visit it directly or it's invisable to you, the only way you get it is using the interface you defined in your Info class, "getMark()" .

                Comment

                • nirJianWu
                  New Member
                  • Apr 2007
                  • 7

                  #9
                  Originally posted by samimmu
                  thank you so much, but u know i still have a problem of creating a file for each student and display the average marks for each one in file .i wish u help out to figure this problem.

                  ur.. I dont understand your requirement very much:(

                  will u please show me a more specific example showing your requirement?

                  regards..

                  Comment

                  • sicarie
                    Recognized Expert Specialist
                    • Nov 2006
                    • 4677

                    #10
                    Originally posted by samimmu
                    thank you so much, but u know i still have a problem of creating a file for each student and display the average marks for each one in file .i wish u help out to figure this problem.
                    Do you have a list of the students beforehand? Otherwise, you'll have to create these files dynamically, which can get interesting.

                    Off the top of my head, I'd recommend creating an array of strings, and then filling that with the names, then when you create the outfiles for the individual students, you can use those strings as the names of the files. I'd also recommend using strcpy() or another of the functions here to tack on the absolute file location, otherwise you'll have to find where the file was created. That's easy enough with the search function, but it's easier to just put them in a directory that's easy to access.

                    Comment

                    • samimmu
                      New Member
                      • Mar 2007
                      • 32

                      #11
                      Originally posted by nirJianWu
                      ur.. I dont understand your requirement very much:(

                      will u please show me a more specific example showing your requirement?

                      regards..
                      thank you, actually my requirement is to create a file for 100 students and display in the file the avarage marks for each student and his name,and make the the user to edit some of his informations

                      Comment

                      • sicarie
                        Recognized Expert Specialist
                        • Nov 2006
                        • 4677

                        #12
                        Originally posted by samimmu
                        thank you, actually my requirement is to create a file for 100 students and display in the file the avarage marks for each student and his name,and make the the user to edit some of his informations
                        So just one file, correct? You can use an fstream (google c++ file io or c file io), and that will create the file.

                        Comment

                        • samimmu
                          New Member
                          • Mar 2007
                          • 32

                          #13
                          Originally posted by sicarie
                          So just one file, correct? You can use an fstream (google c++ file io or c file io), and that will create the file.
                          [ i need more than one file. i have 100 students, so i need to create a file for each one of them which can show their names,avarages marks,] thank you so much

                          Regards

                          Alkindi

                          Comment

                          • sicarie
                            Recognized Expert Specialist
                            • Nov 2006
                            • 4677

                            #14
                            Originally posted by samimmu
                            [ i need more than one file. i have 100 students, so i need to create a file for each one of them which can show their names,avarages marks,] thank you so much

                            Regards

                            Alkindi
                            Then yeah, my post #10 would be the way I would approach the problem.

                            Comment

                            • samimmu
                              New Member
                              • Mar 2007
                              • 32

                              #15
                              i wish u can get my wants or requirement, actually here i can not create a file and cin the information for each students, i want the size to 100, but unfortunately i can not use the for loop

                              #include<iostre am>
                              #include<fstrea m>
                              #include<string >
                              #include<iomani p>
                              using namespace std;

                              const int size=10;
                              int main()
                              {

                              ifstream inFile;
                              ofstream outFile;

                              int quiz1, quiz2, quiz3, quiz4, quiz5;
                              double average;
                              char studentId;
                              inFile.open("a: \\quiz.txt");
                              outFile.open("a :\\quizavg.out" );


                              // for(int i=0 ; i< size; i++)
                              cout<<" the students information"<<e ndl;
                              inFile>>student Id;
                              outFile<<" student ID: "<< studentId<< endl;
                              inFile>> quiz1>>quiz2>> quiz3>> quiz4>> quiz5;
                              outFile<<" Quize scores:"<< setw(4)<< quiz1
                              << setw(4)<< quiz2<< setw(4)<< quiz3<< setw(4)<< quiz4
                              <<setw(4)<<quiz 5<< endl;

                              average= static_cast<dou ble>(quiz1+quiz 2+quiz3+quiz4+q uiz5)/5.0;
                              outFile<< " Average test score:"<< setw(6)<< average<< endl;


                              inFile.close();
                              outFile.close() ;
                              system("pause") ;
                              return 0;
                              }

                              Comment

                              Working...