|
http://www.codeproject.com/internet/amhttputils.asp
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief and you
|
|
|
|
|
I have an assignment that requires me to code a petty cash program in C++. I did this assignment a while back when it was required to code it in python. Is there a simple way to convert a python code into C++. I managed to get the menu working. I am trying to get it to do the math needed to adjust the account.
Here is the python code that works:
<br />
class Account:<br />
def __init__(self, initial):<br />
self.balance = initial<br />
<br />
def deposit(self, amt):<br />
self.balance = self.balance + amt<br />
<br />
def withdraw(self, amt):<br />
self.balance = self.balance - amt<br />
if self.balance < 0:<br />
print 'Not enough here'<br />
else:<br />
return self.balance<br />
<br />
def getbalance(self):<br />
return self.balance<br />
<br />
<br />
from datetime import date<br />
now = date.today()<br />
a = Account(0)<br />
print 'Please select a number'<br />
print '1 Deposit'<br />
print '2 Withdraw'<br />
print '3 Inquiry'<br />
number = input('Please choose an option: ')<br />
while True:<br />
if number == 1:<br />
dep = input('Enter amount deposited: $')<br />
a.deposit(dep)<br />
print 'Deposit of $', dep, 'makes account balance $', a.getbalance(), 'as of', now<br />
elif number == 2:<br />
wit = input('Enter amount withdrawn: $')<br />
a.withdraw(wit)<br />
print 'Withdraw of $', wit, 'makes account balance $', a.getbalance(), 'as of', now<br />
elif number == 3:<br />
print 'Account balance is $', a.getbalance(), now<br />
<br />
else:<br />
break<br />
print<br />
number = int(raw_input('Do you want something else? or enter 4 to exit '))<br />
<br />
print 'Goodbye'<br />
this is what I have so far in C++:
<br />
#include <iostream><br />
#include <string><br />
#include <sstream><br />
using namespace std;<br />
<br />
void DisplayMenu();<br />
int GetSelection();<br />
<br />
int main()<br />
{<br />
int selection = 0;<br />
while (selection != 4) {<br />
DisplayMenu();<br />
selection = GetSelection();<br />
switch (selection){<br />
case 1:<br />
cout << "Deposit" << endl;<br />
break;<br />
case 2: <br />
cout << "Withdraw" << endl;<br />
break;<br />
case 3:<br />
cout << "Inquiry" << endl;<br />
break;<br />
case 4:<br />
cout << "Quitting" << endl;<br />
break;<br />
default:<br />
cout << "\aInvalid selection" << endl;<br />
break;<br />
}<br />
} <br />
return 0;<br />
}<br />
void DisplayMenu()<br />
{<br />
cout << " ---Menu---" << endl;<br />
cout << "1=Deposit" << endl;<br />
cout << "2=Withdraw" << endl;<br />
cout << "3=Inquiry" << endl;<br />
cout << "4=Quit" << endl;<br />
}<br />
int GetSelection()<br />
{<br />
int selection;<br />
cout << "Enter selection: ";<br />
cin >> selection;<br />
return selection;<br />
}
I want to make the class Account work in C++. I don't want someone to do the assignment, just give me a clue as to how to go about it. Thanks in advance for any help.
Elaine
|
|
|
|
|
kerthy_29 wrote: I want to make the class Account work in C++.
Ok, so what is an account (object)? What are its properties? What can you tell it (settor methods)? What can it tell you about itself (getter methods). Knowing this will go a long way towards designing the class.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
I understand some of that. This is what I have figured out so far, but I don't know if it would work when I add it to the code I already have done. Does this look right?
<br />
class Account {<br />
int AccountBalance;<br />
};<br />
class Deposit{<br />
int AccountBalance, amount;<br />
public:<br />
void set_values(int, int);<br />
int dep () {return (AccountBalance+amount);}<br />
}; <br />
class Withdraw{<br />
int AccountBalance, amount;<br />
public:<br />
void set_values(int, int);<br />
int wit () {return (AccountBalance-amount);} <br />
};
Thanks for the help.
Elaine
|
|
|
|
|
Account seems about right. It's a noun, so a class is appropriate. However, Deposit and Withdraw are verbs. This indicates that they should be operations rather than classes.
What do you think about this design: (I will let you fill in the code.. it's your assignment! )
class Account {
int balance;
public:
void Deposit(int amount)
{
}
void Withdraw(int amount)
{
}
void SetBalance(int balance)
{
}
int CurrentBalance()
{
}
}; I made the operations part of the Account class. In an enterprise class design, this should be designed a bit differently. But let's not shoot over the target - the aim is to learn the C++ language, right? I also added SetBalance and CurrentBalance so that client code (fancy name for code which uses objects of a class), can initialize the object and inspect the object.
I would like to know how you reasoned when you made your design. Don't see this is as criticism. If there's one thing I've learned during the years, is that if you try to explain your own design in human language, the design mistakes will be very easy (or at least easier) to spot. The golden rule is that if it doesn't make sense in a human language, it won't make sense in a programming language.
--
Verletzen zerfetzen zersetzen zerstören
Doch es darf nicht mir gehören
Ich muss zerstören
|
|
|
|
|
Thanks for pointing out some fixes. I got the original coding from the python coding that I had already done and knew that it worked.
Elaine
|
|
|
|
|
After inspection of your original python code, it would seem that you are already familiar with my proposed design.. Sorry about that.
--
Verletzen zerfetzen zersetzen zerstören
Doch es darf nicht mir gehören
Ich muss zerstören
|
|
|
|
|
I wouldnt be sorry about it Joergen - you put her back on the tracks with her understanding of what it could/should look like in c++ - when I looked at her suggestion I thought 'yuck', transliteration of Python or not
At least unlike other people's homework attempts, she's made an effort, which is rare enough these days, so good-on-ya
'g'
|
|
|
|
|
I have an existing DLL named as A1.dll ,which is a MFC dll .
I added a configuration that renames the output of the same as B1.dll
hence it generates b1.lib
In my application i used to link with A1.lib which I replaced with B1.lib and I copied B1.dll to the executable folder .
Now when i run it , i get an error saying that A1.dll is missing .
Where do i specify this dependency ?
Engineering is the effort !
|
|
|
|
|
|
act_x wrote: A1.lib which I replaced with B1.lib
Probably, you have not removed A1.lib, from linking options.
|
|
|
|
|
I want to create a software that will produce almost all types of BANNERS(static and animated).So, is Visual C++/MFC a better approach for this one or should I go with VB.net?........
|
|
|
|
|
|
|
If you have to ask a question like that, and word it like that, I would suggest going with whatever one has the easiest-to-use graphic library, to lessen the chance of breaking something. (I would guess that VB.Net would be the safest choice for you.)
Peace!
-=- James Please rate this message - let me know if I helped or not!<HR> If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! See DeleteFXPFiles
|
|
|
|
|
McDonalds makes a Fish Filet
led mike
|
|
|
|
|
|
I still like that, I have developed it into a statement... waiting for the next appropriate VB Lounge thread to unleash it.
led mike
|
|
|
|
|
Some days there's a ton of 'em in a row....and when reading through your replies I think
it's like your picking them up in a bus for their one way ride to McDs! LOL
|
|
|
|
|
I'm using VC++ 6.0 and I can't figure out how to find the location of the virtual store in Vista. Everything works okay until I call a shell command to open a file. Any thoughts or suggestions would be appreciated!
Will
|
|
|
|
|
What is the "virtual store"?
|
|
|
|
|
I believe it's a folder under the %userprofile%\AppData\Local\ folder.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
Can you CreateCompatibleDC/Bitmap from a printer DC?
I'm trying to write out what i print as a .BMP file
But all i get is a big black bitmap...
Any ideers?
...Steve
(_hdc below is the printer DC and has already been drawn on
fn is the .bmp filename and w,h are the size of the _hdc)
Oops - debugging output here:
fn=c:\test_00.bmp w=6400 h=4900
Canvas::SaveBmp bgn
got bitblt
got di bitmap
got bm
got bmi
got size=94080000
alloc'd n got bits
writin file
cleanup
Canvas::SaveBmp end
HDC mCnv;
if ((mCnv = ::CreateCompatibleDC (_hdc)) == NULL)
Die ("Canvas::SaveBmp CreateCompatibleDC");
HBITMAP mBmp, pBmp;
if ((mBmp = ::CreateCompatibleBitmap (_hdc, w, h)) == NULL)
Die ("Canvas::SaveBmp CreateCompatibleBitmap");
if ((pBmp = (HBITMAP) ::SelectObject (mCnv, mBmp)) == NULL)
Die ("Canvas::SaveBmp SelectObject");
if (::BitBlt (mCnv, 0, 0, w, h, _hdc, 0, 0, SRCCOPY) == FALSE)
Die ("Canvas::SaveBmp BitBlt died");
BITMAP bm;
::GetObject (mBmp, sizeof (bm), (LPVOID) & bm);
DBG(" got bm");
BITMAPINFOHEADER bmi;
MemSet (& bmi, 0, sizeof (bmi));
bmi.biSize = sizeof (bmi);
bmi.biWidth = w;
bmi.biHeight = h;
bmi.biPlanes = 1;
bmi.biBitCount = bm.bmBitsPixel;
bmi.biCompression = BI_RGB;
DBG(" got bmi");
if (! ::GetDIBits (mCnv, mBmp, 0, h, NULL, (BITMAPINFO *)(& bmi),
DIB_RGB_COLORS))
Die ("Canvas::SaveBmp GetDIBits died");
if (bmi.biSizeImage == 0)
Die ("Canvas::SaveBmp GetDIBits - no size");
sprintf(dbg," got size=%d", bmi.biSizeImage);DBG(dbg);
char *bits = new char [bmi.biSizeImage];
if (! ::GetDIBits (mCnv, mBmp, 0, h, bits, (BITMAPINFO *)(& bmi),
DIB_RGB_COLORS))
Die ("Canvas::SaveBmp GetDIBits(2) died");
DBG(" alloc'd n got bits");
BITMAPFILEHEADER bmf;
MemSet (& bmf, 0, sizeof (bmf));
bmf.bfType = ((WORD) 'B' | ('M' << 8));
bmf.bfSize = sizeof (bmf) + sizeof (bmi) + bmi.biSizeImage;
bmf.bfOffBits = sizeof (bmi);
File f;
if (f.Open (fn, "w")) {
DBG(" writin file");
f.Put (& bmf, sizeof (bmf));
f.Put (& bmi, sizeof (bmi));
f.Put (bits, bmi.biSizeImage);
f.Shut ();
}
DBG(" cleanup");
delete [] bits;
::SelectObject (mCnv, pBmp);
::DeleteObject ( mBmp);
::DeleteDC (mCnv);
DBG("Canvas::SaveBmp end");
|
|
|
|
|
Steve Hazel wrote: fn=c:\test_00.bmp w=6400 h=4900
That is quite a large bitmap. On my machine CreateCompatibleBitmap falls over and GetLastError() returns 0x00000008 - "Not enough storage is available to process this command" when I try to create large bitmaps.
You may be right I may be crazy -- Billy Joel --
Within you lies the power for good, use it!!!
|
|
|
|
|
Thanks for checkin' into it - I appreciate that
Yep. I noticed it was biiig, but this machine handles it ok
and I only need the app to work for -me- so I can get this bitmap for my docs.
I wonder how to get around this...
I wonder how the printer driver handles this big of a bitmap?
Keeps it compressed or somethin?
I'd really like to have a full resolution bitmap (or jpg or whatever) for
my docs for this app. (which eventually will just do a regular printpreview)
Anybody got any ideers?
Thanks
...Steve
|
|
|
|
|