Sign in to follow this  

Question about arrays...

This topic is 4748 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok, I have this class assignment that asks us to use a function template called min to determine the smaller of two *arguements*. It then wants us to test the program using integer, character and floating point numbers. I've created a program that tests this, but i'll be used 6 differant variables... Let me show you what I mean.
#include <iosteram>

using std::namespace;

template < class T >
T min( T value1, T value2 )
{
if( value1 < value2 )
return value1;
else
return value2;
}


int main()
{
int int1;
int int2;

cout << "Input two integer values: ";
cin >> int1 >> int2;

cout << "The smaller integer value is: " << min( int1, int2 );

char char1;
char char2;

cout << "\n\nInput two characters: ";
cin >> char1 >> char2;

cout << "The smaller character value is: " << min( char1, char2 );

float flo1;
float flo2;

cout <<"\n\n Input two float values: ";
cin >> flo1 >> flo2;


cout << "The smaller float value is: " << min( flo1, flo2 )
<< endl;

return 0;

}




Now to my question. Instead of having 3 seperate prompts for values, is it possible to to have only one prompt? Hope this helps. Or am I missing something completely? Thanks for the help and the time you took to read this. :) [Edited by - Clover_Maximus on December 14, 2004 11:05:53 AM]

Share this post


Link to post
Share on other sites
You're missing something completely. I don't really understand your question but you can't have an array where each element is of different data types. You could :



#include <iostream>

using namespace std ;

template< class dataType >
dataType min( dataType a, dataType b ) ;

int main( )
{

int intArray[2] ;
char charArray[2] ;
float float Array[2] ;

// Prompt user for two ints.
// Save user input in both elements of the int array.

// Prompt user for two chars.
// Save user input in both elements of the char array.

// Prompt user for two floats.
// Save user input in both elements of the float array.

cin.get( ) ;
return 0 ;
}


Share this post


Link to post
Share on other sites
Quote:
Original post by Khaosifix
Quote:

cout <<"\n\n Input two float values: ";
cin << flo1 << flo2;


And the cin uses the '>>' operator not the other one.


Ha...yeah. I should of double checked that, Thank you!

I think Frunny answered what I was trying to say but let me rephrase if I can.

Instead of prompting 3 seperate times, I only want to prompt once for the input.

Is that possible?

Share this post


Link to post
Share on other sites
Heh, you could always go:


int int1, int2;
char char1, char2;
float flo1, flo2;
cout << "Input two integers, followed by two characters, followed by two floating point values:" << endl;
cin >> int1 >> int2 >> char1 >> char2 >> flo1 >> flo2;
// find mins and output results




However, I kind of doubt that that's what you were looking for.

Share this post


Link to post
Share on other sites
I think he might be looking for this?

template <typename T>
void test_input(std::string string_name) {
T x;
T y;
// input into x and y
// invoke min()
// report result
}

test_input<int>("integers");
// similarly other types

Share this post


Link to post
Share on other sites
you could do a statement like this

#include <iosteram>
#include <string>
using std::namespace;

template < class T >
T min( T value1, T value2 )
{
if( value1 < value2 )
return value1;
else
return value2;
}


int main()
{

string reader = ""
cout << "imput for:"
cin >> reader;
if(reader == "float")
{
float flo1;
float flo2;

cout <<"\n\n Input two float values: ";
cin >> flo1 >> flo2;
cout << "The smaller float value is: " << min( flo1, flo2 );

system("pause")
return 0;
}
...
//do that with all of em then
else
cout << "invalid data type";
system("pause")
return 0;
};



that would ask the data type, prompt you to imput 2 of that kind of data, run your function on it, print the result, print "PRESS ANY KEY TO CONTINUE" then once you press a key it closes

You will want to modify "the smaller float value is" to fit the other data type and some other stuff too but i hop you get what i mean
[smile]




Share this post


Link to post
Share on other sites

This topic is 4748 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this