Monday, May 24, 2010

Using C++: Is there a way to make this program a user defined number of names to be entered and sorted???

//Sort an array of 3 names


#include %26lt;iostream%26gt;


#include%26lt;string%26gt;


using namespace std;


void swap(string %26amp;n1, string %26amp;n2)


//receive strings n1 and n2, exchange


{ string temp;


temp=n1;


n1=n2;


n2=temp;


} //swap





void main() //input 3 names, sort, print


{ string names[3]; //3 names: [0],[1],[2]


int index;


for(index=0; index%26lt;3; index++)


{ cout%26lt;%26lt;"enter a name: ";


getline(cin,names[index]);


} //for loop


if (names[0]%26gt;names[1])


swap(names[0],names[1]);


if (names[1]%26gt;names[2])


swap(names[1],names[2]);


if (names[0]%26gt;names[1])


swap(names[0],names[1]);


for(index=0; index%26lt;3; index++)


cout%26lt;%26lt;names[index]%26lt;%26lt;"\n";


} //main

Using C++: Is there a way to make this program a user defined number of names to be entered and sorted???
//do this:


int nint;


cin%26gt;%26gt;nint;


string names[nint];


// and this


for(index =0;index%26lt;nint;index++)


{


//etc.
Reply:Uhm, are you asking is there a way to create a user defined data type? I don't think there is a such thing as a "user defined number" or what you said... Unless you are asking if there is a way to call that whole segment of code using a function with in a class? Or perhaps by using a structure.... I am not really to sure on what you are asking. Please try and be more clear so people can help you :)


No comments:

Post a Comment