|
Ayman Mashal wrote: after freeing a pointer using free() is it required to set it to NULL ?
it will good programming practice if you set it NULL, it may save you many trouble
|
|
|
|
|
Hey guys,
I guess I need to brush up on my pointers a bit. How do I keep the value of a spot in a pointer from changing when the object it was set with changes? my code looks something like this:
<br />
Node someNode;<br />
Node * nodeArray = new Node[40];<br />
<br />
.<br />
.<br />
.<br />
<br />
for(int i = 0; i < 40; i++)<br />
{<br />
<br />
nodeArray[i] = someNode;<br />
}<br />
I'm looking for a way to keep my array values from all ending up as the final value of someNode.
I know this has something to do with dereferencers and stuff here and there, but it's been quite a while. I checked google and a few other places, but no luck.
Thanks,
Sean
|
|
|
|
|
Write a copy constructor in Node that makes a deep copy. That way, each assignment in
nodeArray[i] = someNode; will make a separate copy of the data.
|
|
|
|
|
That's a really handy little trick . Thanks for the help (I don't think I would have ever remembered that one).
|
|
|
|
|
Hi, I am a total beginner - first time using this forum.
I need to write a fraction calculator program that can do +,-,*,and /. The output format required is like this:
> 1/2 + 3/4
> 5/4
>2 1/2 - 3/4
>1 3/4
Essentially, I need to ask the user to input a valid expression, and be able to output the result on the screen. Notice that I have to be able to do mixed fractions. This is a huge challenge for me. I don't know how to extract key data from user's inputs.
This is an example given in class - but my professor said that we can't have the program menu driven. It means I can't ask the user numerator and denominator separately, for example. A good method/strategy to write this program is appreciated.
Note: something with me posting header files - so the ones really needed: stdlib.h, iostream, math.h, conio.h, string
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <math.h>
#include <conio.h>
#include <time.h>
#include <string>
using namespace std;
class Fraction
{
public:
Fraction()
{
num = 0;
denom = 1;
}
Fraction(int a, int b) : num(a), denom(b)
{
int div = gcd(num, denom);
num /= div;
denom /= div;
}
Fraction(const Fraction &x)
{
num = x.num;
denom = x.denom;
}
Fraction operator =(const Fraction &x)
{
num = x.num;
denom = x.denom;
return Fraction(num,denom);
}
friend ostream& operator <<(ostream& out, const Fraction &x);
friend istream& operator >>(istream& in, Fraction &x);
friend Fraction operator +(const Fraction &x, const Fraction &y);
friend Fraction operator -(const Fraction &x, const Fraction &y);
friend Fraction operator *(const Fraction &x, const Fraction &y);
friend Fraction operator /(const Fraction &x, const Fraction &y);
private:
int num, denom;
int gcd(int p, int q)
{
return (q==0)? p : gcd(q, p%q);
}
};
Fraction operator +(const Fraction &x, const Fraction &y)
{
int num, denom;
num = x.num*y.denom + y.num*x.denom;
denom = x.denom * y.denom;
return Fraction(num,denom);
}
Fraction operator *(const Fraction &x, const Fraction &y)
{
int num, denom;
num = x.num*y.num;
denom = x.denom * y.denom;
return Fraction(num,denom);
}
Fraction operator -(const Fraction &x, const Fraction &y)
{
int num, denom;
num = x.num*y.denom - y.num*x.denom;
denom = x.denom * y.denom;
return Fraction(num,denom);
}
Fraction operator /(const Fraction &x, const Fraction &y)
{
int num, denom;
num = x.num*y.denom;
denom = x.denom * y.num;
return Fraction(num,denom);
}
ostream& operator <<(ostream& out, const Fraction &x)
{
out<<x.num<<' '<<x.denom;
="" return="" out;
}
istream&="" operator="">>(istream& in, Fraction &x)
{
char junk;
in>>x.num>>junk>>x.denom;
return in;
}
char menu()
{
cout<<"Please make a selection: "<<endl
<<"="" a="" input="" the="" fraction="" a"<<endl
="" b="" b"<<endl
="" +="" add="" and="" -="" subtract="" from="" divide="" by="" *="" multiply="" c="" print="" answer="" c"<<endl
="" 1="" move="" to="" 2="" q="" quit"<<endl
="" <<"your="" selection:="" ";
="" char="" ans;
="" cin="">>ans;
cin.ignore(1000,'\n');
return ans;
}
void operation(char choice, Fraction &a, Fraction &b, Fraction &c)
{
switch(toupper(choice))
{
case 'A' : cout<<"Please enter the fraction A: ";
cin>>a;
break;
case 'B' : cout<<"Please enter the fraction B: ";
cin>>b;
break;
case 'C' : cout<<"The fraction C is: "<
|
|
|
|
|
Are you familiar with the C++ String find and substr functions?
If you are I would use them, if not, I would get familiar and use them
To parse a statement I would have something like this...
String inputExpression;
cin >> inputExpression; // User enters 5 1/4 + 3 3/4 - for this example
int firstStop inputExpression.find_first_of(" /");
if (firstStop == String::npos) cerr << "UH OH!!!"; // This means the user didn't input right
if (inputExpression.at(firstStop) == ' ') //if this is true it must be a mixed number
//use substr to extract the whole number
if (inputExpression.at(firstStop) == '/') //This means it was just a fraction
//use substr to extract the numerator
----
Then you can use find_first_of("+-*/", previousBreak); to find the position of the operator in the String and evaluate that. Good luck, hope this helps.
--- Edit
I realized I didn't explain this well above. The find_first_of(char*) function searches the string for the first occurance of any character passed as the argument. That's why find_first_of(" /") can be used to find either the first space or the first slash.
In the use for finding the operator (you would also use this if the expression was a mixed number) find_first_of(char*, int) the char* again is the list of characters to search for, and the int is the starting position to begin the search. This way it doesn't always find the same thing.
|
|
|
|
|
Here's a stumper for you gurus out there. Have any of you used the file association or drag and drop to drop a file on you application to automatically start your app and open the file? Then you are aware of the CWinApp member, m_lpCmdLine. This member lpstring contains the full path of the file you dropped on the application or double clicked to open. Here's a puzzler for you! Have you ever tried to open the data file using similar code?
CFile File;
File.Open(m_lpCmdLine)
Then you probably are aware the File.Open will fail on a bad path.
However, if you create a CString variable sPath and set it equal exactly to the path m_lpCmdLine contains...presto...it opens.
Does anyone know why?
-- modified at 13:45 Sunday 18th March, 2007
Someone from the CodeGuru board has suggested the following...
The reason for this behavior is the operating system places quotes around the file name so that it can be interpreted as a single command line parameter in case there are spaces in the path.
I haven't investigated exactly where the quotes get stripped off in MFC, but it's probably in CCmdLineInfo or CDocTemplate.
So, in other words, the two strings are not identical. If you want to use CFile to open the command line directly you will have to remove the quotes first.
...this has merit for investigating and I will post the results once I test this.
Here's a snippit of the code.
void CApplicationApp::OnFileNew()<br />
{<br />
UINT nStrLen = 0;<br />
UINT nStrCount = 0;<br />
char lpszCmdStr[_MAX_PATH];<br />
CString strCWD;<br />
CString strFilename;<br />
CFile File;<br />
CError Error;<br />
CMiscUtil Util;<br />
<br />
if (m_lpCmdLine[0] != '\0')<br />
{<br />
strCWD = strFilename = m_lpCmdLine;<br />
}<br />
else<br />
{<br />
strcpy(lpszCmdStr, m_pszHelpFilePath);<br />
nStrLen = strlen(lpszCmdStr);<br />
while (lpszCmdStr[nStrLen] != '\\')<br />
{<br />
lpszCmdStr[nStrLen] = 0;<br />
--nStrLen;<br />
}<br />
g_strProgramPath = lpszCmdStr;<br />
strFilename = g_strProgramPath + "Project" + ".fil";<br />
}<br />
<br />
MessageBox(0, strFilename, "Debug Message #1", MB_OK);<br />
<br />
if (File.Open(strFilename, CFile::modeRead))<br />
{<br />
File.Close();<br />
theApp.OpenDocumentFile(strFilename);<br />
}<br />
else if (File.Open(strFilename, CFile::modeCreate | CFile::modeWrite))<br />
{<br />
File.Write(&Project, sizeof(class CProjectData));<br />
File.Close();<br />
strFilename = g_strProgramPath + "\\Project" + ".idx";<br />
if (File.Open(strFilename, CFile::modeCreate | CFile::modeWrite))<br />
{<br />
File.Close();<br />
strFilename = g_strProgramPath + "\\Project" + ".fdb";<br />
if (File.Open(strFilename, CFile::modeCreate | CFile::modeWrite))<br />
{<br />
File.Close();<br />
}<br />
theApp.OpenDocumentFile(strFilename);<br />
}<br />
}<br />
else<br />
{<br />
Error.Message(47);<br />
}<br />
}
|
|
|
|
|
The answer I got from the CodeGurus board was right on the money.
It was right on the money with the answer. Short and SWEET. Developed a routine to strip the quotes and now I have drag/drop or file association in my application. Then I started looking at my message boxes again. Oops, it slipped right past me!
Message box before StripQuotes:
Debug Message #1
"c:\Documents and Settings\Users\My Documents\My Data\project.fil"
after StripQuotes:
Debug Message #1
c:\Documents and Settings\Users\My Documents\My Data\project.fil
The problem was staring me in the face and I didn't even catch it. I am so used to seeing strings with quotes at each end the display didn't even strike me as odd! Good reason to resort to Gurus. LOL at myself! How much time did I spend on this...
-- modified at 23:56 Monday 19th March, 2007
|
|
|
|
|
I need to get the text under cursor when user click on another applications such as MS Word, Internet Explore,Notepad...or the text under Icon,or the text in menubar,toolbar,in Explore. Any think that I can use GetWindowText(hWnd,..) method or WM_GETTEXT to get the text, but it can't solve every case,I can't get the handle of Icon, or any item in toolbar,menubar or a page in MS Word so I can't use GetWindowText method,WM_GETTEXT too.Furthermore they only return the title of window that have handle we pass.This's not things we expect.
Does someone have a piece of code or in-depth instructions on how to
deal with the problem?Thanks in advance!
ngoo duwcs
chvt_smpt
|
|
|
|
|
Why do you want to do this?
Peter.
|
|
|
|
|
I want to do a Dictionary project, support user search by mouse Click on Text. So i have to get text at point that user click but i haven't just found way to solve this problem ....
-- modified at 23:18 Saturday 17th March, 2007
chvt_smpt
|
|
|
|
|
ngooduwcs wrote: I want to do a Dictionary project, support user search by mouse Click on Text.
So why do you need "text under Icon,or the text in menubar,toolbar,in Explore?" Are you planning on spell-checking those?
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
ngooduwcs wrote: Does someone have a piece of code or in-depth instructions on how to
deal with the problem?
IAccessible::get_accValue() should get you some of what you want, but I do not know of a one-size-fits-all solution.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
|
i'm using the Menu template in my work.
i want to add a new menu selection that will display a new menu which will have some selections.
how can i add it in the file: menu.cpp, and menu.h
tooti
|
|
|
|
|
Hi all,
I am not sure of the following code.
vector<socket *="">::iterator it;
for(it = vecGuiInSockets.begin(); it != vecGuiInSockets.end(); ++it)
{
(*it)->cleanup_socket();
delete (*it);
it--;
}
vecGuiInSockets.clear();
First of all, what does the clear() clean?
Does it just clean the pointers to Socket,
or it cleans the object to which the pointer points?
Second, is the for() loop correctly done?
Thanks!
Yonggoo
|
|
|
|
|
since you didn't use < and > rather than < and > respectedly, i cannot know what your vactor contains. so if my answer is not accurate, please fix your question.
the std::vector<>::clear() function clears the vector, but nowhere calls the objects destructors.
if your vector contains objects (like std::vector<MyType> ), then you manage the life of the objects. calling clear() will only remove the reference (actually, the copy) of the obect in the vector.
if your vector contains pointers (like std::vector<MyType*> ), you have to be more than careful because memory leak are close to occur; especially if you allocated dynamically using new . Using such vectors, you must first iterate over the vector to delete all the contained pointers, and only then call clear() .
ps: please also use <pre></pre> tags when posting code sample so that you keep being formated correctly
|
|
|
|
|
toxcct wrote: so if my answer is not accurate, please fix your question.
Best answer I've heard all day!
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
DavidCrow wrote: Best answer I've heard all day!
if my answer was that good, then some idiot didn't understood it, because it seems that i've been rated badly (once again)
|
|
|
|
|
I doubt the code will work. It doesn't help that we can see < or > characters in your post! The it-- is crazy and will cause all types of problems.
Steve
|
|
|
|
|
Hi all,
I am using VC6. Is there any easy way to make the font used in the dialog bold (MS Sans Serif)? Especially for all the CStatic's?
I know I can simply use one of the CStatic-derived classes here, but that means I have to declare/initialize/set each member of the CStatic for all my dialogs! This means hundreds!
Thanks for any input.
|
|
|
|
|
In the dialog properties you can set the font for the dialog. That font will then be used by all the controls on the dialog.
You may be right I may be crazy -- Billy Joel --
Within you lies the power for good, use it!!!
|
|
|
|
|
But as I mentioned in my original post, you can't make it BOLD from the properties, can you?
|
|
|
|
|
You did not mention that you tried it from the properties
In VC8 it is possible to it from the properties, but not in VC6. I even tried modifying the dialog resource directly in the rc file by changing the FONT property. I was able to toggle the italic property but not change the weight.
Another easy option is to use EnumChildWindows to enumerate through all your controls and send a WM_SETFONT message to each one.
You may be right I may be crazy -- Billy Joel --
Within you lies the power for good, use it!!!
|
|
|
|
|
thanks a lot. just what i was looking for.
|
|
|
|
|