-4

I m sorting words by comparing ascii of the 1st element of the words with another word. I have used structures to store and an array to store 5 words and then using sorting comparing asciis. Also I want it without using any character function. What wrong am I doing?

error the compilor is giving: invalid conversion from char to int, invalid array assignment, expected primary expression before int

#include <iostream>
#include <cctype>
#include <cstring>
using namespace std;


struct Words{
char name[20];
};

int main(){

Words words[5];


for(int i=0; i<5; i++){

    cout<<"Enter the name"<<endl;
    cin>>words[i].name;

}
int temp;
//sorting

for(int i=0; i<5; i++){
//ascii code

    for(int j=0; j<4; j++){
        if(words[j+1].(int)name[0]<words[j].(int)name[0]){
            temp=words[j].name;
            words[j].name=words[j+1].name;
            words[j+1].name=temp;
        }


    }
}

cout<<endl;
//output
for(int i=0; i<5; i++){

    cout<<words[i].name<<endl;


}

return 0;
}
user3100177
  • 65
  • 1
  • 9
  • 1
    We will not inform you about your errors. You tell us and we will try to help you to remove that. – haccks Dec 14 '13 at 09:51
  • error the compilor is giving: invalid conversion from char to int, invalid array assignment, expected primary expression before int – user3100177 Dec 14 '13 at 09:52
  • 1
    So `using namespace std`. Then why do you tag this as `c`? This is not C code. And anyway, resolving a trivial compiler error is out of scope here. Learn basic syntax yourself. –  Dec 14 '13 at 09:58

3 Answers3

0

In the line

    if(words[j+1].(int)name[0]<words[j].(int)name[0]){

you try to cast a member variable from char to int using the wrong syntax. You can't write the cast in between the member access dot and the member name. You have to write it in front of the whole expression to be cast:

    if((int)words[j+1].name[0]<(int)words[j].name[0]){

Also, you should consider what to do in case two words have the same first character but differ otherwise. If you'd use std::string you could simply compare those strings. And by the way, there is std::sort.

leemes
  • 44,967
  • 21
  • 135
  • 183
  • I tried this but still giving me error but less than before. Error as follows : invalid array assignment, invalid conversion from char to int. – user3100177 Dec 14 '13 at 10:17
0

This statement

    if(words[j+1].(int)name[0]<words[j].(int)name[0]){

is syntactically invalid. The correct statement will look the following way

    if( ( int )words[j+1].name[0]<( int )words[j].name[0]){

However there is no any sense to make such a record because (the C++ Standard)

The usual arithmetic conversions are performed on operands of arithmetic or enumeration type

On the other hand if type char behave as signed char and your array can contain characters with negative values then the sorting will be invalid. I advice to rewrite the statement the following way

    if( ( unsigned char )words[j+1].name[0]<( unsigned char )words[j].name[0]){

Also these statement are invalid

        temp=words[j].name;
        words[j].name=words[j+1].name;
        words[j+1].name=temp;

Arrays (variable temp shall be defined as char[20]) have no the assignment operator. Either use standard C function strcpy or define the arrays as std::array<char,20>

For example (you need include header

        strcpy( temp, words[j].name );
        strcpy( words[j].name, words[j+1].name );
        strcpy( words[j+1].name, temp );

Or ( you need include header )

struct Words{
 array<char, 20> name;
};

array<char, 20> temp;
            temp=words[j].name;
            words[j].name=words[j+1].name;
            words[j+1].name=temp;
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Thanks a lot, it worked just tell me the use of unsigned char for my knowledge. – user3100177 Dec 14 '13 at 10:41
  • For example let assume that the code of one character (in hexadecimal) is 0x80 and the code of other character is 0x50. You would want that the character with code 0x50 precedes the character with code 0x80. However the value of the character with code 0x80 is negative and it will precedes the chatacter with code 0x50. If you will compare them as unsigned characters then the character with value 0x50 will precedes the character with value 0x80. – Vlad from Moscow Dec 14 '13 at 10:51
0

you should also change the type of temp from int to char, since name is a character variable (invalid conversion from char to int is due to this error)

fasadat
  • 1,025
  • 1
  • 12
  • 27