Convert functions into classes and header file

This is a discussion on Convert functions into classes and header file within the C Programming forums, part of the General Programming Boards category; I did this program of c++ that simulates the tv show deal or no deal, the problem is that the ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    1

    Convert functions into classes and header file

    I did this program of c++ that simulates the tv show deal or no deal, the problem is that the program is one big main function, and I need to add an implementation file and a header file, can somebody help me move some of the function so the program runs correctly, thanks.

    This is my program:

    Code:
    #include <iostream>
    
    #include <fstream>
    
    #include <iomanip>
    
    #include <ctime>
    
    #include <cstdlib>
    
    using namespace std;
    
    const int cols=3;
    
    void displayPartOne(int, double, int);
    
    void displayPartTwo();
    
    void displayPartThree();
    
    void displayMoneyRemainingTable(double [][cols], const int);
    
    void displayCaseTable(double[][cols], const int);
    
    void isCaseNumberInRange(int [], int, int);
    
    double calculateBankersOffer(double [][cols], const int, double);
    
    void isCaseNumberReselected(int [], const int);
    
    int main()
    
    {
    
    	int i, t, x=1, z=0, cases=26;
    
    	int hidden, answer;
    
    	const int Size=6;
    
    	int TempArray[Size]={1,1,1,1,1,1};
    
    	double temp1, temp2, y=0.0;
    
    	const int rows=26;
    
    	double Cases[rows][cols];
    
    	ifstream fin;
    
    
    
    	srand(time(0));
    
    
    
    	// Opens text file and makes sure it works.
    
    	fin.open("amounts.txt");
    
    	if (fin.fail())
    
    		cout << "Error reading file. Please check for amounts.txt file." << endl;
    
    
    
    	for (i=0; i<rows; i++)
    
    	{
    
    		fin >> Cases[i][0];
    
    		fin >> Cases[i][1];
    
    		fin >> Cases[i][2];
    
    	}
    
    	for (int j=0; j<rows; j++)
    
    	{
    
    		i=0;
    
    		t=((i+1)+rand()%(cases-(i+1)));
    
    		temp1=Cases[j][0];
    
    		Cases[j][0]=Cases[t][0];
    
    		Cases[t][0]=temp1;
    
    		temp2=Cases[j][2];
    
    		Cases[j][2]=Cases[t][2];
    
    		Cases[t][2]=temp2;
    
    		i++;
    
    	}		
    
    	
    
    	displayPartOne(x, y, z);
    
    	displayMoneyRemainingTable(Cases, rows);
    
    	displayPartTwo();
    
    	displayCaseTable(Cases, rows);
    
    	displayPartThree();
    
    
    
    	cout << "Pick 1 case as the hidden case: (Pick case # between 1-26 and press Enter)" << endl;
    
    	cin >> hidden;
    
    	isCaseNumberInRange(TempArray, Size, hidden);
    
    	Cases[hidden-1][1]=-2;
    
    	z=hidden;
    
    
    
    	cout << "Please pick 6 cases for this round: (Pick a case # and press Enter)" << endl;
    
    	cin >> TempArray[0];
    
    	Cases[TempArray[0]-1][1]=-1;
    
    	Cases[TempArray[0]][2]=-1;
    
    	cin >> TempArray[1];
    
    	Cases[TempArray[1]-1][1]=-1;
    
    	Cases[TempArray[1]][2]=-1;
    
    	cin >> TempArray[2];
    
    	Cases[TempArray[2]-1][1]=-1;
    
    	Cases[TempArray[2]][2]=-1;
    
    	cin >> TempArray[3];
    
    	Cases[TempArray[3]-1][1]=-1;
    
    	Cases[TempArray[3]][2]=-1;
    
    	cin >> TempArray[4];
    
    	Cases[TempArray[4]-1][1]=-1;
    
    	Cases[TempArray[4]][2]=-1;
    
    	cin >> TempArray[5];
    
    	Cases[TempArray[5]-1][1]=-1;
    
    	Cases[TempArray[5]][2]=-1;
    
    
    
    	system ("cls");
    
    	y=calculateBankersOffer(Cases, rows, y);
    
    	displayPartOne(x, y, z);
    
    	displayMoneyRemainingTable(Cases, rows);
    
    	displayPartTwo();
    
    	displayCaseTable(Cases, rows);
    
    	displayPartThree();
    
    
    
    	cout << "Deal or no deal? (deal=1 / no deal=2, input 1 or 2 and press Enter)" << endl;
    
    	cin >> answer;
    
    	if (answer==1)
    
    	{
    
    		cout << "You win: $" << y << endl;
    
    	}
    
    	else 
    
    	{
    
    		x++;
    
    		cout << "Please pick 5 cases for this round: (Pick a case # and press Enter)" << endl;
    
    		cin >> TempArray[0];
    
    		cin >> TempArray[1];
    
    		cin >> TempArray[2];
    
    		cin >> TempArray[3];
    
    		cin >> TempArray[4];
    
    		isCaseNumberReselected(TempArray, Size);
    
    		Cases[TempArray[0]-1][1]=-1;
    
    		Cases[TempArray[0]][2]=-1;
    
    		Cases[TempArray[1]-1][1]=-1;
    
    		Cases[TempArray[1]][2]=-1;
    
    		Cases[TempArray[2]-1][1]=-1;
    
    		Cases[TempArray[2]][2]=-1;
    
    		Cases[TempArray[3]-1][1]=-1;
    
    		Cases[TempArray[3]][2]=-1;
    
    		Cases[TempArray[4]-1][1]=-1;
    
    		Cases[TempArray[4]][2]=-1;
    
    
    
    		system ("cls");
    
    		y=0;
    
    		y=calculateBankersOffer(Cases, rows, y);
    
    		displayPartOne(x, y, z);
    
    		displayMoneyRemainingTable(Cases, rows);
    
    		displayPartTwo();
    
    		displayCaseTable(Cases, rows);
    
    		displayPartThree();
    
    	}
    
    
    
    	cout << "Deal or no deal? (deal=1 / no deal=2, input 1 or 2 and press Enter)" << endl;
    
    	cin >> answer;
    
    	if (answer==1)
    
    	{
    
    		cout << "You win: $" << y << endl;
    
    	}
    
    	else
    
    	{
    
    		x++;
    
    		cout << "Please pick 4 cases for this round: (Pick a case # and press Enter)" << endl;
    
    		cin >> TempArray[0];
    
    		cin >> TempArray[1];
    
    		cin >> TempArray[2];
    
    		cin >> TempArray[3];
    
    		isCaseNumberReselected(TempArray, Size);
    
    		Cases[TempArray[0]-1][1]=-1;
    
    		Cases[TempArray[0]][2]=-1;
    
    		Cases[TempArray[1]-1][1]=-1;
    
    		Cases[TempArray[1]][2]=-1;
    
    		Cases[TempArray[2]-1][1]=-1;
    
    		Cases[TempArray[2]][2]=-1;
    
    		Cases[TempArray[3]-1][1]=-1;
    
    		Cases[TempArray[3]][2]=-1;
    
    
    
    		system ("cls");
    
    		y=0;
    
    		y=calculateBankersOffer(Cases, rows, y);
    
    		displayPartOne(x, y, z);
    
    		displayMoneyRemainingTable(Cases, rows);
    
    		displayPartTwo();
    
    		displayCaseTable(Cases, rows);
    
    		displayPartThree();
    
    	}
    
    
    
    	cout << "Deal or no deal? (deal=1 / no deal=2, input 1 or 2 and press Enter)" << endl;
    
    	cin >> answer;
    
    	if (answer==1)
    
    	{
    
    		cout << "You win: $" << y << endl;
    
    	}
    
    	else
    
    	{
    
    		x++;
    
    		cout << "Please pick 3 cases for this round: (Pick a case # and press Enter)" << endl;
    
    		cin >> TempArray[0];
    
    		cin >> TempArray[1];
    
    		cin >> TempArray[2];
    
    		isCaseNumberReselected(TempArray, Size);
    
    		Cases[TempArray[0]-1][1]=-1;
    
    		Cases[TempArray[0]][2]=-1;
    
    		Cases[TempArray[1]-1][1]=-1;
    
    		Cases[TempArray[1]][2]=-1;
    
    		Cases[TempArray[2]-1][1]=-1;
    
    		Cases[TempArray[2]][2]=-1;
    
    
    
    		system ("cls");
    
    		y=0;
    
    		y=calculateBankersOffer(Cases, rows, y);
    
    		displayPartOne(x, y, z);
    
    		displayMoneyRemainingTable(Cases, rows);
    
    		displayPartTwo();
    
    		displayCaseTable(Cases, rows);
    
    		displayPartThree();
    
    	}
    
    
    
    	cout << "Deal or no deal? (deal=1 / no deal=2, input 1 or 2 and press Enter)" << endl;
    
    	cin >> answer;
    
    	if (answer==1)
    
    	{
    
    		cout << "You win: $" << y << endl;
    
    	}
    
    	else
    
    	{
    
    		x++;
    
    		cout << "Please pick 2 cases for this round: (Pick a case # and press Enter)" << endl;
    
    		cin >> TempArray[0];
    
    		cin >> TempArray[1];
    
    		isCaseNumberReselected(TempArray, Size);
    
    		Cases[TempArray[0]-1][1]=-1;
    
    		Cases[TempArray[0]][2]=-1;
    
    		Cases[TempArray[1]-1][1]=-1;
    
    		Cases[TempArray[1]][2]=-1;
    
    
    
    		system ("cls");
    
    		y=0;
    
    		y=calculateBankersOffer(Cases, rows, y);
    
    		displayPartOne(x, y, z);
    
    		displayMoneyRemainingTable(Cases, rows);
    
    		displayPartTwo();
    
    		displayCaseTable(Cases, rows);
    
    		displayPartThree();
    
    	}
    
    
    
    	cout << "Deal or no deal? (deal=1 / no deal=2, input 1 or 2 and press Enter)" << endl;
    
    	cin >> answer;
    
    	if (answer==1)
    
    	{
    
    		cout << "You win: $" << y << endl;
    
    	}
    
    	else
    
    	{
    
    		x++;
    
    		cout << "Please pick 1 case for this round: (Pick a case # and press Enter)" << endl;
    
    		cin >> TempArray[0];
    
    		isCaseNumberReselected(TempArray, Size);
    
    		Cases[TempArray[0]-1][1]=-1;
    
    		Cases[TempArray[0]][2]=-1;
    
    
    
    		system ("cls");
    
    		y=0;
    
    		y=calculateBankersOffer(Cases, rows, y);
    
    		displayPartOne(x, y, z);
    
    		displayMoneyRemainingTable(Cases, rows);
    
    		displayPartTwo();
    
    		displayCaseTable(Cases, rows);
    
    		displayPartThree();
    
    	}
    
    
    
    	cout << "Deal or no deal? (deal=1 / no deal=2, input 1 or 2 and press Enter)" << endl;
    
    	cin >> answer;
    
    	if (answer==1)
    
    	{
    
    		cout << "You win: $" << y << endl;
    
    	}
    
    	else
    
    	{
    
    		x++;
    
    		cout << "Please pick 1 case for this round: (Pick a case # and press Enter)" << endl;
    
    		cin >> TempArray[0];
    
    		isCaseNumberReselected(TempArray, Size);
    
    		Cases[TempArray[0]-1][1]=-1;
    
    		Cases[TempArray[0]][2]=-1;
    
    
    
    		system ("cls");
    
    		y=0;
    
    		y=calculateBankersOffer(Cases, rows, y);
    
    		displayPartOne(x, y, z);
    
    		displayMoneyRemainingTable(Cases, rows);
    
    		displayPartTwo();
    
    		displayCaseTable(Cases, rows);
    
    		displayPartThree();
    
    	}
    
    
    
    	cout << "Deal or no deal? (deal=1 / no deal=2, input 1 or 2 and press Enter)" << endl;
    
    	cin >> answer;
    
    	if (answer==1)
    
    	{
    
    		cout << "You win: $" << y << endl;
    
    	}
    
    	else
    
    	{
    
    		x++;
    
    		cout << "Please pick 1 case for this round: (Pick a case # and press Enter)" << endl;
    
    		cin >> TempArray[0];
    
    		isCaseNumberReselected(TempArray, Size);
    
    		Cases[TempArray[0]-1][1]=-1;
    
    		Cases[TempArray[0]][2]=-1;
    
    
    
    		system ("cls");
    
    		y=0;
    
    		y=calculateBankersOffer(Cases, rows, y);
    
    		displayPartOne(x, y, z);
    
    		displayMoneyRemainingTable(Cases, rows);
    
    		displayPartTwo();
    
    		displayCaseTable(Cases, rows);
    
    		displayPartThree();
    
    	}
    
    
    
    	cout << "Deal or no deal? (deal=1 / no deal=2, input 1 or 2 and press Enter)" << endl;
    
    	cin >> answer;
    
    		if (answer==1)
    
    	{
    
    		cout << "You win: $" << y << endl;
    
    	}
    
    	else
    
    	{
    
    		x++;
    
    		cout << "Please pick 1 case for this round: (Pick a case # and press Enter)" << endl;
    
    		cin >> TempArray[0];
    
    		isCaseNumberReselected(TempArray, Size);
    
    		Cases[TempArray[0]-1][1]=-1;
    
    		Cases[TempArray[0]][2]=-1;
    
    
    
    		system ("cls");
    
    		y=0;
    
    		y=calculateBankersOffer(Cases, rows, y);
    
    		displayPartOne(x, y, z);
    
    		displayMoneyRemainingTable(Cases, rows);
    
    		displayPartTwo();
    
    		displayCaseTable(Cases, rows);
    
    		displayPartThree();
    
    	}
    
    
    
    	{
    
    		system ("cls");
    
    		y=0;
    
    		y=calculateBankersOffer(Cases, rows, y);
    
    		displayPartOne(x, y, z);
    
    		displayMoneyRemainingTable(Cases, rows);
    
    		displayPartTwo();
    
    		displayCaseTable(Cases, rows);
    
    		displayPartThree();
    
    	}
    
    
    
    	cout << "Do you want to keep the case or switch with the remaining case(1=keep/2=switch)" << endl;
    
    	cin >> answer;
    
    	if (answer==1)
    
    	{
    
    		cout << "You win: $" << Cases[hidden][2] << endl;
    
    	}
    
    	else if (answer==2)
    
    	{
    
    		for (int i=0; i<rows; i++)
    
    		{
    
    			if ((Cases[i][2]!=-1)&&(Cases[i][1]!=-2))
    
    				cout <<"You win: $" << Cases[i][2] << endl;
    
    		}	
    
    	}
    
    
    
    	return 0;
    
    }
    
    
    
    void displayPartOne(int x,double y, int z)
    
    {
    
    	cout << "----------------------------------------------------------------------" << endl;
    
    	cout << "[[ Deal Or No Deal ]]" << endl;
    
    	cout << "----------------------------------------------------------------------" << endl;
    
    	cout << "Round: " << x << endl;
    
    	cout << "Banker Offer: $" << y << endl;
    
    	cout << "Hidden Case #: " << z << endl;
    
    	cout << "----------------------------------------------------------------------" << endl;
    
    	cout << "[ Money Remaining ]" << endl;
    
    	cout << "----------------------------------------------------------------------" << endl;
    
    }
    
    void displayPartTwo()
    
    {	
    
    	cout << "----------------------------------------------------------------------" << endl;
    
    	cout << "[ Cases ]" << endl;
    
    	cout << "----------------------------------------------------------------------" << endl;
    
    }
    
    void displayPartThree()
    
    {
    
    	cout << "----------------------------------------------------------------------" << endl;
    
    }
    
    
    
    void displayMoneyRemainingTable(double Cases[][cols], int rows)
    
    {
    
    	int f, p, q=2, value=0, value1=13;
    
    	for (int j=0; j<13; j++)
    
    	{
    
    		for (int i=0; i<rows; i++)
    
    	{
    
    		if (Cases[i][0]==value)
    
    			f=i;
    
    		if (Cases[i][0]==value1)
    
    			p=i;
    
    	}
    
    		if (Cases[f][2]==-1)
    
    			cout << "----" << "       ";
    
    		else 
    
    			cout << fixed << setprecision(q) << "$" << Cases[f][2]<< "       ";  
    
    		if (Cases[p][2]==-1)
    
    			cout << "----" << endl;
    
    		else
    
    			cout << setprecision(0) << "$" << Cases[p][2] << endl;
    
    		value++;
    
    		value1++;
    
    		q=0;
    
    	}
    
    }
    
    void displayCaseTable(double Cases[][cols], int rows)
    
    {
    
    	for (int i=0; i<6; i++)
    
    	{
    
    		if (Cases[i][1]==-2)
    
    			cout << "(hidden)" << "     ";		
    
    		else if (Cases[i][1]==-1)
    
    			cout << "(picked)" << "     ";
    
    		else 
    
    			cout << "Case " << Cases[i][1] << "       ";
    
    	}
    
    	cout << endl;
    
    	cout << setw(0);
    
    	for (int i=6; i<9; i++)
    
    	{
    
    		if (Cases[i][1]==-2)
    
    			cout << "(hidden)" << "     ";
    
    		else if (Cases[i][1]==-1)
    
    			cout << "(picked)" << "     ";
    
    		else 
    
    			cout << "Case " << Cases[i][1] << "       ";
    
    	}
    
    	for (int i=9; i<12; i++)
    
    	{
    
    		if (Cases[i][1]==-2)
    
    			cout << "(hidden)" << "     ";
    
    		else if (Cases[i][1]==-1)
    
    			cout << "(picked)" << "     ";
    
    		else 
    
    			cout << "Case " << Cases[i][1] << "      ";
    
    	}
    
    	cout << endl;
    
    	cout << setw(0);
    
    	for (int i=12; i<18; i++)
    
    	{
    
    		if (Cases[i][1]==-2)
    
    			cout << "(hidden)" << "     ";
    
    		else if (Cases[i][1]==-1)
    
    			cout << "(picked)" << "     ";
    
    		else 
    
    			cout << "Case " << Cases[i][1] << "      ";
    
    	}
    
    	cout << endl;
    
    	cout << setw(0);
    
    	for (int i=18; i<24; i++)
    
    	{
    
    		if (Cases[i][1]==-2)
    
    			cout << "(hidden)" << "     ";
    
    		else if (Cases[i][1]==-1)
    
    			cout << "(picked)" << "     ";
    
    		else 
    
    			cout << "Case " << Cases[i][1] << "      ";
    
    	}
    
    	cout << endl;
    
    	cout << setw(0);
    
    	for (int i=24; i<26; i++)
    
    	{
    
    		if (Cases[i][1]==-2)
    
    			cout << "(hidden)" << "     ";
    
    		else if (Cases[i][1]==-1)
    
    			cout << "(picked)" << "     ";
    
    		else 
    
    			cout << "Case " << Cases[i][1] << "      ";
    
    	}
    
    	cout << endl;
    
    	cout << setw(0);
    
    }
    
    void isCaseNumberInRange(int TempArray[], int Size, int hidden)
    
    {
    
    	if ((hidden<0)||(hidden>26))
    
    	{
    
    		cout << "This Case is not in the range" << endl;
    
    	}
    
    	for (int i=0; i<6; i++)
    
    	{
    
    		if ((TempArray[i]<0)||(TempArray[i]>26))
    
    			cout << "This Case is not in the range." << endl;
    
    	}
    
    
    
    }
    
    double calculateBankersOffer(double Cases[][cols], int rows, double y)
    
    {
    
    	double total=0, k=0;
    
    	int count=0;
    
    	
    
    	for (int i=0; i<rows; i++)
    
    		if ((Cases[i][2]!=-1)&&(Cases[i][2]!=-2))
    
    		{
    
    			total+=Cases[i][2];
    
    			count++;
    
    		}
    
    
    
    		k=total/count;
    
    
    
    		return k;
    
    }
    
    void isCaseNumberReselected(int TempArray[], int Size)
    
    {
    
    	for (int i=0; i<=5; i++)
    
    		if (TempArray[i]<0)
    
    			cout << "One of these cases has already been selected." << endl;
    
    }

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    You've posted your problem in wrong section.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  3. #3
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Granted this not in c, but the work needed to break this up into multiple .h files will still hold true here. What you need to do is place all your function declarations in a say a func_decl.h file, then place the function definitions in a func_impl.h file. Then in your main.c file, include those header files. Is that what you were seeking?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Defining multiple classes in the same header file
    By Stonehambey in forum C++ Programming
    Replies: 2
    Last Post: 08-14-2008, 10:36 AM
  2. #include header files or .cpp files?
    By DoctorX in forum C++ Programming
    Replies: 3
    Last Post: 12-23-2006, 11:21 AM
  3. I keep seeing some advice.. (Header file related)
    By Lithorien in forum C++ Programming
    Replies: 10
    Last Post: 01-14-2005, 03:31 AM
  4. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 04:25 AM
  5. help with classes and member functions
    By syner in forum C++ Programming
    Replies: 4
    Last Post: 07-19-2002, 08:45 PM

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