Problems with functions and classes

Problems with functions and classes

Page 1 of 1

1 Replies - 1234 Views - Last Post: 12 November 2006 - 12:45 PM Rate Topic: -----

#1 pazenlatierra  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-November 06

Problems with functions and classes

Post icon  Posted 12 November 2006 - 10:08 AM

I developed a class, using the same class I write a main function that instantiates a Fraction object and call two functions
s1.fnEnterFractionValue();
s2.fnDisplayFraction(): This function prompts the user to enter 1 to see the fraction in fraction format or 2 to see to see the fraction in a decimal format.
I compile the program without any problems BUT only the first function got executed…WHAT I MISSING HERE? I am new at this

/******************************************************************************
  This main function contains two functions that are declare 
  in the fraction_class: 
						fnEnterFractionValue()
						fnDisplayFraction()	
  fnEnterFractionValue: Prompt the user to enter the value of the numerator 
						and denominator 
  fnDisplayFraction:	If the User enter 1: display the fraction as 1/4 
										  2 : display the fraction as a decimal
										  0.25  
*********************************************************************************/
#include "stdafx.h"
#include <iostream>
#include "fraction_class.h"
#include <iomanip>
using namespace std;
/*--------------------------------------------------------------------------
Name: main
Purpose: 

--------------------------------------------------------------------------*/
int main()
{
   fraction_class s1,s2; // defines an object of class fraction_class.h
   {
		s1.fnEnterFractionValue();
		cout << " *****************************************************************************"<<endl;
		cout << " * Please Enter the numerator and the denominator for the fraction, *"		 <<endl;
		cout << " * If you entered 0 as denominator the program will display				  *" <<endl;
		cout << " * and error and will allow you to continue entering numbers until you enter*" <<endl;
		cout << " * a number that is greater than o **ENJOY** *"								<<endl;
		cout << " ****************************************************************************" <<endl;
		cout << " " <<endl;
		cout << "Enter Numerator : ";
		cin >> s1.num1, 
		cout <<  "Enter denominator : ";
		cin >> s1.dem1; cout << '\n'; 
		if (s1.num1 == 0 ||s1.dem1 == 0)
			{
				cout << "I can not divide by 0. Please enter a number > 0: " <<endl;
				cin>> s1.dem1; 
			}
		  s2.fnDisplayFraction();
		  return 0;
   } // end of s1.fnEnterFractionValue() 
		 	 
	s2.fnDisplayFraction(); 
	  {
		//int numb1;
		//int nresult1;
		double result;
		cout << " *****************************************************************************"<<endl;
		cout << " * Please Enter number 1 if you want to see the fraction in a fraction format " <<endl;
		cout << " * Enter number 2 if you want to see the fraction as a decimal 			   " <<endl;
		cout << " ****************************************************************************" <<endl;
		cout << " " <<endl;
		cout << "Enter Number, Remember 1 OR 2: ";
		cin >> s2.numb1;cout << '\n'; 
		if  (s2.numb1 == 1)
			cout <<  "You Just Entered: "; cout <<  s2.num1,  cout << " / " , cout << s2.dem1; 
			cout << '\n';
			cout << " ********************************************** ";
		if (s2.numb1 == 2)
			 result = s2.num1 / s2.dem1;
			 cout <<  "You Just Entered: "; cout << result;
		return 0;
		system ("pause");	
	  } 
	 
}; // end of main 
	
Here is the CLASS: (I am doing other things with this class, it works for otherf programs 
// fraction_class.h
// Fractions calculator 
// This program define a class called func_cal (function calculater)
// This programs contains public data that hold integers the num1,num2 
// and dem1 and dem2.
// The program also contains the following public members fuctions 
// instructUser(),GetData(), doDivideZero
// 
#include "stdafx.h"
#include <iostream>
using namespace std;


class fraction_class	// define the class
{
 public :
 int num1,num2,
	 dem1,dem2;
 int nMaxDenominator, m_Denominator;;
 int numb1, nresult1;
 double result;

 //Function to get InstrUser
 void InstructUser()
  { } 
// Function Get Data
   int GetData()
	{return 0;  }
// Function to Get Data for the Array Fractions 
int GetDataArrFra()
  {return 0;}
//Function to calculate MaxDenoninator in the Array Function  
 int FindMaxDeno()
 {return 0;}
 int fnEnterFractionValue()
 { return 0;}
 int fnDisplayFraction() 
 {return 0;}
};



Is This A Good Question/Topic? 0
  • +

Replies To: Problems with functions and classes

#2 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: Problems with functions and classes

Posted 12 November 2006 - 12:45 PM

for a start you set up s1.num and s1.dem but then call
s2.fnDisplayFraction();


when you should call
s1.fnDisplayFraction();


even then it would not work as the fnDisplayFraction() function defined in class fraction_class is
int fnDisplayFraction()
{return 0;}


which will display nothing ; the correct code appears to be in the file with the main() function
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1