Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This program will ask for the list of ten names then sort the names in ascending order (from A – Z).

My problem is that the program won't run because it has an error.
C#
#include<iostream.h>

main()
{
 char str[10];
 char g;

 for(int j=0; j<10; j++)
  {
     cin>>str[j];
    for(int i=0; i<10; i++)
     {
      if(str[i]>str[i+1])
        {
         g=str[i];
         str[i]=str[i+1];
         str[i+1]=g;
        }
     }
  }
 for(int y=0; y<10; y++)
  {
    cout<<str[y];
  }
 return 0;
}

Linker Warning: No module definition file specified: using defaults
So whats wrong with my program? I really don't understand the error.
Please help me revise my code so that it will run successfully.
Posted
Updated 3-Jul-14 13:16pm
v2
Comments
Mohibur Rashid 3-Jul-14 20:17pm    
Did you write the code?
[no name] 3-Jul-14 21:33pm    
If whatever compiler you are using is more than 5 years old, upgrade to a modern compiler.
Member 10011989 4-Jul-14 13:16pm    
yes agree with wes ..
plz use latest compiler

C++
#include<iostream.h>

int main(int argc, char* argv[])
{

 char *str[10];
 char *g;
 
 for(int j=0; j<10; j++)
  {
	 str[j]= new char[255];
     cin>>str[j];
 }

 for(int k=9;k>=0;k--)
    for(int i=0; i<k; i++)
     {
      if(*str[i]>*str[i+1])
        {
         g=str[i];
         str[i]=str[i+1];
         str[i+1]=g;
        }
     }
	cout << "----" <<endl;
  
 for(int y=0; y<10; y++)
  {

    cout<<str[y]<< endl;
	delete[] str[y];
  }

 return 0;
}


Note:
1. char *str[10] is an array of 10 char pointers
2. so you need to allocate memory for these pointer thus
str[j]= new char[255];
3. for g to refer to str[i], g must also be a pointer
char *g
4. we need clean up memory before exit thus
delete[] str[y]
 
Share this answer
 
v2
Hey, this is C++! Why don't you use std::string, std::vector, and std::qsort?
Your coding is plain old C.
Cheers
Andi
 
Share this answer
 
hello
if you looking up again your program you see that the str array isn't a string array but just character array may be this is the problem
you sould be use the declaration bellow:

C++
char *str[10];


After you can use one of sorting algorithms (e.g sort by selection)
 
Share this answer
 
Comments
GwapoKho 3-Jul-14 19:17pm    
It appears to be like this
Compiling NONAME00.CPP:
Error NONAME00.CPP 15: Cannot convert 'char *' to 'char' in function main()
Error NONAME00.CPP 17: Cannot convert 'char' to 'char *' in function main()
Warning NONAME00.CPP 26: 'g' is assigned a value that is never used in function main()

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900