|
how to get the number of digits after the decimal point
ex :
Input : 5.1234
Expected Output : 4
i wrote this code but there are wornings..
what is wrong here ??
double input;
int x=input; // so x=the integeral part of input
int y=input-x; // so y=the fractional part
while(y>0)
{count++;
input*=10;
x=input;
y=input-x;
}
plz help me !!
elmahdy
ahmed elmahdy
|
|
|
|
|
First, this really sounds like homework. Not that such such posts are prohibited, but you may get a quicker response just reading ahead in your text book.
Anyway, two hints:
1) int s won't hold the fractional bits of a number.
2) converting a number from one type to another, when the second type is not able to correctly represent all values possible in the first, will cause compiler warnings.
Shog9
I returned and saw under the sun,
that the race is not to the swift,
nor the battle to the strong...
|
|
|
|
|
Given that 5.1234 is not going to represented exactly as such in memory (i.e., it's more than likely going to be something like 5.123999), you're going to have a hard time using your current code, assuming it even works.
elmahdy wrote:
int y=input-x; // so y=the fractional part
The problem with this is that 5.1234 - 5 = 0.1234 in memory, but only the 0 gets assigned to variable y . Make sense?
You could try converting the number to a string, but most of the conversion routines want to know how many digits to put after the decimal, which sort of defeats the purpose!
|
|
|
|
|
Where is the value of input coming from?
If you are getting it from the console, then use scanf with a string type specifier.
Get the length of that string
Start at the 0th char
Increment the index until you find the '.'
The number of decimal places is the string length - the current index (assuming you pre-increment)
|
|
|
|
|
Here is one solution, but it is fraught with problems, I will let you work out what they are!
#include <math.h><br />
#include <stdio.h><br />
<br />
int main(int argc, char* argv[])<br />
{<br />
<br />
double dVal = 5.1234,<br />
dResult = 0.9,<br />
dInt = 0.0;;<br />
<br />
int nKount = 0;<br />
<br />
while(dResult > 0.00001)<br />
{<br />
dVal *= 10.0;<br />
<br />
dResult = modf(dVal, &dInt);<br />
<br />
nKount++;<br />
}<br />
<br />
printf("Value = %d\n", nKount);<br />
return 0;<br />
}
It is amazing what you can accomplish if you do not care who gets the credit. - Harry S Truman
|
|
|
|
|
What is the c++ version of this in c#?
string[][] a two diminsion string array.
I just want std c++ if possible.
Thank You
Bo Hunter
|
|
|
|
|
|
The brackets are (unfortunately) tied to the variable name instead of the type. So your example is the correct way to do it in C++.
--
Frivolous Theorem of Arithmetic: Almost all natural numbers are very, very, very large.
|
|
|
|
|
In Java, brackets can go either with variable or type. Crazy...
|
|
|
|
|
Yes its really bothering me.... I have a CTreeCtrl on a modeless dialog, When a selection is made on the list control that is on the dialog box I want another class, generic, to parse out the contents to the related file... how can I do this? I understand that I can't pass a CTreeCtrl as a return value from the generic class (CParseDocument) and I can't get the generic class to get control of the Dialog class and make it update the control.
Also I'm using another class for this to organize it and it gets really messy if I keep the parsing in the Dialog class.
Heres what I've been trying to get to work (it keeps returning NULL):
TV_INSERTSTRUCT tv_is;
tv_is.hParent = TVI_ROOT;
tv_is.hInsertAfter = TVI_LAST ;
tv_is.item.mask = TVIF_TEXT;
tv_is.item.pszText = ConvertCStringToChar(m_strSectionName);
hSectionRoot = m_cSlidOpen.m_ctrlXMLTree.InsertItem(&tv_is);
if (hSectionRoot==NULL)
{
AfxMessageBox("failed");
return;
}
-Steven Hicks
CPACodeProjectAddict
|
|
|
|
|
|
<Hello,
How can i handle interrupts in win2k environment?
Thanks in advance...
|
|
|
|
|
I'm pretty sure that you need to run in ring 0 which means you write a device driver or similar kernel-mode code.
Brad
|
|
|
|
|
|
Hello,
I would like to create an empty folder on the disk, which command should I used in Visual C++? Thank you for your help!
Nachi
|
|
|
|
|
I'd start with CreateDirectory() .
|
|
|
|
|
Hi,
I have a custom listbox set up with several columns (file name, file type, size and path), and I want it so that whenever a user clicks on a column header, the items are sorted, just as in Windows Explorer, for instance.
The column click part is easy, as I just have this code underneath case WM_NOTIFY:
<br />
case IDC_LIST1:<br />
if(((LPNMHDR)lParam)->code == LVN_COLUMNCLICK)<br />
{<br />
if(((LPNMLISTVIEW)lParam)->iSubItem == 0)<br />
{<br />
}<br />
}<br />
However, I have no idea about how to use LVM_SORTITEMS. Obviously, I need to send it using SendDlgItemMessage and use a callback function as it says over on MSDN, but I don't know how to set up the callback function.
The problem is that all of the examples I have found are MFC based, but I am _not_ using MFC, just the straight Windows API.
Does anybody know where I can find a decent "sort listview items" code example that does not use MFC, or could anybody give me some pointers on how to go about it myself?
Many thanks for any help,
KB
|
|
|
|
|
Kayembi wrote:
...but I don't know how to set up the callback function.
It has the following signature:
int CALLBACK CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort);
The MSDN article Q170884 should give you some ideas, even though it's in VB.
Basically, each item that is added to the list control has an associated item data (use the LVM_SETITEM message for this). When CompareFunc() is called, lParam1 points to one item's data and lParam2 points to another item's data. You have to cast lParam1 and lParam2 to the appropriate type, and then do some sort of comparision.
|
|
|
|
|
Hello
Does anyone know how to print HTML files from a console application? I wanna print the files like the I Explorer shows them not the source code . I think a solution could be using IWebBwoser control but i don't know how to embed it to a console application
Thanks a lot
xxx
|
|
|
|
|
You could write a simple web resource provider[^] to download the page to disk, then just print the file by doing:
CString strPrintFile = "MyFile.htm";
SHELLEXECUTEINFO seInfo;
::ZeroMemory (&seInfo, sizeof (seInfo));
seInfo.cbSize = sizeof (seInfo);
seInfo.fMask = SEE_MASK_FLAG_DDEWAIT;
seInfo.hwnd = GetSafeHwnd();
seInfo.lpVerb = "print";
seInfo.lpFile = strPrintFile.GetBuffer(0);
seInfo.lpDirectory = "";
seInfo.nShow = SW_SHOWNORMAL;
BOOL bStatus = ::ShellExecuteEx (&seInfo);
strPrintFile.ReleaseBuffer();
if (!bStatus) {
CString strMessage;
strMessage.Format (IDS_CantPrintFile, strPrintFile.GetBuffer(0));
strPrintFile.ReleaseBuffer();
AfxMessageBox (strMessage);
}
/ravi
Let's put "civil" back in "civilization"
Home | Articles | Freeware | Music
ravib@ravib.com
|
|
|
|
|
I create a new document in my doc/view app, and when I close it asks if I want to save it. How do I keep it from asking me this?
I have tried SetModifiedFlag(FALSE) and that isn't working. Any other ideas?
"It is better to remain silent and be thought a fool than to open one's mouth and remove all doubt." - Abraham Lincoln
|
|
|
|
|
Jason Henderson wrote:
I have tried SetModifiedFlag(FALSE) and that isn't working.
Yes, but where are you doing this, and what code, even MFC, is setting it back to TRUE ? If you do anything at all to your document, its dirty flag will be set to TRUE . Set a breakpoint in SetModifiedFlag() . Once there, check the call stack.
|
|
|
|
|
I figured it out.
I'm overriding SaveModified now and it seems to be OK.
thanks
"It is better to remain silent and be thought a fool than to open one's mouth and remove all doubt." - Abraham Lincoln
|
|
|
|
|
Overriding BOOL CDocument::CanCloseFrame(CFrameWnd *pFrame) could also do the trick.
HTH,
K.
Every gun that is made, every warship launched, every rocket fired, signifies in the final sense a theft from those who hunger and are not fed, those who are cold and are not clothed - Dwight D. Eisenhower
|
|
|
|
|
Is this possible to write a service program running under NT/2000/XP, which has a global keyboard hook, and capture the user login name and pasword? has anybody tried it? I just don't want spend couple of days and then find out no way to do it.
Thanks
|
|
|
|