wcypierre

[C++] String Comparison

Nov 23rd, 2011
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.26 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cstdlib>
  4.  
  5. int main(){
  6.     using namespace std;
  7.  
  8.     string name[10];
  9.     int people;
  10.     string min, max;
  11.  
  12.     cout << "How many people are there (1 to 10)? "<<endl;
  13.     cin  >> people;
  14.  
  15.     while(people<1 || people>10){
  16.         cout<<"Maximum size should be between 1 and 10."<<endl;
  17.         cout<<"Please re-enter the number of people:"<<endl; cin>>people;
  18.     }
  19.  
  20.     cout<<"Enter person 1's name :"<<endl;
  21.     cin>>name[0];
  22.  
  23.     for(int i=0; i<people-1; i++){
  24.         cout << "Enter the next person's name"<<endl;
  25.         cin  >> name[i+1];
  26.  
  27.         if(i == 0){
  28.             if(name[i].compare(name[i+1]) > 0){
  29.                 max = name[i];
  30.                 min = name[i+1];
  31.             }
  32.             else if(name[i].compare(name[i+1]) < 0){
  33.                 max = name[i+1];
  34.                 min = name[i];
  35.             }
  36.         }
  37.         else{
  38.             if(name[i+1].compare(max) > 0){
  39.                     max = name[i+1];
  40.             }
  41.             else if(name[i+1].compare(min) < 0){
  42.                 min = name[i+1];
  43.             }
  44.         }
  45.     }
  46.  
  47.     cout << min << " is at the head of the line" << endl;
  48.     cout << max << " is at the end of the line" << endl;
  49.  
  50.     system("pause");
  51. }
  52.  
  53.  
Advertisement
Add Comment
Please, Sign In to add comment