| | |
Problem of sorting words of each string using pointers
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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
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
C++ Syntax (Toggle Plain Text)
//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.
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.
// 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 p
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.
•
•
•
•
Originally Posted by HackWizz
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++)
{
*/
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
C++ Syntax (Toggle Plain Text)
for (int j=0; j<l_no; j++) {}
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.
Thanks,
comwizz
Heres the code.
C++ Syntax (Toggle Plain Text)
//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; } } }
•
•
Join Date: Feb 2006
Posts: 54
Reputation:
Solved Threads: 2
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;
}
}
}
[/code][/QUOTE]
i ll just run it n reply back.. i typed a pointer rong so need to rectify n then i ll reply...
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;
}
}
}
[/code][/QUOTE]
i ll just run it n reply back.. i typed a pointer rong so need to rectify n then i ll reply...
•
•
•
•
Originally Posted by comwizz
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.
C++ Syntax (Toggle Plain Text)
//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; } } }
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();
}
*Voted best profile in the world*
•
•
Join Date: Feb 2006
Posts: 54
Reputation:
Solved Threads: 2
•
•
•
•
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?
•
•
•
•
Originally Posted by HackWizz
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...
*Voted best profile in the world*
![]() |
Other Threads in the C++ Forum
- Previous Thread: file size
- Next Thread: KEy board event trapping and manipulation
| Thread Tools | Search this Thread |
api array based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets







,
. 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..