Please check it out

This is a discussion on Please check it out within the C++ Programming forums, part of the General Programming Boards category; Please suggest some improvements for this simple OOP database. code Code: #include<iostream> #include<string> using namespace std; class data { int ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    3

    Please check it out

    Please suggest some improvements for this simple OOP database.

    code


    Code:
    #include<iostream>
    #include<string>
    using namespace std;
    class data
    {
    	int roll;
    	string name;
    	bool condition;
    public:
    	data(){condition=0;}
    	data(int a,string b){roll=a; name=b;}
    	~data(){}
    	void setroll(int a){roll=a;}
    	void setname(string a){name=a;}
    	int getroll(){return roll;}
    	string getname(){return name;}
    	void getdetail()
    	{ 
    		if(condition==0)
    		cout<<"The data is not entered for this roll no. "<<endl;
    		else
    		cout<<"The roll no. of student is "<<roll<<". And name of student is "<<name<<endl;
    	}
    	void setdetail(){cout<<"Enter name of student "; cin>>name;}
    	void setflag(){condition=1;}
    };
    
    
    
    int main()
    {
    	data student[100];
    	string terminator;
    	cout<<"\t\tWEL COME TO STUDENT DATABASE DESIGNED BY ALI SAHU"<<endl<<endl;
    	cout<<"\t\tPlease enter command 'enter' to enter data"<<endl;
    	cout<<"\t\tPlease enter command 'edit' to edit data"<<endl;
    	cout<<"\t\tPlease enter command 'view' to view data "<<endl;
    	cout<<"\t\tTo terminate program enter exit command"<<endl;
    	cout<<endl;
    	while (terminator!="exit")
    	{	cout<<"command>>";
    		cin>>terminator;
    		if(terminator=="exit")
    		cout<<"Terminating"<<endl;
    		else if(terminator=="enter"||terminator=="edit")
    		{
    			int a;
    			cout<<"Enter the roll no to enter or edit the student data "; cin>>a;
    			student[a-1].setdetail();
    			student[a-1].setroll(a);
    			student[a-1].setflag();
    			cout<<"The data is stored"<<endl;
    			
    		}
    		else if(terminator=="view")
    		{
    			int a;
    			cout<<"Enter roll no to view result: "; cin>>a;
    			student[a-1].getdetail();
    		}
    
    		else 
    			cout<<"Wrong command please try again"<<endl;
    
    
    	}
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    There are some minor improvements you could make, but overall the code is not bad.

    1) Do a check to make sure you don't overflow the student array. If the user inserts a number greater than 100, then it will crash your application.

    2) Make your class const correct. For instance, the getname function should be declared as:
    Code:
    string getname() const { return name; }
    3) You should check that cin succeeded before using the user input.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-06-2010, 12:42 AM
  2. how to check input is decimal or not?
    By kalamram in forum C Programming
    Replies: 3
    Last Post: 08-31-2007, 08:07 PM
  3. Please check this loop
    By Daesom in forum C++ Programming
    Replies: 13
    Last Post: 11-02-2006, 01:52 AM
  4. A way to check for Win98 or WinXP
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 10-31-2002, 11:06 AM
  5. how to check for end of line in a text file
    By anooj123 in forum C++ Programming
    Replies: 6
    Last Post: 10-25-2002, 12:21 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21