Hello everyone,
I was having this problem of sorting words of a string entered by user where the user enters many strings and words of each string are sorted separately. This had to be done using pointers . Heres my attempt.I dont know where I am going wrong as garbage is being stored in some of the char pointers.
Please help.
Thanks,
comwizz

//Program to read a set of lines and sort each word in the line
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
 int i=0,j=0;
 char *temp,*p;
 int w_len,l_no,q;
 cout<<"Enter the number of lines you want to enter ";
 cin>>l_no;
 char ***a;
 a=new char**[l_no];
 for(j=0;j<l_no;i++)
 {
  cout<<"Enter the number of words in the line"<<i+1<<":"<<endl;
  cin>>w_len;
  *(a+j)=new char*[w_len];
  cout<<"Enter the line"<<endl;
  cin.getline(p,150,-1);
  char *y;
  p++;
  y++;
  for(i=0;i<w_len;i++)
  {
   int count=0;
   y=p;
   while((*p!=' ')&&(*p!='.')&&(*p!='\0'))
   {
    count++;p++;
   }
   p++;
   *(*(a+j)+i)=new char[count];
   strncpy(*(*(a+j)+i),y,count);
  }
  for(i=0;i<w_len;i++)
  {
   for(int k=0;k<w_len-i-1;j++)
   {
    if((strcmp(*(*(a+j)+i),*(*(a+j)+i+1)))>0)
    {
     temp=*(*(a+j)+i);
     *(*(a+j)+i)=*(*(a+j)+i+1);
     *(*(a+j)+i+1)=temp;
    }
   }
  }
 }
 for(j=0;j<l_no;j++)
 {
  cout<<"The correct order for the line"<<j+1<<"is"<<endl;
  for(i=0;i<w_len;i++)
  {
   cout<<*(*(a+j)+i)<<endl;
  }
 }
}

your program is littered with uninitialized pointers! set all pointers to 0 when declared and it will make your debugging a lot easier. Replace all those pointer arithmetic with normal array indexing. Make sure the program allocates memory before using it.

// replace this
 *(a+j)=new char*[w_len]; // exception attemtping to dereference uninitialized pointer
// with this
  a[j]=new char*[w_len];

// using uninitialized pointer and unallocated [b]p[/b]
  cin.getline(p,150,-1);

and replace obsolete <iostream.h> with <iostream>

Unless you are required to use char pointers, you should consider replacing them with std::vector and std::string and eliminate all that messey memory allocations.

hi comwizz ;) ,
n hi 2 every1 else readin d reply :) . m a new member in the club.i dont get exactly wat u hve typed.... but i see in the foll code u has used j as a variable n incremented i... so look out..
/*
for(j=0;j<l_no;i++)
{
*/

hi comwizz ;) ,
n hi 2 every1 else readin d reply :) . m a new member in the club.i dont get exactly wat u hve typed.... but i see in the foll code u has used j as a variable n incremented i... so look out..
/*
for(j=0;j<l_no;i++)
{
*/

A good way to avoid these sorts of bugs is to only declare the loop variable at the point you actually need to use it.
As far as I can see, the program does not need j anywhere outside the scope of the for loops in which it is used.

one remedy could be

for (int j=0; j<l_no; j++)  {}
Comments
Good:sunny

Heres my corrected code. Still , I am having problems , that when two lines are entered by the user , after sorting the words of the first line , the word length of second line as well as the contents of the second line are not taken. Why is this happennin'?? Also I have to do the entire exercise using pointers and not use vectors or arrays. any inputs are welcome.
Thanks,
comwizz
Heres the code.

//Program to read a set of lines and sort each word in the line
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
 int i=0,j=0;
 char *temp=0,*p=0;
 int w_len[10],l_no,q;
 cout<<"Enter the number of lines you want to enter ";
 cin>>l_no;
 char ***a;
 a=new char**[l_no];
 for(j=0;j<l_no;j++)
 {
  cout<<"Enter the number of words in the line"<<i+1<<":"<<endl;
  cin>>w_len[j];
  *(a+j)=new char*[w_len[j]];
  cout<<"Enter the line"<<endl;
  p=new char[40];
  cin.getline(p,150,-1);
  char *y;
  p++;
  y++;
  for(i=0;i<w_len[j];i++)
  {
   int count=0;
   y=p;
   while((*p!=' ')&&(*p!='.')&&(*p!='\0'))
   {
    count++;p++;
   }
   p++;
   *(*(a+j)+i)=new char[count];
   strncpy(*(*(a+j)+i),y,count);
   a[j][i][count]='\0';
  }
  for(i=0;i<w_len[j];i++)
  {
   for(int k=0;k<w_len[j]-i-1;k++)
   {
    if((strcmp(*(*(a+k)+i),*(*(a+k)+i+1)))>0)
    {
     temp=*(*(a+j)+i);
     *(*(a+j)+i)=*(*(a+j)+i+1);
     *(*(a+j)+i+1)=temp;
    }
   }
  }
 }
 for(j=0;j<l_no;j++)
 {
  cout<<"The correct order for the line"<<j+1<<"is"<<endl;
  for(i=0;i<w_len[j];i++)
  {
   cout<<*(*(a+j)+i)<<endl;
  }
 }
}

I would need help. It would be great if you could help me.
Thanks,
comwizz

hi comWizz,
look for red n thnk abt it...n if u r in the pc rite now do send back n mail or reply back here

/*
//Program to read a set of lines and sort each word in the line
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
 int i=0,j=0;
 char *temp=0,*p=0;
 int w_len[10],l_no,q;
 cout<<"Enter the number of lines you want to enter ";
 cin>>l_no;
 char ***a;
 a=new char**[l_no];
 for(j=0;j<l_no;j++)
 {
  cout<<"Enter the number of words in the line"<<i+1<<":"<<endl;
  cin>>w_len[j];
  *(a+j)=new char*[w_len[j]];
  cout<<"Enter the line"<<endl;
  p=new char[40];
  cin.getline(p,150,-1);
  char *y;
  p++;
  ///y++;///

without ne intializiation or assignment increment thnk abt it

for(i=0;i<w_len[j];i++)
  {
   int count=0;
   y=p;
   while((*p!=' ')&&(*p!='.')&&(*p!='\0'))
   {
    count++;p++;
   }
   p++;
   *(*(a+j)+i)=new char[count];
   strncpy(*(*(a+j)+i),y,count);
   a[j][i][count]='\0';
  }
  for(i=0;i<w_len[j];i++)
  {
   for(int k=0;k<w_len[j]-i-1;k++)
   {
    if((strcmp(*(*(a+k)+i),*(*(a+k)+i+1)))>0)
    {

---

///     temp=*(*(a+j)+i);
     *(*(a+j)+i)=*(*(a+j)+i+1);
     *(*(a+j)+i+1)=temp;
////
comparin *(*(a+k)+i),*(*(a+k)+i+1          n
swappin *(*(a+j)+i),*(*(a+j)+i+1);

---

    }
   }
  }
 }
 for(j=0;j<l_no;j++)
 {
  cout<<"The correct order for the line"<<j+1<<"is"<<endl;
  for(i=0;i<w_len[j];i++)
  {
   cout<<*(*(a+j)+i)<<endl;
  }
 }
}

i ll just run it n reply back.. i typed a pointer rong so need to rectify n then i ll reply...

Edited 3 Years Ago by diafol: fixed formatting

Heres my corrected code. Still , I am having problems , that when two lines are entered by the user , after sorting the words of the first line , the word length of second line as well as the contents of the second line are not taken. Why is this happennin'?? Also I have to do the entire exercise using pointers and not use vectors or arrays. any inputs are welcome.
Thanks,
comwizz
Heres the code.

//Program to read a set of lines and sort each word in the line
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
 int i=0,j=0;
 char *temp=0,*p=0;
 int w_len[10],l_no,q;
 cout<<"Enter the number of lines you want to enter ";
 cin>>l_no;
 char ***a;
 a=new char**[l_no];
 for(j=0;j<l_no;j++)
 {
  cout<<"Enter the number of words in the line"<<i+1<<":"<<endl;
  cin>>w_len[j];
  *(a+j)=new char*[w_len[j]];
  cout<<"Enter the line"<<endl;
  p=new char[40];
  cin.getline(p,150,-1);
  char *y;
  p++;
  y++;
  for(i=0;i<w_len[j];i++)
  {
   int count=0;
   y=p;
   while((*p!=' ')&&(*p!='.')&&(*p!='\0'))
   {
    count++;p++;
   }
   p++;
   *(*(a+j)+i)=new char[count];
   strncpy(*(*(a+j)+i),y,count);
   a[j][i][count]='\0';
  }
  for(i=0;i<w_len[j];i++)
  {
   for(int k=0;k<w_len[j]-i-1;k++)
   {
    if((strcmp(*(*(a+k)+i),*(*(a+k)+i+1)))>0)
    {
     temp=*(*(a+j)+i);
     *(*(a+j)+i)=*(*(a+j)+i+1);
     *(*(a+j)+i+1)=temp;
    }
   }
  }
 }
 for(j=0;j<l_no;j++)
 {
  cout<<"The correct order for the line"<<j+1<<"is"<<endl;
  for(i=0;i<w_len[j];i++)
  {
   cout<<*(*(a+j)+i)<<endl;
  }
 }
}

Sometimes it baffles me as to what they teach people nowadays. Which scares me, because in a few years I hope to do a degree in computer science.

Your teacher is asking you to use the c++ language to do an assignment with just one caveat - you can't actually use a single function associated with c++ to help make things easier. :rolleyes: Does that not sound odd to you?

//Program to read a set of lines
//and sort each word in the line
#include<iostream>
#include<string>
#include<vector>
using namespace std;

int main()
{
int lines;
cout<<"How many lines do you want to enter?"<<endl;
cin>>lines;
cin.ignore();

cout<<"So type them in then retard...\n\n"<<endl;

string burp;
string pedantic;
vector <string> iamthwee;
vector <string> array;
vector <string> pfft;

for(int i=0; i<lines; i++)
{
cout<<i+1<<":";
getline(cin, burp, '\n');

iamthwee.push_back(burp);


while (burp.find(" ", 0) != string::npos)
{
size_t pos = burp.find(" ", 0);
pedantic = burp.substr(0, pos);
burp.erase(0, pos + 1);
array.push_back(pedantic);
}

array.push_back(burp);
sort(array.begin(),array.end());


string crapola="";
for(int j=0; j<array.size(); j++)
{
crapola=crapola+array[j]+" ";
}
pfft.push_back(crapola);
cout<<"\n";

array.erase(array.begin(),array.end());
}
cout<<"Those lines in alphabetical order...."<<endl;
for(int jkl=0; jkl<pfft.size(); jkl++)
{
cout<<jkl+1<<":"<<pfft[jkl]<<endl;
}

cin.get();

}

Your teacher is asking you to use the c++ language to do an assignment with just one caveat - you can't actually use a single function associated with c++ to help make things easier. :rolleyes: Does that not sound odd to you?

Actually comwizz has learnt c and is still learning c++... so in the assignment it(the program) requires the programmer to use basically c logics with cin and cout statments and include iostream header files.. without using all the string and vectors concepts...

Actually comwizz has learnt c and is still learning c++... so in the assignment it(the program) requires the programmer to use basically c logics with cin and cout statments and include iostream header files.. without using all the string and vectors concepts...

A pointless endeavour. Good luck, when you two actually graduate. :eek:

>I dont know where I am going wrong as garbage is being stored in some of the char pointers.
You're overcomplicating the problem. Compare and contrast your solution with this one:

#include <iostream>
#include <iomanip>
#include <cstring>

using namespace std;

void jsw_insertion ( char **a, int n )
{
  for ( int i = 1; i < n; i++ ) {
    char *save = *(a + i);
    int j;

    for ( j = i; j >= 1 && strcmp ( *(a + (j - 1)), save ) > 0; j-- )
      *(a + j) = *(a + (j - 1));

    *(a + j) = save;
  }
}

int main()
{
  int lines;

  cout<<"Number of lines: ";
  cin>> lines;

  while ( --lines >= 0 ) {
    int words;

    cout<<"Number of words: ";
    cin>> words;

    char **line = new char*[words];
    char buffer[1024];

    // Gather words in a line
    for ( int i = 0; i < words; i++ ) {
      cin>> setw ( sizeof buffer ) >> buffer;
      *(line + i) = new char[strlen ( buffer ) + 1];
      strcpy ( *(line + i), buffer );
    }

    jsw_insertion ( line, words );

    cout<<"The sorted line is: ";
    for ( int i = 0; i < words; i++ )
      cout<< *(line + i) <<' ';
    cout<<'\n';

    // Tidy up
    while ( --words >= 0 )
      delete [] line[words];
    delete [] line;
  }
}

Hi narue,
I shall try and understand the code given by you and run it.. I need to understand the "buffer" and hopefully I will..

What is "iomanip" included for ...? I suppose "cstring" is combined version of "string" and "ctype"...??? :?:

Thanks you were the first to reply with accordance to our need.. You seem to be helpful .... :) and so try and help jobe in " sol to ex. 4-6 in Acc. C++" as i made him write a long post with his/her code and it appeared out of my scope at this moment.. I will see the code and reply back..

thanxs again!!!

> What is "iomanip" included for ...?
iomanip is included so that I could use the setw manipulator with cin's >> operator. Reading strings with cin's >> operator is dangerous because most people don't take precautions against buffer overflow. setw does that.

>I suppose "cstring" is combined version of "string" and "ctype"...???
cstring is the C++ equivalent of string.h.

>so try and help jobe in " sol to ex. 4-6 in Acc. C++"
I decided that not replying would be more instructive for JoBe. Be aware that I read every thread, so please don't direct me to questions on the assumption that I haven't seen them.

I decided that not replying would be more instructive for JoBe. Be aware that I read every thread, so please don't direct me to questions on the assumption that I haven't seen them.

I wasn't directing you I was just requesting....!!!!!! I thought you were so helpful to me so I had expected you to be helpful to me in helping others because I couldn't!! Anyways Thats your wish how u look at things and understand them!!!

GOOD BYE!!!

Hello narue,
I used the code and it ran correctly.But few questions..
1. Null Pointer Assignment warning gets printed on the screen when the pointers are not allocated memory but u have everywhere except in save pointer in the function jsw_insertion.. I allocated it the memory but still the warning appeared???

2.Also while u used the buffer in the input ...Well I don't exactly know iomanip so I will try seeing that ... But one thing in that while compiling the code step by step it (cin) was executed many times(specified) ... i.e. in the first time it took the line but the subsequent times it didn't... Also in the code of comwizz if u c and run the program once after the input through cin next time the it is executed without input from the user..why so???

3. Also the question was to read the set of lines together and then print the sorted version together..

The code given by you was great.. answering our question almost and very small indeed. it was very simple for a a new programmer to understand!! GREAT!!
Thanxs a lot!!!
HackWizz

>Also the question was to read the set of lines together and
>then print the sorted version together..
You neglected to mention that requirement.

>2 <snip>
I didn't understand a word of that. Give me an example of what the program is doing and what you think it should be doing.

>but u have everywhere except in save pointer in the function jsw_insertion
Correct. jsw_insertion works with pointers to existing memory, so there's no need to allocate anything new. All it does is move around addresses.

>I allocated it the memory but still the warning appeared???
Can you be more specific?

Yes,I am sorry I forgot to mention that..!

For the warning ... After the program runs completely on the output screen I have "Null Pointer Assignment" written...
That was what I was asking.. Is that due to unallocated pointer??

While I used F8 to understand the program ... the line cin while taking the input to gather the words in the first F8 it takes input from the user and then pressing F8 repeatedly while highlighting the line it does not take the input and moves on...

Even in comwizz's code while inputing the no of words using the array after the first line is inputed next time instead of taking the input from the user it takes some garbage like
-23568... so is it some bug in cin or an error in the code??

This article has been dead for over six months. Start a new discussion instead.