|
shaveyourhead wrote:
n fact, I've got .net training next week and with any luck will soon leave 4.2 behind.
Cool. Well if after that you still do C++, I can't emphasize enough how worthwhile it would be to replace this kind of code with std::string (or std::wstring if you need wchar_t support). It's just much less hassle and looks cleaner too.
¡El diablo está en mis pantalones! ¡Mire, mire!
Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)!
SELECT * FROM User WHERE Clue > 0
0 rows returned
Save an Orange - Use the VCF!
|
|
|
|
|
I got it figured out.
null terminated string was the cause
needed to set mystring[x] = 0;
and voila! no more smiley faces on the screen.
Just a big smiley face on me.
|
|
|
|
|
I really need some help with this one. I am new to the computer world, I am a college student and I am currently taking a course that covers object oriented programming. I needed help in trying to figure out how to start it. If anyone reads this please help me. Thank you for taking the time to read this. Below I have provided the criteria for doing the assignment and see if anyone may know how to do it, because I DON'T!!!.
Objectives:
You are to write a program that simulates the operation of a calculator. The calculator will perform the simple arithmetic operation of add, subtract, multiply, and divide. The user will be allowed to invoke methods to get data from the user (i.e., the operands), perform a given type of calculation, and to display the result.
Specific Instructions:
1. Define the class -- Calculator
a) Data members: Operand1, Operand2, and Answer -- all of type float.
b) Methods: Constructor, GetData, Add, Subtract, Multiply, Divide, and ShowAnswer -- all void return types.
c) Data members should be private, but all methods should be public
2. Constructor -- Calculator
a) The constructor should provide for two arguments of type float: Op1, and Op2
b) Give default values of zero to Op1, and 1 to Op2 in case the user passes no arguments when objects are created.
c) The constructor should assign the values of Op1 and Op2 to Operand1 and Operand2, respectively.
3. Each method that does a calculation should store the answer in Answer but not do any output itself.
4. The ShowAnswer method is responsible for displaying the calculated result of the last calculation.
5. Create four objects, Problem1, Problem2, Problem3, and Problem4 as follows:
Calculator Problem1(3,7), Problem2(8,3), Problem3, Problem4(17,5);
6. Do the following method calls: (YOU MUST FOLLOW THESE INSTRUCTIONS!)
a) For Problem1, call the method that will add the default values, and call the method to display the result.
b) For Problem2, call the method that will subtract the default values, and call the method to display the result.
c) For Problem3, call the method to get data, provide values of 7 and 3, multiply them, and display results.
d) For Problem4, call the method that will divide the first operand by the second, and display the results.
Jonathan
|
|
|
|
|
You're getting a 1 vote because this isn't the place to post homework assignments. Grow up, grab yourself buy the balls, get some hair on your chest, and make a good faith effort to do the work *first*, then come back and ask *specific* questions, *after* you have made the effort to try and learn/solve the problem. Otherwise sod off.
Cheers and have a nice day!
Ooh and remember, chocolate milk is good for you, Reading is actually helpful, and Google is your friend.
¡El diablo está en mis pantalones! ¡Mire, mire!
Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)!
SELECT * FROM User WHERE Clue > 0
0 rows returned
Save an Orange - Use the VCF!
|
|
|
|
|
Mr. Crafton, I was not trying to pass of my homework on anybody, I was simply asking for someone to help me with a starting point. The reason that I am taking this course because it was one of the few that was left, I had to register late for classes due to a outstanding balance, that until recently I could even pay for. If I knew what to do even after reading the chapter, which I did, I wouldn't have asked for help. I tried to start it and it was completely wrong, the assignment is one that I have yet to even do in my previous computer classes. Therefore, I would appreciate that you not post something like that under my message, when I am trying to find someone to help me, if you have no suggestions please don't bother posting.
And you too have a nice day.
Jonathan
|
|
|
|
|
Don't get so uptight, Jonathon. Folks post homework questions here at least once per week. Sometimes they are well received, other times they are not. Several factors are involved.
Like most, I do not mind helping, but I need a show of good faith that you are actually doing the work and only asking for help when you are stuck. It helps no one for a homework assignment to be posted verbatim and ask others to do it for you.
Are others in your class having like difficulties? Does the instructor favor going to him/her for "getting starting" questions such as yours?
A much better approach is to take your assigmnent, break it up into smaller pieces and ask for help with those individually, perhaps even rewording the text so that it longer resembles homework. This is much better than trying to get help for the assignment as a whole, which will either get you ignored or flamed.
DHAPPREP wrote:
I tried to start it and it was completely wrong...
Show us what you have and we can critique it.
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
You got flamed, that's normal because no one likes to answer yet another homework question.
I will though, give you some hints to start of...
1. Define the class -- Calculator -> see your book, if you can't do this, don't even start.
a) Data members: Operand1, Operand2, and Answer -- all of type float. -> create three variables of type float, that should be do-able
b) Methods: Constructor, GetData, Add, Subtract, Multiply, Divide, and ShowAnswer -- all void return types. -> actually, C++ consists of functions, but anyway, a function does something with your variables. The constructor normally initializes your variables, I suspect GetData will analyze the input. (take in input, put the first number in operand1, the second in operand two and decide if you have to add/substract/... [EDIT]Reading further I see this is not the case, I have no idea what it should do[/EDIT]
c) Data members should be private, but all methods should be public -> who-ever tells you all functions should be public is a moron. Only make public those things that you want other classes to see. Constructor, GetData, Add, Subtract, Multiply, Divide, and ShowAnswer are good to make public in this case.
2. Constructor -- Calculator
a) The constructor should provide for two arguments of type float: Op1, and Op2
public Calculater(float Op1 = 0, float Op2 = 0){ ... }
b) Give default values of zero to Op1, and 1 to Op2 in case the user passes no arguments when objects are created. -> see a)
c) The constructor should assign the values of Op1 and Op2 to Operand1 and Operand2, respectively.-> I hope you can do this
3. Each method that does a calculation should store the answer in Answer but not do any output itself. Answer = Operand1 +,-,* or / Operand2
4. The ShowAnswer method is responsible for displaying the calculated result of the last calculation. -> that would be something like cout >> Answer= >> endl; // make it nicer then this
5. Create four objects, Problem1, Problem2, Problem3, and Problem4 as follows:
Calculator Problem1(3,7), Problem2(8,3), Problem3, Problem4(17,5);
->this will create your Object
6. Do the following method calls: (YOU MUST FOLLOW THESE INSTRUCTIONS!)
a) For Problem1, call the method that will add the default values, and call the method to display the result. Problem1.Add(); Problem1.ShowAnswer();
b) For Problem2, call the method that will subtract the default values, and call the method to display the result. -> cf a)
c) For Problem3, call the method to get data, provide values of 7 and 3, multiply them, and display results.-> cf a)
d) For Problem4, call the method that will divide the first operand by the second, and display the results.-> cf a)
You're extremely lucky I'm bored for the moment .
So: define your class, with all the functions, then call the class from your main.
If you succed, you can always try to input the numbers yourself, making an interactive calculator.
good luck.
"If I don't see you in this world, I'll see you in the next one... and don't be late." ~ Jimi Hendrix
|
|
|
|
|
V. wrote:
actually, C++ consists of functions...
True, but when they belong to a class, they are called methods.
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
DHAPPREP wrote:
1. Define the class -- Calculator
a) Data members: Operand1, Operand2, and Answer -- all of type float.
b) Methods: Constructor, GetData, Add, Subtract, Multiply, Divide, and ShowAnswer -- all void return types.
c) Data members should be private, but all methods should be public
Here is a starting point:
class Calculator
{
public:
Calculator( float Op1 = 0.0, float Op2 = 1.0 );
void GetData();
void Add();
void Subtract();
void Multiply();
void Divide();
void ShowAnswer( void );
private:
float Operand1;
float Operand2;
float Answer;
}; DHAPPREP wrote:
5. Create four objects, Problem1, Problem2, Problem3, and Problem4 as follows:
Calculator Problem1(3,7), Problem2(8,3), Problem3, Problem4(17,5);
Since your class works with floating-point numbers, I think it would make sense to also test with floating-point numbers rather than just integers.
Calculator Problem1(17.2, 9.07),
Problem2(0.8, 3);
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
Hint :
Calculator::Add()
{
Answer = Operand1 + Operand2;
}
It doesn't get much simpler!
|
|
|
|
|
Hi, I wonder if anyone could could help me with a Win32 coding problem. I was trying to program a notepad clone that supported encryption. The problem is that I have an EDIT control, initially set up using WS_HSCROLL and ES_AUTOHSCROLL as part of the windows style. This allows the text to scroll without word wrapping. In order to toggle word wrap on and off, I simply use SetWindowLong to set the window style with the WS_HSCROLL and ES_AUTOHSCROLL bits set to diable word wrapping, and with them unset in order to word wrap. This nearly works. Swapping between the two modes gets rid the horizontal scroll bar, as it does in notepad, but the text does not change. If I start with the word wrap set, then the text wraps as it should. After changing modes however, the Hscroll bar appears, but the text continues to wrap. Likewise, if I start with the text wrap off then the text scrolls when typed, but switching modes means that the scrollbar disappears but the text does not start wrapping.
Using MessageSpy to see what notepad does I saw that it destroyed the old Edit and created a new one with the appropriate windows styles set. Is there no way to change the wrap mode without destroying the edit? I wrote my own word wrap function and set it using the EM_SETWORDBREAKPROC message, but it is never called.
If anyone knows of any simpler way to switch in and out of word wrap I would be grateful to hear of it.
Many thanks, Jan
|
|
|
|
|
Have you tried calling
DWORD flags = SWP_NOACTIVATE | SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER;
SetWindowPos( editHwnd, NULL, 0, 0, 0, 0, flags );
I know that the only way to get certain window style settings to take effect is to call SetWindowPos() like this. Hope this helps!
¡El diablo está en mis pantalones! ¡Mire, mire!
Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)!
SELECT * FROM User WHERE Clue > 0
0 rows returned
Save an Orange - Use the VCF!
|
|
|
|
|
Hi, and thanks for the reply. I didn't mention in my parent post, but I DID already have that code at the bottom of my function. I needed it to display the HSCROLL appearing or disappearing, but it does not help with the problem of word wrapping.
|
|
|
|
|
I copied my project from a Windows 2000 PC to a Windows XP PC, but now I get a load of compile erros and I a completely at a loss to why
Visual Studio 7.1 is installed on both PCs.
c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\PlatformSDK\Include\WinBase.h(1553): error C2059: syntax error : ')'<br />
c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\PlatformSDK\Include\WinBase.h(1560): error C2059: syntax error : ')'<br />
c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\PlatformSDK\Include\WinBase.h(1633): error C2059: syntax error : ')'<br />
c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\PlatformSDK\Include\WinBase.h(1640): error C2059: syntax error : ')'<br />
c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\PlatformSDK\Include\WinBase.h(1647): error C2059: syntax error : ')'<br />
c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\PlatformSDK\Include\WinBase.h(1655): error C2059: syntax error : ')'<br />
c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\PlatformSDK\Include\WinBase.h(1671): error C2059: syntax error : ')'<br />
c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\PlatformSDK\Include\WinNT.h(340): error C2143: syntax error : missing ';' before '*'<br />
c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\PlatformSDK\Include\WinDef.h(204): error C2143: syntax error : missing ';' before '*'<br />
c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\PlatformSDK\Include\WinDef.h(205): error C2143: syntax error : missing ';' before '*'<br />
c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\PlatformSDK\Include\WinBase.h(1620): error C2143: syntax error : missing ';' before '__stdcall'<br />
c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\PlatformSDK\Include\WinBase.h(1628): error C2143: syntax error : missing ';' before '__stdcall'
Does anyone know the cause of these errors, they have been no code changes.
grahamfff
|
|
|
|
|
The problem is plainly in the Windows SDK, perhaps you had a different SDK installed on one machine ? Either way, have you looked at the lines being complained about ? How about the project settings, are they unchanged ? The code that's blowing up shouldn't be, but it's not your code, either way. Having said that, the C++ compiler can report some weird errors, relative to what the real problem is...
Christian
I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
|
|
|
|
|
I had this happen at work. It "appeared" that the header was screwed up. In the end it got written up to a bad hard drive, which eventually got replaced, and later on I got a new system out of it! 3 cheers for MS!
¡El diablo está en mis pantalones! ¡Mire, mire!
Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)!
SELECT * FROM User WHERE Clue > 0
0 rows returned
Save an Orange - Use the VCF!
|
|
|
|
|
Grahamfff wrote:
CodeProjectForum problems or suggestions?
I copied my project from a Windows 2000 PC to a Windows XP PC, but now I get a load of compile erros and I a completely at a loss to why
Visual Studio 7.1 is installed on both PCs.
c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\PlatformSDK\Include\WinBase.h(1553): error C2059: syntax error : ')'
...
c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\PlatformSDK\Include\WinBase.h(1560):
c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\PlatformSDK\Include\WinBase.h(1628): error C2143: syntax error : missing ';' before '__stdcall'
Hello,
I got the same scenario also when I moved from Win2K to WinXP, and the problem was with old projects that did not define the corresponding preprocessor macros that are generated on a new stdafx.h file, for example: (I'm not sure which one of the #defines fixed my problem)
#ifndef VC_EXTRALEAN
#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers
#endif
#ifndef WINVER // Allow use of features specific to Windows 95 and Windows NT 4 or later.
#define WINVER 0x0400 // Change this to the appropriate value to target Windows 98 and Windows 2000 or later.
#endif
#ifndef _WIN32_WINNT // Allow use of features specific to Windows NT 4 or later.
#define _WIN32_WINNT 0x0500 // Change this to the appropriate value to target Windows 98 and Windows 2000 or later.
#endif
#ifndef _WIN32_WINDOWS // Allow use of features specific to Windows 98 or later.
#define _WIN32_WINDOWS 0x0410 // Change this to the appropriate value to target Windows Me or later.
#endif
#ifndef _WIN32_IE // Allow use of features specific to IE 4.0 or later.
#define _WIN32_IE 0x0400 // Change this to the appropriate value to target IE 5.0 or later.
#endif
#define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS // some CString constructors will be explicit
#define _AFX_ALL_WARNINGS
Look on Using the Windows Headers[^] in the MSDN docs.
Hope this solves your problem.
-- Ricky Marek (AKA: rbid)
-- "Things are only impossible until they are not" --- Jean-Luc Picard
My articles
|
|
|
|
|
The last thing I did on my Windows 2000 PC was to clean the project.
I have tried the above but still no luck.
This is mad, projects should be movable between PC's and Windows versions.
Completely
grahamfff
|
|
|
|
|
I am having trouble converting double to char. The number can be 15 digits in length with
maximum of 5 decimal precision or minimum 0. Tried using sprintf/ltoa but it gives some
wierd results.
sprintf when used in this format sprintf(buffer,"%.5f",dNum); works fine if there are 10
digits to the left of the decimal. For digits >10 to the left of decimal i.e 11,12,13 the results are incorrect.
Code Snippet:
char buffer[20];
CString csTest;
dNum = 99999999999.999;
sprintf(buffer,"%.5f",dNum);
csTest.Format(_T("%.5f"),dNum);
Test cases with 11 digits to the left
INPUT (dNUM) OUTPUT
99999999999.99999 9999999999.99999
99999999999.9999 99999999999.99989
99999999999.999 99999999999.99899
99999999999.99 99999999999.99001
99999999999.9 99999999999.89999
I tried using the CStrings' Format function but I get the same result as that function also inherently uses sprintf.
|
|
|
|
|
|
That sounds so zen.
¡El diablo está en mis pantalones! ¡Mire, mire!
Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)!
SELECT * FROM User WHERE Clue > 0
0 rows returned
Save an Orange - Use the VCF!
|
|
|
|
|
I take it that you actually want to pad the the number with trailing 0's.
Here's 1 method:
// test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <conio.h>
int _tmain(int argc, _TCHAR* argv[])
{
char buffer[64];
char Format[32];
double dNum = 99999999999.0;
int MaxPrecision = 5;
int Precision;
double Divisor = 10.0;
for(Precision = 0; Precision <= 5; Precision++)
{
sprintf(Format, "%%.%uf", Precision);
int CharCount = sprintf(buffer, Format, dNum);
if(CharCount >= 0)
{
int i;
if(!strchr(buffer, '.'))
{
buffer[CharCount] = '.';
CharCount++;
}
for(i = 0; i < MaxPrecision - Precision; i++)
buffer[CharCount + i] = '0';
buffer[CharCount + i] = 0;
}
printf("%s\n", buffer);
dNum = dNum + (9 / Divisor);
Divisor *= 10;
}
getch();
return 0;
}
|
|
|
|
|
Sorry, I misinterpreted your question in my previous reply.
The short answer is that you have exceeded the floating point (or is it printf?) library precision by using 11 + 5 digits (excluding the decimal point).
This example may help you understand what is going on:
// test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <conio.h> // for getch()
int _tmain(int argc, _TCHAR* argv[])
{
char buffer[64];
double dNum;
int MaxPrecision = 8;
int Precision;
double Divisor;
long MaxDecimal;
for(double dblNum = 8888; dblNum <= 88888888888; dblNum = dblNum * 10 + 8)
{
for(int CurPrecision = 1; CurPrecision <= MaxPrecision; CurPrecision++)
{
dNum = dblNum;
Divisor = 10.0;
fprintf(stdout, "\nCurrent Precision: %u\n\n", CurPrecision);
for(Precision = 0, MaxDecimal = 0; Precision < CurPrecision; Precision++)
MaxDecimal = MaxDecimal * 10 + 9;
for(Precision = 0; Precision <= CurPrecision; Precision++)
{
int CharCount;
CharCount = sprintf(buffer, "%.*f", CurPrecision, dNum);
fprintf(stdout, "%s\t", buffer);
CharCount = sprintf(buffer, "%.*f", Precision, dNum);
if(CharCount >= 0)
{
int i;
if(!strchr(buffer, '.'))
{
buffer[CharCount] = '.';
CharCount++;
}
for(i = 0; i < CurPrecision - Precision; i++)
buffer[CharCount + i] = '0';
buffer[CharCount + i] = 0;
}
fprintf(stdout, "%s\n", buffer);
dNum = dNum + (9 / Divisor);
Divisor *= 10;
}
}
}
getch();
return 0;
}
If you run the example, you will notice that things become decidedly 'iffy' when you reach the 16 char output limit. The reason the second sprintf statement works better is that it is forcing rounding of the decimal part (so it must be printf that is the culprit), but since your problem states that the maximum number of digits is 15, you should have no problem with these limits.
|
|
|
|
|
Try something like this. I use code very similar to this all the time. NOTE I have not tested the negative double (the insertion of the - sign may need to be adjusted.
void convertMyDoubleToString(double myDouble,int precision,CString& result) const{
char* buffer;
int dec;
int sign;
size_t i;
buffer = _fcvt(myDouble,precision,&dec,&sign);
for (i=strlen(buffer)-1;i>(size_t)sign;i--){
if (i == dec){
buffer[i+1] = buffer[i];
buffer[i] = '.';
break;
}else{
buffer[i+1] = buffer[i];
}
}
result = buffer;
if (myDouble >= 0 && myDouble < 1){
result = CString("0.") + result.Left(result.GetLength()-1);
}else if (myDouble < 0 && res > 1){
result = CString("-0.") + result; //MAY NEED TO ADJUST THIS
}else if (myDouble <= 1){
result = CString("-") + result; //MAY NEED TO ADJUST THIS
}
}
|
|
|
|
|
Try
sprintf(buffer,"%.5lf",dNum);
(remark the l between 5 and f)
SkyWalker
|
|
|
|
|