473,405 Members | 2,310 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Having difficulty creating and using classes.

32
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




Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<fstream>
  3. using namespace std;
  4. const int SIZE=100;
  5.  class Info{
  6.        private:
  7.                string id;
  8.                double mark;
  9.  
  10.  
  11.        public:
  12.            Info();
  13.            void display();
  14.             void getData(); 
  15.             void setMark(double); // change the value.
  16.             double getMark(); // assest the value
  17.              string getName();
  18.  
  19. };
  20.  
  21. Info:: Info(){
  22.        id=" ";
  23.        mark=0;
  24.        }
  25.        void Info:: getData(){
  26.             cout<<" please enter the student id:"<<endl;
  27.             getline(cin,id);
  28.             cout<<" please enter the student mark:"<<endl;
  29.             cin>> mark;
  30.             std:: cin.ignore();
  31.             }
  32.  void Info:: display(){
  33.    cout<<id<< "\t"<< mark << endl;
  34.  
  35. }   
  36. void Info::setMark(double m){
  37.      mark=m;
  38.      }
  39.      double Info::getMark(){
  40.             return mark;
  41.             }
  42.            void save(Info s[], int SIZE){
  43.                  ofstream myfile;
  44.                  myfile.open("mark.sami");
  45.                  for( int i=0; i<SIZE; i++)
  46.                  myfile<< s[i].getName <<"\t"<<s[i].mark<<endl<<endl;;
  47.             myfile.close();
  48.             }
  49.              void load(){
  50.                   string line;
  51.                   ifstream myfile("mark.sami");
  52.                   if(myfile.is_open())
  53.                   {
  54.                                       while(! myfile.eof())
  55.                                       {
  56.                                               getline(myfile,line);
  57.                                               cout<<line<< endl;
  58.                                               }
  59.                                               myfile.close();
  60.                                               }
  61.                                               else cout<< " Unable to open file"<<endl;
  62.                                               }    
  63.  
  64.  int main()
  65.  {
  66.     Info std[SIZE];
  67.     for(int i=0; i<SIZE; i++)
  68.      std[i].getData();
  69.      cout<<" \nName\tmark"<<endl;
  70.      cout<<"=========================="<<endl;
  71.     // load();
  72.    for(int i=0; i<SIZE; i++)
  73.     std[i].display();
  74.     save(std,SIZE);
  75.  
  76.      return 0;
  77.      }
Apr 23 '07 #1
15 1842
sicarie
4,677 Expert Mod 4TB
What are the errors you are getting? Can you copy and paste them here as well?
Apr 23 '07 #2
samimmu
32
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
Apr 23 '07 #3
sicarie
4,677 Expert Mod 4TB
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!).
Apr 23 '07 #4
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<<endl;;"

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()".
Apr 24 '07 #5
samimmu
32
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.
Apr 24 '07 #6
samimmu
32
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?
Apr 24 '07 #7
samimmu
32
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.
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<<endl;;"

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()".
Apr 25 '07 #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.

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

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

regards..
Apr 25 '07 #9
sicarie
4,677 Expert Mod 4TB
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.
Apr 25 '07 #10
samimmu
32
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
Apr 25 '07 #11
sicarie
4,677 Expert Mod 4TB
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.
Apr 25 '07 #12
samimmu
32
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
Apr 26 '07 #13
sicarie
4,677 Expert Mod 4TB
[ 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.
Apr 26 '07 #14
samimmu
32
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<iostream>
#include<fstream>
#include<string>
#include<iomanip>
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"<<endl;
inFile>>studentId;
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)<<quiz5<< endl;

average= static_cast<double>(quiz1+quiz2+quiz3+quiz4+quiz5)/5.0;
outFile<< " Average test score:"<< setw(6)<< average<< endl;


inFile.close();
outFile.close();
system("pause");
return 0;
}
Apr 29 '07 #15
sicarie
4,677 Expert Mod 4TB
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<iostream>
#include<fstream>
#include<string>
#include<iomanip>
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"<<endl;
inFile>>studentId;
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)<<quiz5<< endl;

average= static_cast<double>(quiz1+quiz2+quiz3+quiz4+quiz5)/5.0;
outFile<< " Average test score:"<< setw(6)<< average<< endl;


inFile.close();
outFile.close();
system("pause");
return 0;
}
The A:\ drive is usually recognized as 'floppy disk,' are you trying to write to a floppy, or your hard drive? It's possible that the letter was assigned to a local drive, but I'd check that out. (C is usually the hard disk)

If you can't use a for loop, why not use one of the other loops to mimick a for loop? Such as while counter < 100 do stuff, and increment counter. Or if counter < 100, dostuff, increment counter.

At first glance, your files look like they're declared properly (unless there's no floppy in there...).
Apr 29 '07 #16

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

Similar topics

4
by: MLH | last post by:
I am having failures processing the following command and I wonder if you can tell me what I must do in order to have success. When I try to run source mysql_dump.sql.txt ==> it is a problem...
1
by: longtim | last post by:
I have been having endless difficulty creating reports/queries that set any relevent parameters from controls in forms. I am creating an application under access 2003 but will target access...
8
by: Simon Edwards | last post by:
Something thats been bugging me for a while... how do you create a namespace that has many children (namespaces) I.e system.io.blah.blah Iv'e done it by creating a class which contains...
15
by: David Thielen | last post by:
Hi; My ASP.NET app (C# calling J# under .net 2.0) creates a png file in a subdirectory to display as part of the created page. However, the bitmap will not display due to a security violation. ...
4
by: Bugs | last post by:
Hi, I wonder if anyone can help me out. I'm building a vb.net application that has a form with a panel that contains several other sub forms (as a collection of controls). What I'm wanting to...
17
by: Lee Harr | last post by:
I understand how to create a property like this: class RC(object): def _set_pwm(self, v): self._pwm01 = v % 256 def _get_pwm(self): return self._pwm01 pwm01 = property(_get_pwm, _set_pwm)
5
by: Rico | last post by:
Hello, I'm trying to create a simple back up in the SQL Maintenance Plan that will make a single back up copy of all database every night at 10 pm. I'd like the previous nights file to be...
24
by: GesterX | last post by:
First of all I'm new to this site but it certainly looks like a place that i will be visiting more often! Onto my problem. I am creating a Hotel Bussiness project in java using BlueJ The...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.