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.
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.
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.
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.
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;
}
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.
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.
Bookmarks