Insert name into array

hello,

i have A task which is following:
Suppose NAME is an 8 elements array which stores five name: Ammar, Fatime, Hassan, Karishma, Owais. Insert a new name Maaz into name list by keeping track of alphabetic order of list.

so i have complete almost 90% the code is following:


#include<iostream>
#include<string>

using namespace std;
int main()
{
int k,j,i;
char name[10][10]={"Ammar","Fatima","Hasssan","Karishma","Owais"};
char name2[10]="Maaz";

for(i=0; i<=6; i++)

{

if(name2[0]<name[i][0])

{
k=i+1;
break;



}

}


for(j=5; j>=k; j--)

{

name[j]=name[j-1];

}

name[j-1]=name2[0];




system("pause");
}


when i try compile two erros are coming..

[Error] invalid array assignment..

[Error] incompatible types in assignment of 'char' to 'char [10]'

please help me to complete the program..
Last edited on
You're getting those errors because you're trying to put strings into an array of chars.

Also, that break is redundant in your 'if' statement, you leave the 'if' statement after all of the statements in the block run.

You could also clean up the code a bit by removing the int i, j, and k and instead creating those variables in the for loops themselves. i.e. for(int i = 0; i <= 6, i++)

If I'm not mistaken, the assignment says to create an array of 8 names, and you've created 2 arrays. One of them being multi-dimensional consisting on 100 elements, and the other being a regular array consisting on 10 elements.
Last edited on
so what i do to complete this program?
I would just make one array of 8 elements and fill it with all 6 names, then write a sort() function to put them in alphabetical order.
Topic archived. No new replies allowed.