I'm new at C++ ..and I dont know what should I do whith my program ...
this program's input for exg. is:
5
mary sara kimi lili olive
:
:
and then sorting them .. but my problem is that when I submit my home work they score me "0" and Icant find out why..!

#include <iostream>
#include <string.h>
#define MAXNAMES 100
using namespace std;

int compare(string s1, string s2){
  int i;
    for ( i = 0; s1[i] && s2[i]; ++i)
    {
        /* If characters are same or inverting the 6th bit makes them same */
        if (s1[i] == s2[i] || (s1[i] ^ 32) == s2[i])
           continue;
        else
           break;
    }

    /* Compare the last (or first mismatching in case of not same) characters */
    if (s1[i] == s2[i])
        return 0;
    if ((s1[i]|32) < (s2[i]|32)) //Set the 6th bit in both, then compare
        return -1;
    return 1;
}

int main() {
    string Names[MAXNAMES];
  int index[MAXNAMES], tmp;

  int n;
  cin>>n;
  for(int i=0; i<n; i++)
    cin>>Names[i];

  for(int i=0; i<n; i++)  index[i] = i;

  int min;

  for(int i=0; i<n-1; i++){
    min = i;
    for(int j=i+1; j<n; j++)
     if( compare(Names[index[j]], Names[index[min]]) < 0 ) {
       min = j;
     }
    if(min != i){
      tmp = index[i];
      index[i] = index[min];
      index[min] = tmp;
    }
  }


  for(int i=0; i<n; i++)  
   cout<<Names[index[i]]<<"\n";

    return 0;
}

Recommended Answers

All 2 Replies

Using this #include <string.h> instead of this #include <string> is causing a compiler error.

but I alraedy use this.. :(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.