calling a function in cpp

K

Ken

hello,

I would to know if it is possible to call an object in a function within a
class.
Meaning , In a class, A function X calling onto a function Y, and function
Y we want one of the two calculation ( eg seconds , out of seconds and
minutes)
thanks ,

Ken
lerameur at yahoo.com

here is the program I wrote, but the function subtraction wich is calling
another function is not working, dont know how to access the info.



#include <iostream>
#include <conio.h> // Used in microsoft visual c++
using namespace std;


class Conversion
{
public:
void menu();
void sec_to_hours();
void time_to_seconds();
void substraction();
};

void Conversion::time_to_seconds() //Converts time to seconds
{ int seconds1, seconds2, hours, minutes, seconds;
char colon;
cout<<"Enter a time (hours:minutes:seconds): " ;
cin>> hours >>colon >>minutes >>colon >> seconds;
if ( hours >= 0 && hours < 24 ) seconds1 = hours * 3600 ;
else cout<<"Invalid amounts of hours " <<endl;
if ( minutes >= 0 && minutes < 60) seconds2 = minutes *60;
else cout<<"Invalid amount of minutes " <<endl;
if ( seconds >= 0 && seconds< 60) ;
else cout<<"Invalid amount of seconds " <<endl;
if ( ( hours >= 0 && hours < 24 ) && ( minutes >= 0 && minutes < 60) &&
( seconds >= 0 && seconds< 60) )
seconds = seconds1 + seconds2 + seconds ;
cout<< " There is: " << seconds <<" seconds." <<endl;
}
void Conversion::substraction()
{ int seconds ;//total_seconds;

time_to_seconds();
cout<<" Now enter the amount of seconds to subtract : " <<endl;
cin >> seconds ;

if ( time_calc.time_to_seconds() >= seconds)
cout<< " Substracting " << time_to_seconds.seconds <<" from " << seconds
<<" is: " << time_to_seconds().seconds - seconds ;
else
cout<< " The time has to be greater then the seconds. Please try again! "
;


}


void Conversion::menu()
{
int choice;
Conversion time_calc;
do {
cout << endl << endl;
cout << " Time Management System "<<endl;
cout << " ============================================== "<<endl;
cout << " 1: Convert a number of seconds into a time "<<endl;
cout << " 2: Convert a time into a number of seconds "<<endl;
cout << " 3: Substrat a number of seconds from a time "<<endl;
cout << " 4: Quit "<<endl;
cout << " ============================================== "<<endl;

cout << " Your choice please: ";
cin >> choice;

switch (choice)
{
case 1:
sec_to_hours();
break;
case 2:
time_calc.time_to_seconds();
break;
case 3:
time_calc.substraction();
break;
case 4: cout<<"Thank you for having used this system, Bye Bye!!!"; break;

default: cout<<"Error: Invalid option, Please try again" ;
}
} while (choice != 4);

}// end function menu

void sec_to_hours() //Conversion from seconds to hours and minutes
{ int seconds;

cout<<"Enter the number of seconds: " ;
cin>> seconds;
if (seconds >= 0 && seconds < 60)
cout<< "The number of seconds is " << seconds ;
else if (seconds >= 60 && seconds < 3600)
cout<< "There are " << seconds / 60 <<" minutes and " << seconds % 60
<<" seconds" ;
else if ( seconds >= 3600 && seconds < 86400 ) // 1 hours or more , 1 hour
= 3600 seconds
cout<< "There are " << seconds /3600 << " hours " << (seconds % 3600) /
60 <<" minutes " << seconds % 60 <<" seconds" ;
else if ( seconds >= 86400 ) //checking for days if seconds greater then
86400seconds ( 1 day)
{ cout<< "There is " << seconds / 86400 <<" days, " << (seconds % 86400)
/3600 << " hours " ;
cout<< ((seconds % 86400) %3600) / 60 <<" minutes " << (((seconds %
86400) %3600) % 60 ) %60 <<" seconds" ;
}
}

}; // end void time_to_seconds


void substraction()
{ int seconds, total_seconds;

time_to_seconds();
cout<<" Now enter the amount of seconds to subtract : " <<endl;
cin >> seconds ;

if ( time_to_seconds().seconds >= seconds)
cout<< " Substracting " << time_to_seconds.seconds <<" from " << seconds
<<" is: " << time_to_seconds().seconds - seconds ;
else
cout<< " The time has to be greater then the seconds. Please try again! "
;

}

void main()
{
Conversion time_calc;
time_calc.menu();
getch(); // Used in microsoft visual c++

}
 
K

Ken

Oups , here is the program:

#include <iostream>
#include <conio.h> // Used in microsoft visual c++
using namespace std;


class Conversion
{
public:
void menu();
void sec_to_hours();
void time_to_seconds();
void substraction();
};

void Conversion::time_to_seconds() //Converts time to seconds
{ int seconds1, seconds2, hours, minutes, seconds;
char colon;
cout<<"Enter a time (hours:minutes:seconds): " ;
cin>> hours >>colon >>minutes >>colon >> seconds;
if ( hours >= 0 && hours < 24 ) seconds1 = hours * 3600 ;
else cout<<"Invalid amounts of hours " <<endl;
if ( minutes >= 0 && minutes < 60) seconds2 = minutes *60;
else cout<<"Invalid amount of minutes " <<endl;
if ( seconds >= 0 && seconds< 60) ;
else cout<<"Invalid amount of seconds " <<endl;
if ( ( hours >= 0 && hours < 24 ) && ( minutes >= 0 && minutes < 60) &&
( seconds >= 0 && seconds< 60) )
seconds = seconds1 + seconds2 + seconds ;
cout<< " There is: " << seconds <<" seconds." <<endl;
}
void Conversion::substraction()
{ int seconds ;//total_seconds;
Conversion time_calc;
time_to_seconds();
cout<<" Now enter the amount of seconds to subtract : " <<endl;
cin >> seconds ;

if ( time_calc.time_to_seconds() >= seconds)
cout<< " Substracting " << time_to_seconds.seconds <<" from " << seconds
<<" is: " << time_to_seconds().seconds - seconds ;
else
cout<< " The time has to be greater then the seconds. Please try again! "
;

}


void Conversion::menu()
{
int choice;
Conversion time_calc;
do {
cout << endl << endl;
cout << " Time Management System "<<endl;
cout << " ============================================== "<<endl;
cout << " 1: Convert a number of seconds into a time "<<endl;
cout << " 2: Convert a time into a number of seconds "<<endl;
cout << " 3: Substrat a number of seconds from a time "<<endl;
cout << " 4: Quit "<<endl;
cout << " ============================================== "<<endl;

cout << " Your choice please: ";
cin >> choice;

switch (choice)
{
case 1:
sec_to_hours();
break;
case 2:
time_calc.time_to_seconds();
break;
case 3:
time_calc.substraction();
break;
case 4: cout<<"Thank you for having used this system, Bye Bye!!!"; break;

default: cout<<"Error: Invalid option, Please try again" ;
}
} while (choice != 4);

}// end function menu

void sec_to_hours() //Conversion from seconds to hours and minutes
{ int seconds;

cout<<"Enter the number of seconds: " ;
cin>> seconds;
if (seconds >= 0 && seconds < 60)
cout<< "The number of seconds is " << seconds ;
else if (seconds >= 60 && seconds < 3600)
cout<< "There are " << seconds / 60 <<" minutes and " << seconds % 60
<<" seconds" ;
else if ( seconds >= 3600 && seconds < 86400 ) // 1 hours or more , 1 hour
= 3600 seconds
cout<< "There are " << seconds /3600 << " hours " << (seconds % 3600) /
60 <<" minutes " << seconds % 60 <<" seconds" ;
else if ( seconds >= 86400 ) //checking for days if seconds greater then
86400seconds ( 1 day)
{ cout<< "There is " << seconds / 86400 <<" days, " << (seconds % 86400)
/3600 << " hours " ;
cout<< ((seconds % 86400) %3600) / 60 <<" minutes " << (((seconds %
86400) %3600) % 60 ) %60 <<" seconds" ;
}
} // end void time_to_seconds

void main()
{
Conversion time_calc;
time_calc.menu();
getch(); // Used in microsoft visual c++

}
 
I

Ian

Ken said:
hello,

I would to know if it is possible to call an object in a function within a
class.
Meaning , In a class, A function X calling onto a function Y, and function
Y we want one of the two calculation ( eg seconds , out of seconds and
minutes)
thanks ,

Ken
lerameur at yahoo.com

here is the program I wrote, but the function subtraction wich is calling
another function is not working, dont know how to access the info.
You've placed what should be class data members in the member function
time_to_seconds().

Hint:
class Conversion
{
int seconds1, seconds2, hours, minutes, seconds;

....
};

Then all member functions can access them, no need for odd things like
time_to_seconds.seconds.

Ian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,525
Members
44,997
Latest member
mileyka

Latest Threads

Top