1 Replies - 97 Views - Last Post: 08 July 2012 - 02:24 AM Rate Topic: -----

#1 rock9212  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 08-July 12

problem want to use class for functions get and display info

Posted 08 July 2012 - 12:40 AM

#include<iostream>
#include<conio.h>
#include<string>
#include<iomanip>
using namespace std;

struct Records
{
   string name;
   int age;
};

void getInfo(Records files[], int SIZE);
void displayInfo(Records files[], int SIZE);

const int SIZE = 50;
int numRec = 0; 

int main()
{
   // Declare an array of objects.
   Records files[SIZE];
   
   cout << "Max amount of files you can enter is " << SIZE << ".\n";
   cout << "\nHow many records do you want to enter?: ";
   cin >> numRec;
   cout << endl;
   
   if (numRec <= SIZE)
   {
      getInfo(files, SIZE);
      cout << endl;
   }
   else
   {
      cout << "You can only enter less than " << SIZE << " records!\n\n";
      exit(0);
   }   

   displayInfo(files, SIZE);   
   return 0;
}


void getInfo(Records files[], int SIZE)
{
   for (int i = 0; i < numRec; i++)
   {
      cout << "Name: ";
      cin >> files[i].name;
      cout << setw(6) << "Age: ";
      cin >> files[i].age;
      cout << endl;
   }
}


void displayInfo(Records files[], int SIZE)
{
   int count = 1;
   cout << "Here are the records you entered:\n\n";

   for (int i = 0; i < numRec; i++)
      {
         cout << "Object#" << count << ":\n";
         cout << "NAME: " << files[i].name << endl;
         cout << setw(6) << "AGE: " << files[i].age << endl;
         cout << endl;
         count++;
         getch();
      }
}

This post has been edited by Salem_c: 08 July 2012 - 01:10 AM


Is This A Good Question/Topic? 0
  • +

Replies To: problem want to use class for functions get and display info

#2 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1418
  • View blog
  • Posts: 2,681
  • Joined: 30-May 10

Re: problem want to use class for functions get and display info

Posted 08 July 2012 - 02:24 AM

Start with
class Records
{
   string name;
   int age;
  public:
   setter(string,int);
   getter(string&,int&);
};



Now have a go at implementing some class member functions, and calling them from your code.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1