5 Replies - 3778 Views - Last Post: 15 July 2012 - 12:39 AM Rate Topic: -----

#1 mgrex  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 188
  • Joined: 25-March 10

Question regarding an approach with templates, Max/Min Values

Posted 14 July 2012 - 09:00 PM

Basically I want the cin statements to accept any of the major types from the user, whether he or she want's to compare numbers of different types or of the same types. It would be appreciated, if someone told me the wisest approach.

The compiler refuses the following code to me trying to assign num1 and num2 to different types at once.

#include <iostream> 
using namespace std;

template <class T1, class T2>		//at most user can compare 2 different types

//T maximum(T1, T2);
//T minimum(T1, T2);
void maximum(T1 & n1, T2 & n2)
{
	cout << ( n1 > n2 ? n1 : n2 ) << " is greater." << endl;
}

//void minimum (T1 n1, T2 n2)

int main() 
{ 

	//T1 num1;
	//T2 num2;
	int num1;
	double num1;

	int num2;
	double num2;

	char num1;
	char num2;

	string num1;
	string num2;

	cout << "Enter a number: ";
	cin >> num1;
	cout << endl;

	cout << "Enter a 2nd number: " << endl;
	cin >> num2;
	cout << endl;

	maximum(num1, num2);

	system("pause");
	return 0 ; 
} 


Is This A Good Question/Topic? 0
  • +

Replies To: Question regarding an approach with templates, Max/Min Values

#2 snoopy11  Icon User is online

  • Engineering ● Software
  • member icon

Reputation: 778
  • View blog
  • Posts: 2,303
  • Joined: 20-March 10

Re: Question regarding an approach with templates, Max/Min Values

Posted 14 July 2012 - 10:17 PM

The wisest approach would be
to setup two more variables of type string call them input1 and input2
do some error checking to see if input IsAlpha
convert string to int or double using stringstream

stringstream(input1) >> num1;




Snoopy.

This post has been edited by snoopy11: 14 July 2012 - 10:19 PM

Was This Post Helpful? 0
  • +
  • -

#3 mgrex  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 188
  • Joined: 25-March 10

Re: Question regarding an approach with templates, Max/Min Values

Posted 15 July 2012 - 12:05 AM

The instructions call for using a template class.

Write templates for the 2 functions minimum and maximum.
 The minimum function should accept two arguments and return the value of the argument that is the lesser of the two. 
The maximum function should accept two arguments and return the value of the argument that is the greater of the two. Design a simple driver program that demonstrates the templates with various data types.



The instructions call for using a template class.

Quote

Write templates for the 2 functions minimum and maximum.
The minimum function should accept two arguments and return the value of the argument that is the lesser of the two.
The maximum function should accept two arguments and return the value of the argument that is the greater of the two. Design a simple driver program that demonstrates the templates with various data types.

Was This Post Helpful? 0
  • +
  • -

#4 snoopy11  Icon User is online

  • Engineering ● Software
  • member icon

Reputation: 778
  • View blog
  • Posts: 2,303
  • Joined: 20-March 10

Re: Question regarding an approach with templates, Max/Min Values

Posted 15 July 2012 - 12:19 AM

Well use a template then,

Im not stopping you...

an idea based on your code
#include <iostream>
#include <sstream>
using namespace std;

template <class T1, class T2>		//at most user can compare 2 different types

//T maximum(T1, T2);
//T minimum(T1, T2);
void maximum(T1 & n1, T2 & n2)
{
	cout << ( n1 > n2 ? n1 : n2 ) << " is greater." << endl;
}

//void minimum (T1 n1, T2 n2)

int main()
{

	//T1 num1;
	//T2 num2;
	double num1;
    double num2;
    string input1, input2;




	cout << "Enter a number: ";
	cin >> input1;
	stringstream(input1) >> num1;
	//do error checking
    cout << endl;

	cout << "Enter a 2nd number: " ;
	cin >> input2;
	stringstream(input2) >> num2;
	//do error checking
	cout << endl;

	maximum(num1, num2);
    cout << "Press Enter Key to Exit";
	cin.ignore();
	cin.get();
	return 0 ;
}




Was This Post Helpful? 1
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3576
  • View blog
  • Posts: 11,125
  • Joined: 05-May 12

Re: Question regarding an approach with templates, Max/Min Values

Posted 15 July 2012 - 12:32 AM

Although I admire you reaching to dynamically determine the type of data entered by the user at runtime, all that your assignment requires of you is to call your min/max() functions with different types. You don't have to determine the data type of the entered data, and then create variables of that type on the fly, and then pass them to your min/max() functions.

If think something like the following would suffice to satisfy the requirements of your assignment:
template <class T>
int CountBit(T value)
{
    // WARNING: this code may be counting bits incorrectly
    int count = 0;
    T lsb = 1;
    while (value != 0)
    {
        count += value & lsb;
        value >>= 1;
    }
    return count;
}

int main()
{
    // Demonstrate counting bits for various types.
    int n = 12;
    cout << "int " << n << " has " << CountBits(n) << " bits turned on." << endl;

    long l = 55;
    cout << "long " << n << " has " << CountBits(l) << " bits turned on." << endl;

    unsigned int ui = 49;
    cout << "unsigned int " << n << " has " << CountBits(ui) << " bits turned on." << endl;

    unsigned long ul = 72;
    cout << "unsigned long " << n << " has " << CountBits(ul) << " bits turned on." << endl;

    char ch = 127;
    cout << "char " << n << " has " << CountBits(ch) << " bits turned on." << endl;
}


Was This Post Helpful? 1
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3576
  • View blog
  • Posts: 11,125
  • Joined: 05-May 12

Re: Question regarding an approach with templates, Max/Min Values

Posted 15 July 2012 - 12:39 AM

The reason I'm telling you not to try to determine types dynamically is because you'll forever be chasing corner cases if you try to determine types at runtime. For example: If the user enters "12" and "127", what type do you use: int, char, unsigned int, unsigned char, float, double? What type do you choose if the user enters "12/27/1995" and "May 15, 2001"? Do you compare these as strings? Or do you parse these into a struct tm? And what if the input are potentially incompatible types like "A" and "65.34e-5"?
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1