-
May 25th, 2011, 04:50 AM
#1
what is bug in this code
see it there is a bug in returning from function because pointer value in function and main is different
#include<iostream>
#include<cstring>
using namespace std;
int* space(char[]);
char* answer(char[]);
int main(void)
{
char arraym[100];
cout<<"Input string(max 100 character and max 10spaces):\n";
cin.getline(arraym,100,'\n');
int *pa=space(arraym);
for(int i=0;i<10;i++)
cout<<"space location"<<(pa[i])<<endl;
return 0;
}
int* space(char arrays[])
{
int p=0;
int ans[10]={0};
int* pans=ans;
cout<<arrays<<endl;
// int tmp=sizeof(arrays)/sizeof(arrays[0]);
for(unsigned int i=0;i<(strlen(arrays));i++)
{
cout<<"\n*****"<<" ";
if(arrays[i]==' ')
{
cout<<"SPACE FOUND AT"<<i<<"th LOCATION"<<endl;
pans[p]=i;
p++;
}
}
for(int i=0;i<10;i++)
{
cout<<"result in function body"<<pans[i]<<endl;
}
return pans;
}
in simple words my question is why value of pans and pa is different give any suggestion to make their value same.
I am making a program which returns the number of words seprately on every next call. It is half of that program .
What is problem in this program? when i compile and run value given by pans in function is different from the pa but i think these should same since function return pans which is transfered to pan.
Thanks for replying.
Last edited by vkash; May 25th, 2011 at 04:55 AM.
-
May 25th, 2011, 04:56 AM
#2
Re: what is bug in this code
After 38 posts you should know how to use code tags for posting code.
Code:
int ans[10]={0};
int* pans=ans;
.....
return pans;
'ans' is created on the stack of the function. This stack is deleted as soon as the function ends. So the address that 'pans' is pointing to doesn't exist anymore after the function is done.
-
May 25th, 2011, 04:56 AM
#3
Re: what is bug in this code
You ahs been already told two day ago:
Originally Posted by Paul McKenzie
After over 30 posts, you should already know how to use code tags.
That is not a code tag. You should have seen that the tags do not work when you preview your message.
Why do you ignore what other people tell you?
And how do you expect any further help after that?
Victor Nijegorodov
-
May 25th, 2011, 04:59 AM
#4
Re: what is bug in this code
Originally Posted by VictorN
You ahs been already told two day ago:Why do you ignore what other people tell you?
And how do you expect any further help after that?
OK now i will keep that in my mind there is no need to reply more to this thread
-
May 25th, 2011, 05:11 AM
#5
Re: what is bug in this code
problem solved see it
#include<iostream>
#include<cstring>
using namespace std;
int* space(char[]);
char* answer(char[]);
int main(void)
{
char arraym[100];
cout<<"Input string(max 100 character and max 10spaces):\n";
cin.getline(arraym,100,'\n');
int *pa=space(arraym);
for(int i=0;i<10;i++)
cout<<"space location"<<(pa[i])<<endl;
delete pa;
return 0;
}
int* space(char arrays[])
{
int p=0;
//int ans[10]={0};
int *ans=new int[10];
int* pans=(ans);
cout<<arrays<<endl;
// int tmp=sizeof(arrays)/sizeof(arrays[0]);
for(unsigned int i=0;i<(strlen(arrays));i++)
{
cout<<"\n*****"<<" ";
if(arrays[i]==' ')
{
cout<<"SPACE FOUND AT"<<i<<"TH LOCATION"<<endl;
pans[p]=i;
p++;
}
}
for(int i=0;i<10;i++)
{
cout<<"result in function body"<<pans[i]<<endl;
}
return pans;
}
special thanks to victorN and skizmo they help me to focus my problem any further suggestion will be veryyy helpfull.
still some value of pa are abnormal i will try to remove those with 0.
once again thanks.
-
May 25th, 2011, 05:16 AM
#6
Re: what is bug in this code
And how about this part of the discussion:
Originally Posted by VictorN
You have been already told two day ago:
Originally Posted by Paul McKenzie
After over 30 posts, you should already know how to use code tags.
That is not a code tag. You should have seen that the tags do not work when you preview your message.
Why do you ignore what other people tell you?
And how do you expect any further help after that?
Originally Posted by vkash
OK now i will keep that in my mind there is no need to reply more to this thread
Victor Nijegorodov
-
May 25th, 2011, 05:19 AM
#7
Re: what is bug in this code
Originally Posted by vkash
problem solved see it
First, use code tags when posting code. I believe I already told you about that.
Second, your program uses wrong form of delete. I mentioned to that to you also.
Third, your program will fail miserably if p > 9:
Code:
int *ans=new int[10];
int* pans= ans;
//...
for(unsigned int i=0;i<(strlen(arrays));i++)
{
cout<<"\n*****"<<" ";
if(arrays[i]==' ')
{
cout<<"SPACE FOUND AT"<<i<<"TH LOCATION"<<endl;
pans[p]=i; // error if p >= 9
p++;
}
}
So your problem is not "solved". Instead of this error-prone form of programming, why not learn how to use std::vector and std::string?
Edit: To show you that your program is still no good, what if the arrays had more than 10 space characters?
Regards,
Paul McKenzie
Last edited by Paul McKenzie; May 25th, 2011 at 05:24 AM.
-
May 25th, 2011, 05:52 AM
#8
Re: what is bug in this code
Originally Posted by VictorN
And how about this part of the discussion:
are you telling this thing to do
Code:
#include<iostream>
#include<cstring>
using namespace std;
int* space(char[]);
char* answer(char[]);
int main(void)
{
char arraym[100];
cout<<"Input string(max 100 character and max 10spaces):\n";
cin.getline(arraym,100,'\n');
int *pa=space(arraym);
for(int i=0;i<10;i++)
cout<<"space location"<<(pa[i])<<endl;
delete []pa;
return 0;
}
int* space(char arrays[])
{
int p=0;
//int ans[100]={0};
int *ans=new int[10];
int* pans=(ans);
cout<<arrays<<endl;
// int tmp=sizeof(arrays)/sizeof(arrays[0]);
for(unsigned int i=0;i<(strlen(arrays));i++)
{
if(arrays[i]==' ')
{
cout<<"SPACE FOUND AT"<<i<<"TH LOCATION"<<endl;
pans[p]=i;
p++;
}
}
for(int i=0;i<10;i++)
{
cout<<"result in function body"<<pans[i]<<endl;
}
return pans;
}
if you are telling something different then explain that
i think i have also solved mentioned bug by Paul
Last edited by vkash; May 25th, 2011 at 05:58 AM.
-
May 25th, 2011, 06:05 AM
#9
Re: what is bug in this code
Originally Posted by vkash
are you telling this thing to do
Code:
#include<iostream>
#include<cstring>
using namespace std;
int* space(char[]);
char* answer(char[]);
int main(void)
{
...
}
Exactly!
Sure you see the difference between code snippets in your OP and in the last one! Don't you?
Victor Nijegorodov
-
May 25th, 2011, 11:23 AM
#10
Re: what is bug in this code
Originally Posted by vkash
i think i have also solved mentioned bug by Paul
I don't see where you fixed this bug. If someone were to enter a string with more than 10 spaces, your program continues to go on as if nothing is wrong.
Regards,
Paul McKenzie
-
May 25th, 2011, 08:36 PM
#11
Re: what is bug in this code
I don't see where you fixed this bug. If someone were to enter a string with more than 10 spaces, your program continues to go on as if nothing is wrong.
ups sorry i edit that i forget to upload edited code that is below
Code:
#include<iostream>
#include<cstring>
using namespace std;
int* space(char[]);
char* answer(char[]);
int main(void)
{
char arraym[100];
cout<<"Input string(max 100 character and max 10spaces):\n";
cin.getline(arraym,100,'\n');
int *pa=space(arraym);
for(int i=0;i<100;i++)
{
if(pa[i]>110)
break;
cout<<"space location"<<(pa[i])<<endl;
}
delete []pa;
return 0;
}
int* space(char arrays[])
{
int p=0;
int *ans=new int[100];
int* pans=(ans);
for(unsigned int i=0;i<(strlen(arrays));i++)
{
if(arrays[i]==' ')
{
cout<<"SPACE FOUND AT "<<i<<" th LOCATION"<<endl;
pans[p]=i;
p++;
}
}
return pans;
}
are all bugs removed?
I have tried it. It gives even more than 10 space count.
actually the full question was
5. (Advanced) Write a function that, when passed a string consisting of words separated by single
spaces, returns the fi rst word; calling it again with an argument of NULL returns the second word,
and so on, until the string has been processed completely, when NULL is returned. This is a
simplifi ed version of the way the native C++ run - time library routine strtok() works. So, when
passed the string ‘one two three ' , the function returns you ‘one' , then ’two' , and fi nally
’ three' . Passing it a new string results in the current string being discarded before the function
starts on the new string.
I try it for 8 hours i reach to the above code that was not full. Finally i download solution from wrox and that is
Code:
// Soln5_5.cpp
#include <iostream>
#include <cstring>
using std::cin;
using std::cout;
using std::endl;
char* parse(const char* str)
{
static char* pStr(nullptr);
static size_t len(0);
static size_t start(0);
size_t pos(0);
char* pReturn(nullptr);
// First time through, save the string
if (str)
{
delete[] pStr; // in case it was allocated
len = strlen(str);
pStr = new char[len+1];
strcpy_s(pStr, len+1, str);
}
if (start >= len)
return nullptr;
// Walk the string from 'start' till we find a blank or the end
for (pos = start; pStr[pos] != ' ' && pStr[pos] != '\0'; pos++);
// Copy the string if we've a word to return, otherwise return nullptr
if (pos != start)
{
pReturn = new char[pos - start + 2];
size_t i(0);
for (size_t j=start; j<pos; i++,j++)
pReturn[i] = pStr[j];
pReturn[i] = '\0';
start = pos+1;
return pReturn;
}
else
{
delete[] pStr; // We are done with the string
pStr = nullptr;
return nullptr;
}
}
int main()
{
char s1[] = "seventy-one fruit balls, please Doris";
cout << "string is '" << s1 << "'\n\nParsing...\n";
char* p = parse(s1);
while (p)
{
cout << p << endl;
delete[] p; // Delete the string that was returned
p = parse(nullptr);
}
return 0;
}
thats all story of this program.
Last edited by vkash; May 25th, 2011 at 08:39 PM.
-
May 25th, 2011, 08:59 PM
#12
Re: what is bug in this code
Originally Posted by vkash
I try it for 8 hours i reach to the above code that was not full. Finally i download solution from wrox and that is
So now go through your code and the code from Wrox press line by line and see what the differences are. Then modify your program to work correctly. If you do this, not only will your program work correctly but you'll understand why it works correctly.
-
May 25th, 2011, 10:19 PM
#13
Re: what is bug in this code
Why didn't you just change the loop?
Code:
int* space(char arrays[])
{
int p=0;
int *ans=new int[10];
int* pans = ans;
int len = strlen(arrays);
for(unsigned int i=0; i < len && p < 10;i++)
{
if(arrays[i]==' ')
{
cout<<"SPACE FOUND AT "<<i<<" th LOCATION"<<endl;
pans[p]=i;
p++;
}
}
return pans;
}
5. (Advanced) Write a function that, when passed a string consisting of words separated by single spaces, returns the first word; calling it again with an argument of NULL returns the second word,
and so on, until the string has been processed completely, when NULL is returned. This is a simplifi ed version of the way the native C++ run - time library routine strtok() works.
So, when passed the string ‘one two three ' , the function returns you ‘one' , then ’two' , and finally’ three' . Passing it a new string results in the current string being discarded before the function starts on the new string.
I guess that book's purpose is to have you reinventing wheels instead of showing how to use the standard library to write robust C++ programs.
Regards,
Paul McKenzie
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|