first of all, Thanks for your reply.
If I use strings the program stops automatically at the second name input
because the "string type"hasn't a specific byte dimension
and than sizeof(agenda) isn't correct.
the add contact function(to explain):
1 2 3 4 5 6 7 8
|
void aggiungi(agenda *&array,int elementi){ //Add function, operands (array's pointer,number of elements)
array=(agenda*)realloc(array,elementi*sizeof(agenda)); //Add 30 byte in the array |(sizeof(agenda)=30byte)|
cout<<"Inserire il nome del contatto\n";
cin>>array[elementi-1].nome;
cout<<"Inserire il numero del contatto\n";
cin>>array[elementi-1].cell;
}
| |
the condition will be "lettera<variable" where variable is the length of the shortest word
(i know that isn't totally correct...but i can't imagine to other solutions...)
so... I've thought about the order function, with my pencil and a paper sheet, this is working:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
void ordina(agenda* &array,int elementi){ //Order Function, operands:"Array"(only the pointer),and the number of elements
int i;
int ciccio; //Needs to know if a change has been made
int lettera=0; //Indicate wich character are we comparing
do{
ciccio=0;
for(i=0;i<elementi-1;i++){ //For each element(-1) of the array
if(array[i].nome[lettera]>array[i+1].nome[lettera]){cout<<"Invertiti: "<<array[i].nome<<" e "<<array[i+1].nome<<"\n"; //Compare the character that we are comparing whith the next element's character
inverti(array,i,i+1);ciccio++; //if The first one is above, reverse them,also does cicco+1
}
if(lettera!=0){
if(array[i].nome[lettera-1]==array[i+1].nome[lettera-1]){ //else,if them are equal, it compares the next char.
if(array[i].nome[lettera]>array[i+1].nome[lettera]){
inverti(array,i,i+1);ciccio++;} //if the first one is above, reverse them. also does ciccio+1
}
}
}
if(ciccio==0){lettera++;} //If ciccio doesn't change, it go to the next char
}while(lettera<=3);
}
| |
but it still not work.... I'm getting crazy
**EDIT**: i saw now that you suggested me to use classes instead of structures.
but I haven't studied classes yet (probably i'm going to study them in the next hours...but this exercise is a school one, so i can't use something that we haven't studied at school)