Question about arrays...

Started by
6 comments, last by raptorstrike 19 years, 4 months ago
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]
Advertisement
No. C++ uses static typing.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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 ;}
---http://www.michaelbolton.comI constantly dream about Michael Bolton.
Quote:
cout <<"\n\n Input two float values: ";
cin << flo1 << flo2;


And the cin uses the '>>' operator not the other one.
---http://www.michaelbolton.comI constantly dream about Michael Bolton.
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?
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.
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Formats______________________________________________________________________________________
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
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;elsereturn 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]




____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie

This topic is closed to new replies.

Advertisement