|
-
June 24th, 2011, 07:22 PM
#1
why is it not working as i expecting.
see a class that has a string here is a function for that class that remove space.
Code:
void clc::remove_spcae()
{
cout<<"string inputted by user is "<<eqn<<endl;
int i(0);
do
{
if(eqn[i]==' ')
{
for(int j=i+1;eqn[j]!='\0';j++)
{
if(eqn[j]==' ')
cout<<"";
else
{
if(eqn[j]=='\0')
{
eqn[i]=eqn[j];
break;
}
eqn[i]=eqn[j];
eqn[j]=' ';
this->eatspcae();
}
}
}
i++;
}
while(eqn[i]!='\0');
cout<<"newer string is "<<this->eqn<<endl;
}
some special i see in this function is that it calls itself even after printing last line that is
Code:
cout<<"newer string is "<<this->eqn<<endl;
it should call irself from inside function not if it came outside do while loop.
if it not do so then it will work correctly.
as well as it hangs just before end.
after running it twice my CPU usage is goes to70%(and not going down) which is usually 5 % to 15% are these things linked to each other.
thnk's to all answerer.
-
June 24th, 2011, 07:57 PM
#2
Re: why is it not working as i expecting.
 Originally Posted by vkash
see a class that has a string here is a function for that class that remove space.
Please post a complete program. No one knows what eon is declared as.
Regards,
Paul McKenzie
-
June 25th, 2011, 01:08 AM
#3
Re: why is it not working as i expecting.
 Originally Posted by Paul McKenzie
Please post a complete program. No one knows what eon is declared as.
Regards,
Paul McKenzie
I have changed this part to run perfectly. after all i have that question(which i ask last post) still remain.
when i code i always write a lot thing in// and /* */ so there will a lot stuff lines
Since i have edited much part of this code from morning but you can still answer this.
Put this code in your editor and then see it because here you will find that i have tried to hide large part of program by /*unwanted code*/
calculator.h file code
Code:
#pragma once
#include<iostream>
#include<cstring>
class clc
{
private:
char* eqn;
public:
//constructor
clc(char* input); //complete working
//destructor
~clc(); //complete working
//copy constructor
clc(const clc&); //complete working
//chage string
void change(char array[]);
//cheack for errors and compability of string with program
//void cheack(); //work ias in progress
//information about object and others
void info(); //complete
//eaatspace function to remove spaces
void eatspcae(); //complete working
//priority of operators.(seeming tough function)
// char* priority();
//do all things(seeming tough function)
// double do_it_all();
// optimizer
void optimize();
//get equation
char* get()
{
return eqn;
}
};
calculator.cpp file very large part of this is incomplete so only see that part which is
not inside /* */ or you can put it in your C++ editor their unwanted lines will look differently.
Code:
#include "calculator.h"
#include<iostream>
#include<cstring>
using namespace std;
clc::clc(char* input="vikash")
{
cout<<"constructor";
try
{
size_t v=strlen(input)+1;
eqn=new char[v];
strcpy_s(eqn,(strlen(input)+1),input);
//=temp;
cout<<strlen(eqn)<<endl;
}
catch(bad_alloc &ex)
{
char*temp=new char[21];
cout<<"ERROR"<<endl;
cout<<"reason => "<<ex.what()<<endl;
cout<<"You have to enter string once againmanually"<<endl;
cout<<"input array you want to input(max 20)\t";
cin.getline(eqn,20);
cout<<endl;
}
cout<<eqn<<endl;
cout<<"constructor end"<<endl;
}
clc::~clc()
{
cout<<"destructor----------------------------------------\n";
delete []eqn;
}
void clc::change(char array[])
{
strcpy_s(this->eqn,strlen(array)+1,array);
}
clc::clc(const clc & copier)
{
cout<<"copy constructor/////////////////\n"<<endl;
int string_length(0);
string_length=strlen(copier.eqn);
try
{
delete eqn;
char* temp=new char[string_length];
}
catch(bad_alloc &ex)
{
cout<<"ERROR in copy constructor"<<endl;
cout<<"reason "<<ex.what()<<endl;
cout<<"manually insert length of string\t";
cin>>string_length;
cout<<endl;
}
strcpy_s(this->eqn,string_length+1,copier.eqn);
}
/*void clc::cheack() //edit this as required
{
try //cactch block work according to code thrown by every part.
{
cout<<"Inspecting string... \n";
//first cheack for braces.
cout<<"brace cheack(0)...\n";
unsigned int left_brace(0);
unsigned int right_brace(0);
for(int i=0;eqn[i]!='\0';i++)
{
if( eqn[i]=='(' )
left_brace++;
if( eqn[i]==')' )
right_brace++;
}
if( right_brace==left_brace)
cout<<"passed in brace cheack"<<endl;
else
throw(0); //here is first throw
//second cheack for operator misuse
cout<<"operator cheack"<<endl;
for(int i=0;eqn[i]!='\0';i++)
{
if( (eqn[i]=='+')||(eqn[i]=='-')||(eqn[i]=='*')||(eqn[i]=='/')||eqn[i]=='.')
if( (eqn[i+1]=='+')||(eqn[i+1]=='-')||(eqn[i+1]=='*')||(eqn[i+1]=='/')||(eqn[i+1]=='.')|| (eqn[i+1]=='\0'))
throw(1); //here is second throw
if(eqn[i+1]=='\0')
break;
}
//third cheack for numericals
for(int i=0;eqn[i]!='\0';i++)
{
if(
*/
//}
/*void clc::info()
{
system("cls");
cout<<"information about object"<<endl;
cout<<"equation is "<<this->eqn<<endl;
cout<<"number of characters is "<<strlen(this->eqn)<<endl;
int i(0);
for(int j=0;j<strlen(this->eqn);j++)
{
if((eqn[j]=='+')||(eqn[j]=='-')||(eqn[j]=='/')||(eqn[j]=='*'))
i++;
}
cout<<"number of operator "<<i<<endl;
}*/
void clc::eatspcae()
{
cout<<"string inputted by user is "<<eqn<<endl;
int i(0);
do
{
if(eqn[i]==' ')
{
for(int j=(i+1);eqn[j]!='\0';j++)
{
if(eqn[j]==' ')
cout<<"";
else
if(eqn[j]!=' ')
{
if(eqn[j]=='\0')
break;
eqn[i]=eqn[j];
eqn[j]=' ';
break;
}
}
}
i++;
}
while(eqn[i]!='\0');
cout<<"newer string is "<<this->eqn<<"\t"<<strlen(eqn)<<endl;
//c.change(tmp1);
cout<<"optimize string size(y)";
char imp='y';
//cin>>imp;
if((imp=='y')||(imp=='Y'))
this->optimize();
else
cout<<"not optimizing";
system("pause");
system("cls");
//c.eatspcae();
}
void clc::optimize()
{
cout<<"welcome string optimizer\n";
cout<<"cheacking if it has space to be optimized..."<<endl;
unsigned int i(0);
for(i=0;i<(strlen(eqn)+1);i++)
{
if(eqn[i]==' ')
break;
}
cout<<"it can be optimized "<<i<<endl;
char*temp;
try{
temp=new char[i+1];}
catch(bad_alloc &ex)
{ cout<<ex.what()<<endl;
system("pause");}
//strcpy_s(temp,(i+1),eqn);
for(unsigned int j=0;j<=i;j++)
{
temp[j]=eqn[j];
}
cout<<"copy complete\n";
temp[i+1]='\0';
cout<<"orignal string"<<eqn <<"\tstring length"<<strlen(this->eqn)<<endl;
cout<<"final string "<<temp<<"\tstring length"<<strlen(temp)<<endl;
//system("pause");
cout<<endl;
delete []eqn;
eqn=temp;
cout<<eqn<<"\t"<<strlen(eqn);
cout<<"here\n";
cout<<eqn<<endl<<strlen(eqn);
cout<<" hang";
}
/*
char* clc::priority()
{
int tmp_task_list[20][5];
//see brackets
for(;;)
{
int task_number(0);
int sub_task_number(0);
int i(0);
if(eqn[i]==')')
{
int j(i)
for(;j>0;j--)
{
if( j=='(' )
break;
}
tmp_task_list[task_number][sub_task_number]=j;
tmp_task_list[
*/
main.cpp file
Code:
//Calculator program.
//version 1
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#include"calculator.h"
#include"function.h"
#include<iostream>
#include<cstring>
#include<cctype>
using namespace std;
int f(clc X)
{
X.info();
cout<<"function"<<endl;
return 0;
}
int main()
{
{
clc c("1 + 2- 3 /4* 5 / 6 + 7 - 8 *9");
//cout<<"Enter the thing to be calculated"<<endl;
//cin.getline(tmp1,49);
cout<<"main******************************************\n";
c.eatspcae();
cout<<"just came to main\n";
f(c);
cout<<"here is data "<<c.get()<<endl;
cout<<"it's length "<<strlen(c.get())<<endl;
}
_CrtDumpMemoryLeaks();
return 0;
}
that is all . there are also two files named as function.h and function.cpp but they are empty so no need to put them here.
this code has a bug a serious bug that is in optimize function regarding heap which did not allow it to run till end i mean it hangs before end but everything is looking fine in optimize function.
If you have any advice to complete it then tell me.
specially for function priority and do_it_all. priority functionn check whole string and then send it to do_it_all where do_it_all function ad subtract and other things according to data send by priority.
Do you think using class in this program is useless? I think so but now i can't change it .
-
June 25th, 2011, 10:18 AM
#4
Re: why is it not working as i expecting.
This doesn't make any sense, and I mentioned this to you in another thread:
Code:
try
{
size_t v=strlen(input)+1;
eqn=new char[v];
strcpy_s(eqn,(strlen(input)+1),input);
//=temp;
cout<<strlen(eqn)<<endl;
}
catch(bad_alloc &ex)
{
char*temp=new char[21];
Why are you attempting to allocate more memory in the catch block if the reason for the catch is that new failed in the try block? Secondly, as I stated to you in the other thread, the only thing you should do with a std::bad_alloc is to throw it back to the caller. Attempting to allocate more memory or calling functions that can potentially allocate memory internally should not be done.
Another issue:
Code:
try
{
delete eqn;
}
Wrong form of delete. It should be "delete[]", not "delete".
More issues:
Code:
//copy constructor
clc(const clc&); //complete working
//...
clc::clc(const clc & copier)
{
cout<<"copy constructor/////////////////\n"<<endl;
int string_length(0);
string_length=strlen(copier.eqn);
try
{
delete eqn; // uninitialized pointer -- undefined behavior
You are deleting a pointer, eqn, that is uninitialized. I already mentioned this to you previously. So don't assume something is "complete working" until you have others look at it.
Exactly what are you trying to do with "remove_space"? What is its purpose? Whatever it is, it can't be as complicated as you've made it with what you've written.
Last, if you're going to post code, remove the commented code from your posts.. You have code where half of it is commented out. Remove the commented out code completely before you post.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; June 25th, 2011 at 10:36 AM.
-
June 25th, 2011, 12:38 PM
#5
Re: why is it not working as i expecting.
 Originally Posted by Paul McKenzie
[code]
More issues:
Code:
//copy constructor
clc(const clc&); //complete working
//...
clc::clc(const clc & copier)
{
cout<<"copy constructor/////////////////\n"<<endl;
int string_length(0);
string_length=strlen(copier.eqn);
try
{
delete eqn; // uninitialized pointer -- undefined behavior
You are deleting a pointer, eqn, that is uninitialized. I already mentioned this to you previously. So don't assume something is "complete working" until you have others look at it.
Due to commented code I think you forget to see somethings
Code:
clc::clc(const clc & copier)
{
cout<<"copy constructor/////////////////\n"<<endl;
int string_length(0);
string_length=strlen(copier.eqn);
try
{
//delete eqn; //this is also a error
char* temp=new char[string_length];
}
catch(bad_alloc &ex)
{
cout<<"ERROR in copy constructor"<<endl;
cout<<"reason "<<ex.what()<<endl;
cout<<"manually insert length of string\t";
cin>>string_length;
cout<<endl;
}
strcpy_s(this->eqn,string_length+1,copier.eqn); //is it legal eqn is not assigned
}
this is copy constructor there is also a error in last line. I have not assigned memory for that so i think this will create error. Am i correct Paul.
 Originally Posted by Paul McKenzie
Exactly what are you trying to do with "remove_space"? What is its purpose? Whatever it is, it can't be as complicated as you've made it with what you've written.
newer eatspace or remove space is working but error in first is still mystery(why that remove_space(in newer that is renamed to eatspace) function called again after going out) to which need to be resolved
 Originally Posted by Paul McKenzie
Last, if you're going to post code, remove the commented code from your posts.. You have code where half of it is commented out. Remove the commented out code completely
I will do this thing next time.
I have not make any big change since last post. But i found that it hangs every time (even after removing those bugs you mentioned)
so where it hangs
Code:
int main()
{
{
clc c("1 + 2- 3 /4* 5 / 6 + 7 - 8 *9");
//cout<<"Enter the thing to be calculated"<<endl;
//cin.getline(tmp1,49);
cout<<"main******************************************\n";
c.eatspcae();
cout<<"just came to main\n";
f(c);
cout<<"here is data "<<c.get()<<endl;
cout<<"it's length "<<strlen(c.get())<<endl;
} //as it moves out of this scope everything before this is working fine
//but as this } came it hangs
_CrtDumpMemoryLeaks();
return 0;
}
as last destructor called all just !!!!!!!!!!!!!!! crashed. I mean as last destructor called all crashed. what's wrong there.
this is somehow related to deletion because if i did not put delete []eqn; in destructor it works fine but memory leak of 10 byte data occurs.
code without comment is here
main.cpp
Code:
//Calculator program.
//version 1
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#include"calculator.h"
#include"function.h"
#include<iostream>
#include<cstring>
#include<cctype>
using namespace std;
int main()
{
clc c("1 2 3 4 5 6 7 8 9");
cout<<"main******************************************\n";
c.eatspcae();
cout<<"just came to main\n";
f(c);
cout<<"here is data "<<c.get()<<endl;
cout<<"it's length "<<strlen(c.get())<<endl;
_CrtDumpMemoryLeaks();
return 0;
}
calculator.h
Code:
#pragma once
#include<iostream>
#include<cstring>
class clc
{
private:
char* eqn;
public:
clc(char* input);
~clc();
clc(const clc&);
void change(char array[]);
void info();
void eatspcae();
void optimize();
char* get()
{
return eqn;
}
};
calculator.cpp
Code:
//minor comments
#include "calculator.h"
#include<iostream>
#include<cstring>
using namespace std;
clc::clc(char* input="vikash")
{
cout<<"constructor";
try
{
size_t v=strlen(input)+1;
eqn=new char[v];
strcpy_s(eqn,(strlen(input)+1),input);
//=temp;
cout<<strlen(eqn)<<endl;
}
catch(bad_alloc &ex)
{
cout<<"range of equation\n";
int ip;
cin>>ip
char*temp=new char[ip];
cout<<"ERROR"<<endl;
cout<<"reason => "<<ex.what()<<endl;
cout<<"You have to enter string once againmanually"<<endl;
cout<<"input array you want to input(max 20)\t";
cin.getline(eqn,i);
cout<<endl;
}
cout<<eqn<<endl;
cout<<"constructor end"<<endl;
}
clc::~clc()
{
cout<<"destructor----------------------------------------\n";
delete []eqn;
}
void clc::change(char array[])
{
strcpy_s(this->eqn,strlen(array)+1,array);
}
clc::clc(const clc & copier)
{
cout<<"copy constructor/////////////////\n"<<endl;
int string_length(0);
string_length=strlen(copier.eqn);
char* temp;
try
{
//delete eqn;
temp=new char[string_length];
}
catch(bad_alloc &ex)
{
cout<<"ERROR in copy constructor"<<endl;
cout<<"reason "<<ex.what()<<endl;
cout<<"manually insert length of string\t";
cin>>string_length;
cout<<endl;
}
eqn=temp;//is it correct
}
void clc::info()
{
system("cls");
cout<<"information about object"<<endl;
cout<<"equation is "<<this->eqn<<endl;
cout<<"number of characters is "<<strlen(this->eqn)<<endl;
int i(0);
for(int j=0;j<strlen(this->eqn);j++)
{
if((eqn[j]=='+')||(eqn[j]=='-')||(eqn[j]=='/')||(eqn[j]=='*'))
i++;
}
cout<<"number of operator "<<i<<endl;
}
void clc::eatspcae()
{
cout<<"string inputted by user is "<<eqn<<endl;
int i(0);
do
{
if(eqn[i]==' ')
{
for(int j=(i+1);eqn[j]!='\0';j++)
{
if(eqn[j]==' ')
cout<<"";
else
if(eqn[j]!=' ')
{
if(eqn[j]=='\0')
break;
eqn[i]=eqn[j];
eqn[j]=' ';
break;
}
}
}
i++;
}
while(eqn[i]!='\0');
cout<<"newer string is "<<this->eqn<<"\t"<<strlen(eqn)<<endl;
//c.change(tmp1);
cout<<"optimize string size(y)";
char imp='y';
//cin>>imp;
if((imp=='y')||(imp=='Y'))
this->optimize();
else
cout<<"not optimizing";
system("pause");
system("cls");
//c.eatspcae();
}
void clc::optimize()
{
cout<<"welcome string optimizer\n";
cout<<"cheacking if it has space to be optimized..."<<endl;
unsigned int i(0);
for(i=0;i<(strlen(eqn)+1);i++)
{
if(eqn[i]==' ')
break;
}
cout<<"it can be optimized "<<i<<endl;
char*temp;
try{
temp=new char[i+1];}
catch(bad_alloc &ex)
{ cout<<ex.what()<<endl;
system("pause");}
//strcpy_s(temp,(i+1),eqn);
for(unsigned int j=0;j<=i;j++)
{
temp[j]=eqn[j];
}
cout<<"copy complete\n";
temp[i+1]='\0';
cout<<"orignal string"<<eqn <<"\tstring length"<<strlen(this->eqn)<<endl;
cout<<"final string "<<temp<<"\tstring length"<<strlen(temp)<<endl;
//system("pause");
cout<<endl;
delete []eqn;
eqn=temp;
cout<<eqn<<"\t"<<strlen(eqn);
}
function.h and function.cpp
Code:
//function.h
#pragma once
#include"calculator.h"
int f(clc X);
//function.cpp
#include"calculator.h"
#include<iostream>
#include<cstring>
using namespace std;
int f(clc X)
{
//X.info();
cout<<"start\n";
cout<<"function"<<endl;
cout<<"end\n";
return 0;
}
that's all non comment code.
Last edited by vkash; June 25th, 2011 at 12:50 PM.
Reason: remove some writing errors
-
June 25th, 2011, 02:56 PM
#6
Re: why is it not working as i expecting.
Your copy constructor is still incorrect:
Code:
try
{
char* temp=new char[string_length]; // this is a memory leak,
}
catch(bad_alloc &ex)
{
cout<<"ERROR in copy constructor"<<endl;
cout<<"reason "<<ex.what()<<endl;
cout<<"manually insert length of string\t";
cin>>string_length;
cout<<endl;
}
strcpy_s(this->eqn,string_length+1,copier.eqn);
There are so many mistakes with the code above.
1) The temp is a local pointer that will go out of scope once the try block has been exited. That means that the memory you allocated can never be retrieved, and therefore you have a memory leak.
2) The amount of memory you allocated is not enough. You forgot to add 1 for the terminating NULL character.
3) The catch block in the copy constructor does not do a throw, so the user now has a bogus copy created just hanging out there in their code. Get out of the habit of using cout's as a way to warn the user that the object couldn't be created. With your code, the object is created -- you did not stop the creation of the object. The only thing that stops the creation of the object on construction is to throw an exception.
As a matter of fact, the copy constructor, since you want to code it that way, must throw an exception for a program to still behave correctly. The reason is that the user or the compiler may make copies of the object, and you don't know how, when, or where those copies can be made. The copies may be made due to passing or returning by value, placing these objects in a container such as a vector, or by other means. By having bogus objects being used by a program is guaranteed to make the program behave incorrectly.
4) The strcpy_s is attempting to copy data into an uninitialized variable. This goes back to item 1). Your "temp" pointer no longer exists and you can't use it.
Last, you still haven't explained what remove_space is supposed to do. Remove what space? Where?
Pretend you were documenting your class to others who will use it, and someone asks you "what does remove_space" do? You would say "it removes all spaces from the beginning of the string" or "it removes consecutive spaces" or something similar to that. The person doesn't care how you do it -- he/she wants to know what it does, and so far, you never explained it on this level.
Regards,
Paul McKenzie
-
June 25th, 2011, 05:48 PM
#7
Re: why is it not working as i expecting.
I've said a bunch of times before too, don't put cout statements in your functions. That's a terrible habit. Let the caller of the function decide how to handle the exception.
-
June 25th, 2011, 11:21 PM
#8
Re: why is it not working as i expecting.
 Originally Posted by GCDEF
I've said a bunch of times before too, don't put cout statements in your functions. That's a terrible habit. Let the caller of the function decide how to handle the exception.
I do this to know that what's happening in the code. You are also correct(as you may a developer) since user only needs output so why to tell him/her what's happening . when i complete this program and found all errors/bugs are removed at that time i will remove unwanted cout's in functions.
my final target in this program is a integration program .for an integrating(maths) program i need something that can calculate the that 's why i make this calculator.
Last edited by vkash; June 25th, 2011 at 11:45 PM.
-
June 25th, 2011, 11:40 PM
#9
Re: why is it not working as i expecting.
 Originally Posted by Paul McKenzie
Your copy constructor is still incorrect:
Code:
try
{
char* temp=new char[string_length]; // this is a memory leak,
}
catch(bad_alloc &ex)
{
cout<<"ERROR in copy constructor"<<endl;
cout<<"reason "<<ex.what()<<endl;
cout<<"manually insert length of string\t";
cin>>string_length;
cout<<endl;
}
strcpy_s(this->eqn,string_length+1,copier.eqn);
There are so many mistakes with the code above.
1) The temp is a local pointer that will go out of scope once the try block has been exited. That means that the memory you allocated can never be retrieved, and therefore you have a memory leak.
2) The amount of memory you allocated is not enough. You forgot to add 1 for the terminating NULL character.
I have changed these things in newer copy constructor do you not see that one the copy constructor about which you are talking is old one.
 Originally Posted by Paul McKenzie
4) The strcpy_s is attempting to copy data into an uninitialized variable. This goes back to item 1). Your "temp" pointer no longer exists and you can't use it.
I also try to fix this thing.
How i try to remove spaces from string this is y paradigm for this.
search for spaces ,if space found.
enter in a new loop which checks that is another place (after place where first space found) is space if yes then loop completes and this loop make a round again till any character found. if character/integer/special character etc found then put this to place just after the place you find last thing(not space). now call eatspace(remove_space) once again it has got newer string in which we get a space less than the original string. now all this happen till '\0' found by do while loop if it find this it prints a line giving yo newer string. now here starts my question why after this cout giving us the final string it terminates, it should exit from function but it is recalled. for this non working eatspace see first post of this thread in last post where i pt newer eatspace that works fine
in newer code in my last post everything is fine except the the end of the program destructor is doing something wrong or memory allocation is wrong due to which deletion creating problem. I don't understand what's happening because if i not delete eqn in destructor then memory leak reported if i make delete []eqn then it crashes.
Paul McKenzie thanks for taking interest in my post.
Last edited by vkash; June 25th, 2011 at 11:50 PM.
-
June 25th, 2011, 11:45 PM
#10
Re: why is it not working as i expecting.
 Originally Posted by vkash
How i try to remove spaces from string this is y paradigm for this.
I have no idea what this means, and the rest of your explanation is too long.
Very simply, what is that function supposed to perform? Don't tell me about loops or variables, just state very simply, what is that function supposed to do? I'll make it simple for you:
Code:
void clc::remove_spcae();
That's all I see -- now explain, what does the function do? Remember, users of a class don't care about the internals of what is done, all they care about is the job it's supposed to do, and so far, I have no idea what job it's supposed to do. The only thing you've stated is "remove space", but which spaces? At the beginning of the string? At the end of the string? Duplicate spaces? What exactly?
Regards,
Paul McKenzie
Last edited by Paul McKenzie; June 26th, 2011 at 12:00 AM.
-
June 26th, 2011, 04:49 AM
#11
Re: why is it not working as i expecting.
 Originally Posted by Paul McKenzie
I have no idea what this means, and the rest of your explanation is too long.
Very simply, what is that function supposed to perform? Don't tell me about loops or variables, just state very simply, what is that function supposed to do? I'll make it simple for you:
Code:
void clc::remove_spcae();
That's all I see -- now explain, what does the function do? Remember, users of a class don't care about the internals of what is done, all they care about is the job it's supposed to do, and so far, I have no idea what job it's supposed to do. The only thing you've stated is "remove space", but which spaces? At the beginning of the string? At the end of the string? Duplicate spaces? What exactly?
Regards,
Paul McKenzie
It is supposed to remove saces from eqn.
NOTE (1+222-*dfsdf85 \0) there are SIX spaces between 5 and \ . It will seem you single but these are six spaces mind it before you read this post.
if i write
clc C("1 + 222 - *df sdf85");
constructor called and this eqn is set to this value(1 + 222 - *df sdf85\0).(ther is no space between 5 and\) now the work of eatspace(remove_sace) is to make
it like this 1+222-*dfsdf85 \0(spaces still remain there but all are at last. ). It has to erase all the space between characters, integers etc it should not take any action to \0 and length of eqn . Now i think you understand what i supposed to do. My last explanation was really stuff i also think so because when i read that even I don't understand what i write there.
after eatspace function optimize function called which change this(1+222-*dfsdf85 \0) to 1+222-*dfsdf85\0 (now spaces are removed now string is taking minimum memory as it can) It's work is to optimize memory. But in last optimize function i put here there was a bug that it do unwanted work if there is no room for optimization. Now I have solved this bug.
Last edited by vkash; June 26th, 2011 at 06:19 AM.
-
June 26th, 2011, 07:44 AM
#12
Re: why is it not working as i expecting.
 Originally Posted by vkash
I do this to know that what's happening in the code. You are also correct(as you may a developer) since user only needs output so why to tell him/her what's happening . when i complete this program and found all errors/bugs are removed at that time i will remove unwanted cout's in functions.
my final target in this program is a integration program .for an integrating(maths) program i need something that can calculate the that 's why i make this calculator.
That's not the point. The point is whatever function that calls the function that throws the error needs to handle it graciously. The user won't know what to do with those messages, but your code will need to know an exception has occurred and handle it in whatever way is appropriate.
-
June 26th, 2011, 09:42 AM
#13
Re: why is it not working as i expecting.
if i write
clc C("1 + 222 - *df sdf85");
constructor called and this eqn is set to this value( 1 + 222 - *df sdf85\0).(ther is no space between 5 and\) now the work of eatspace(remove_sace) is to make
it like this 1+222-*dfsdf85 \0(spaces still remain there but all are at last. ). It has to erase all the space between characters, integers etc it should not take any action to \0 and length of eqn .
So basically, all you're doing is moving the space characters to the end of the string. That's all you needed to state. You didn't have to talk about loops, variables, or anything else.
Please look at the code below:
Code:
#include <algorithm>
#include <cctype>
#include <cstring>
// this is a standalone function or static function of your class.
class clc
{
//...
static bool IsValidChar(int ch)
{ return !isspace(ch); }
void remove_spcae();
//...
};
//...
void clc::remove_spcae()
{
std::stable_partition(eqn, eqn + strlen(eqn), IsValidChar);
}
That code does everything your loop does. Try to understand what each part does and how it does it. The results should be exactly the same as what you're trying to achieve.
If you want a one-line version:
Code:
#include <algorithm>
#include <functional>
#include <cctype>
//...
std::stable_partition(eqn, eqn + strlen(eqn), std::not1(std::ptr_fun<int, int>(std::isspace)));
This should do the same thing, and you don't need to write an isValidChar function.
I have faith that at some point your loop will work, since you seem to know what you're doing (but still need to debug the code). However, if you have an application you're trying to develop, there is no need to continue trying to do an academic exercise of writing loops and debugging. You have algorithms such as above that do all of this work for you, bug free.
So the question is do you have an app to develop and want to do it fast and bug-free, or are you going to spend time trying to get a loop to work (and again, you seem to know the basics of writing a for loop and understanding things, but you're spending an awful lot of time debugging code).
Regards,
Paul McKenzie
Last edited by Paul McKenzie; June 26th, 2011 at 09:56 AM.
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
|