|
Message Removed
modified 26-Jun-14 7:43am.
|
|
|
|
|
Hi,
I am no expert in C and I have started learning it again after several years. Excuse me for stupid questions. My question is simple.
When i give %f as conversion character inside printf, but pass some integer to it, what happens? It prints some garbage value? or is there any pattern?
Consider the below program..
#include <stdio.h>
main()
{
float gift1, gift2;
float total;
printf("How much do you want to spend on 1 \n");
scanf(" %f", &gift1);
printf("How much do you want to spend on 2 \n");
scanf(" %f", &gift2);
total = gift1+gift2;
printf("\nThe total you will be spending on gifts is $%.2f", total);
printf("\n %s %d %f %c", "Gokul", 90, 100, 'X');
return 0;
}
In the above program, i have added a final printf which has nothing to do with the rest of the program. I have simply added it. And i have passed the integer 100 to the %f conversion character. While executing the program, i give some random values for gift1,gift2 which accounts to, say 1958.9967. So, total is 1958.9967
But the value of total is also printed as part of my final line, like below
Gokul 90 1958.9967
Here, X is not printed as well. So, what is happening. Instead of 100, when i tried with 100.19, it gives the desired result (like below)
Gokul 90 100.19 X
I tried another small program like below..
#include<stdio.h>
main()
{
printf("\n %f specifies floating number, but i will give an integer",1987);
return 0;
}
Here, my output was like below
0.000000 specifies floating number, but i will give an integer.
Here, where does this 0.000000 comes from? In case of previous program it printed the value of "total" - why? what is the behavior here?
Btw, I am using Code Blocks compiler, if that's of any significance here.
Thanks in advance.
|
|
|
|
|
|
When %f is encountered in the format string, the computer attempts to pop a floating point number off of the stack, what it finds instead is 100 , an integer. When you changed it to 100.19 , the computer found what it was expecting.
My rule of thumb is always use a (double or float ) cast, or append .0 to any floating point numbers. That way, these problems and others are avoided.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
Dear You are trying to convert an int value into float without using any of the coversion processing thats why you got wrng ans. everytime when you put a int value
Similiarly when you are trying to put int value into float its also gave unrelevant answer
Frk
|
|
|
|
|
Hi Gokul,
I understood your doubt. Here i am trying to clarify why it is happening clearly.
printf("\n %s %d %f %c", "Gokul", 90,100,'X');
in the above piece of code if you observe clearly, with the assumption of starting address will be 0x1000, the contents of your piece of code will occupy memory as follows.
"Gokul"- at 0x1000
90 - at 0x1006
100 - at 0x1010
'X' - at 0x1015
Now as per your format specifiers first %s will fetch first 6 Bytes("Gokul"),
%d will fetch next 4 bytes of data as integer, so fetches 90,
now %f means it will fetch next 8 Bytes of data means from 0x1010 to 0x1017 which includes memory location of 'X' also, after fetching it will try to display float number but it won't display as no external coversion you are doing. So it will display 0.0000000
Then during this time we are at memory location 0x1018 in which some garbage value(Σ) will be there as we are not storing anything.
Even though if you give some other character in place of 'X' also it will give same value(Σ) as garbage is there in 0x1018 location.
if you change 100 to some float number or explicitly converted to float number((float)100), then it will occupy 8 bytes memory now. then no problems and no unwanted results you get.
If any clarifications feel free to ask, you will get help definitely.
Thank you.
modified 9-Aug-14 7:55am.
|
|
|
|
|
Hi I'm trying to learn 3D graphics using C++/MFC. I suppose the context of my questions don't really have to do with C++
perse but more linear algebra I guess. Please bear with me since I'm dumb as a rock.
From my research the pipeline consists of projections/transformation
"model (vertices)" -> World -> View Cube (or tho projection) -> screen.
after I transform the vertices of the model to "world" and then project it to the "view cube" for clipping I then flatten the vertices in the desired axis. So if I wanted a 2d orthographic side view, I would set the Y coordinate to 0 in the matrix to flatten.
Please see this graphic [^]
My question is, do I then need to rotate the left vector space I flatten the object(s) on to a "front" vector space and then draw on the screen?
thanks
modified 23-Jun-14 18:17pm.
|
|
|
|
|
Hi all
I want to achieve such an efect like - one view which takes all MDI area and some views which arelike non modal dialogs
How can I do it?
Two Child frames will be correct? one for "big" view which will be always visible and second for a "smal" views
maybe another solution would be better
Thank you in advance for help and advice
|
|
|
|
|
curious what are you showing in your MDI?
you can just use the same instance of CDocument object for both the MDI and the non modal dialogs.
just get a pointer to the MDI CDocument object and access within your non modal dialogs classe(s)
You can also just get pointers to the specific controls used in your MDI (tree, list, GDI obects) and then just acces these in your modal classes. So the data will all be the same.
there are several effective ways to do this, but it's situational
|
|
|
|
|
I am trying to compile the old code from y server to deply into the new server and ever service code is facing this problem .
I have installed visual studio 2008 and visual studio 2005 to compile these codes .
while compileing the code in debug mode and release i am facing this error
fatal error C1083: Cannot open include file: 'iostream.h': No such file or directory
I tried to change it as then the following errors are displyed
1>.\WFTeams.cpp(97) : error C2065: 'cout' : undeclared identifier
1>.\WFTeams.cpp(97) : error C2065: 'flush' : undeclared identifier
1>.\WFTeams.cpp(397) : warning C4996: 'strcat' was declared deprecated
1> c:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\string.h(78) : see declaration of 'strcat'
1> Message: 'This function or variable may be unsafe. Consider using strcat_s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See online help for details.'
1>.\WFTeams.cpp(398) : warning C4996: 'strcat' was declared deprecated
1> c:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\string.h(78) : see declaration of 'strcat'
1> Message: 'This function or variable may be unsafe. Consider using strcat_s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See online help for details.'
1>Build log was saved at "file://d:\devcodes\WFTeams\Release\BuildLog.htm"
1>WFTeams - 2 error(s), 3 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
fatal error C1083: Cannot open include file: 'iostream.h': No such file or directory
|
|
|
|
|
You really don't need to ask the same question in multiple places on the site
In vino veritas
|
|
|
|
|
Change #include <iostream.h> to the following 2 lines -
#include <iostream>
using namespace std;
«_Superman_»
I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++) (October 2009 - September 2013) Polymorphism in C
|
|
|
|
|
Thanks for the response .
I have tried replacing iostream.h as susggested and then it is showing the link errors
LINK : warning LNK4199: /DELAYLOAD:OleAcc.dll ignored; no imports found from OleAcc.dll
1>stdafx.obj : error LNK2001: unresolved external symbol __afxForceEXCLUDE
1>thread.obj : error LNK2001: unresolved external symbol __afxForceEXCLUDE
1>WFTeams.obj : error LNK2001: unresolved external symbol __afxForceEXCLUDE
1>Workflow.obj : error LNK2001: unresolved external symbol __afxForceEXCLUDE
1>ADSI.obj : error LNK2001: unresolved external symbol __afxForceEXCLUDE
1>classes.obj : error LNK2001: unresolved external symbol __afxForceEXCLUDE
1>Database.obj : error LNK2001: unresolved external symbol __afxForceEXCLUDE
1>Service.obj : error LNK2001: unresolved external symbol __afxForceEXCLUDE
1>WFTeams.obj : error LNK2001: unresolved external symbol "void __cdecl operator delete(void *)" (??3@YAXPAX@Z)
1>Workflow.obj : error LNK2001: unresolved external symbol "void __cdecl operator delete(void *)" (??3@YAXPAX@Z)
1>Workflow.obj : error LNK2001: unresolved external symbol "void __cdecl operator delete(void *)" (??3@YAXPAX@Z)
1>stdafx.obj : error LNK2001: unresolved external symbol "void __cdecl operator delete(void *)" (??3@YAXPAX@Z)
1>thread.obj : error LNK2001: unresolved external symbol "void __cdecl operator delete(void *)" (??3@YAXPAX@Z)
1>thread.obj : error LNK2001: unresolved external symbol "void __cdecl operator delete(void *)" (??3@YAXPAX@Z)
1>WFTeams.obj : error LNK2001: unresolved external symbol "void __cdecl operator delete(void *)" (??3@YAXPAX@Z)
1>Database.obj : error LNK2001: unresolved external symbol "void __cdecl operator delete(void *)" (??3@YAXPAX@Z)
1>Database.obj : error LNK2001: unresolved external symbol "void __cdecl operator delete(void *)" (??3@YAXPAX@Z)
1>Service.obj : error LNK2001: unresolved external symbol "void __cdecl operator delete(void *)" (??3@YAXPAX@Z)
1>Service.obj : error LNK2001: unresolved external symbol "void __cdecl operator delete(void *)" (??3@YAXPAX@Z)
1>ADSI.obj : error LNK2001: unresolved external symbol "void __cdecl operator delete(void *)" (??3@YAXPAX@Z)
1>ADSI.obj : error LNK2001: unresolved external symbol "void __cdecl operator delete(void *)" (??3@YAXPAX@Z)
1>classes.obj : error LNK2001: unresolved external symbol "void __cdecl operator delete(void *)" (??3@YAXPAX@Z)
1>classes.obj : error LNK2001: unresolved external symbol "void __cdecl operator delete(void *)" (??3@YAXPAX@Z)
1>stdafx.obj : error LNK2019: unresolved external symbol "void __stdcall AfxThrowInvalidArgException(void)" (?AfxThrowInvalidArgException@@YGXXZ) referenced in function "int __cdecl AfxCrtErrorCheck(int)" (?AfxCrtErrorCheck@@YAHH@Z)
1>thread.obj : error LNK2001: unresolved external symbol "void __stdcall AfxThrowInvalidArgException(void)" (?AfxThrowInvalidArgException@@YGXXZ)
1>WFTeams.obj : error LNK2001: unresolved external symbol "void __stdcall AfxThrowInvalidArgException(void)" (?AfxThrowInvalidArgException@@YGXXZ)
1>Workflow.obj : error LNK2001: unresolved external symbol "void __stdcall AfxThrowInvalidArgException(void)" (?AfxThrowInvalidArgException@@YGXXZ)
1>ADSI.obj : error LNK2001: unresolved external symbol "void __stdcall AfxThrowInvalidArgException(void)" (?AfxThrowInvalidArgException@@YGXXZ)
1>classes.obj : error LNK2001: unresolved external symbol "void __stdcall AfxThrowInvalidArgException(void)" (?AfxThrowInvalidArgException@@YGXXZ)
1>Database.obj : error LNK2001: unresolved external symbol "void __stdcall AfxThrowInvalidArgException(void)" (?AfxThrowInvalidArgException@@YGXXZ)
1>Service.obj : error LNK2001: unresolved external symbol "void __stdcall AfxThrowInvalidArgException(void)" (?AfxThrowInvalidArgException@@YGXXZ)
1>stdafx.obj : error LNK2019: unresolved external symbol "void __stdcall AfxThrowMemoryException(void)" (?AfxThrowMemoryException@@YGXXZ) referenced in function "int __cdecl AfxCrtErrorCheck(int)" (?AfxCrtErrorCheck@@YAHH@Z)
1>thread.obj : error LNK2001: unresolved external symbol "void __stdcall AfxThrowMemoryException(void)" (?AfxThrowMemoryException@@YGXXZ)
1>WFTeams.obj : error LNK2001: unresolved external symbol "void __stdcall AfxThrowMemoryException(void)" (?AfxThrowMemoryException@@YGXXZ)
1>Workflow.obj : error LNK2001: unresolved external symbol "void __stdcall AfxThrowMemoryException(void)" (?AfxThrowMemoryException@@YGXXZ)
1>ADSI.obj : error LNK2001: unresolved external symbol "void __stdcall AfxThrowMemoryException(void)" (?AfxThrowMemoryException@@YGXXZ)
1>classes.obj : error LNK2001: unresolved external symbol "void __stdcall AfxThrowMemoryException(void)" (?AfxThrowMemoryException@@YGXXZ)
1>Database.obj : error LNK2001: unresolved external symbol "void __stdcall AfxThrowMemoryException(void)" (?AfxThrowMemoryException@@YGXXZ)
1>Service.obj : error LNK2001: unresolved external symbol "void __stdcall AfxThrowMemoryException(void)" (?AfxThrowMemoryException@@YGXXZ)
1>stdafx.obj : error LNK2001: unresolved external symbol "const type_info::`vftable'" (??_7type_info@@6B@)
1>thread.obj : error LNK2001: unresolved external symbol "const type_info::`vftable'" (??_7type_info@@6B@)
1>WFTeams.obj : error LNK2001: unresolved external symbol "const type_info::`vftable'" (??_7type_info@@6B@)
1>Workflow.obj : error LNK2019: unresolved external symbol "const type_info::`vftable'" (??_7type_info@@6B@) referenced in function "public: virtual struct CRuntimeClass * __thiscall CWorkflowRecordset::GetRuntimeClass(void)const " (?GetRuntimeClass@CWorkflowRecordset@@UBEPAUCRuntimeClass@@XZ)
1>ADSI.obj : error LNK2001: unresolved external symbol "const type_info::`vftable'" (??_7type_info@@6B@)
1>classes.obj : error LNK2001: unresolved external symbol "const type_info::`vftable'" (??_7type_info@@6B@)
1>Database.obj : error LNK2001: unresolved external symbol "const type_info::`vftable'" (??_7type_info@@6B@)
1>Service.obj : error LNK2001: unresolved external symbol "const type_info::`vftable'" (??_7type_info@@6B@)
1>stdafx.obj : error LNK2019: unresolved external symbol _malloc referenced in function "public: static void * __cdecl ATL::CCRTAllocator::Allocate(unsigned int)" (?Allocate@CCRTAllocator@ATL@@SAPAXI@Z)
1>thread.obj : error LNK2001: unresolved external symbol _malloc
am i missing any property setup ...
please let me know
|
|
|
|
|
The first warning is just telling you OleAcc.dll is not used and you can remove the reference to it.
Okay now the problem is simple they are all linker errors you are through the compiler process
That means the linker include directory is wrong or not set at all and those are standard MFC libraries so they should be available.
I am going to take a wild guess take a look under the project settings->linker->command line
It wouldn't happen to contain the command /NODEFAULTLIB would it?
That is the only way I can think to get that problem and if it's there please remove it.
In vino veritas
|
|
|
|
|
So have you looked to see if the iostream.h file exists? If you can't find it, neither can VS.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
if the problem is cannot open include file <iostream.h>, You must first check the following:
1. Check your directories if your compiler you used was save in the same directories.
2. How to check your directories (press Alt O(Options) in your C ++(IDE)compiler then choose directories.
3. Then change your directories if it is not properly placed. (If you save your compiler in the C drive, the directories must in C Drive also.
Celine
|
|
|
|
|
Quote: This code work:
int CPageWelcome::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CWnd::OnCreate(lpCreateStruct) == -1)
return -1;
CStatic * lblPresent = new CStatic();
lblPresent->Create(L"Present", WS_CHILD | WS_VISIBLE,
CRect(20, 20, 100, 40), this);
return 0;
}
This code dont work:
int CPageWelcome::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CWnd::OnCreate(lpCreateStruct) == -1)
return -1;
CStatic lblPresent;
lblPresent.Create(L"Present", WS_CHILD | WS_VISIBLE,
CRect(20, 20, 100, 40), this);
return 0;
}
Please help i dont understand. Thank.
|
|
|
|
|
In the first code snippet, the CStatic object is created on the heap and will remain in memory till delete is called on the object.
In the second code snippet, the CStatic object is created on the stack and will go out of scope (will be destroyed) as soon as OnCreate exits.
«_Superman_»
I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++) (October 2009 - September 2013) Polymorphism in C
|
|
|
|
|
yea thank and if i make
private:
CStatic mpStatic;
like private member variable
End block in OnCreate not destroyed CStatic object and dont work.
If use pointer all OK.
OMG it is work and before 1 hour not =( sorry fkn bug.
Thank you for help. =)
modified 23-Jun-14 4:40am.
|
|
|
|
|
Have you tried to set
CStatic lblPresent
as member variable ? Something like
CStatic m_lblPresent
|
|
|
|
|
I have my program like this
int main()
{
char *p = "Test"
p++;
p++;
printf("%s", p[-1]);
return 0;
}
what will be printed by printf statment
Thanks,
Sakthi
|
|
|
|
|
The statement is perfectly valid "in this case".
Using an index will simply add it to the address.
So p[-1] will internally become *(p-1) .
In this case, since you're incrementing p twice, p[-1] should print e .
Correction - My earlier comment would be true if you change the statement to printf("%c", p[-1]);
«_Superman_»
I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++) (October 2009 - September 2013) Polymorphism in C
modified 23-Jun-14 4:07am.
|
|
|
|
|
p[-1] returns the character at that position ('e' in your case). When passing this value as address of a string to printf, it will try to print the memory content at the address 0x65 as string.
|
|
|
|
|
While p[-1] is valid (why didn't you try to compile it?), you are using it the wrong way: a char pointer (char * ) is expected by the format specifier "%s" while you are passing a char ( p[-1] , is like *(p-1) , that is a char ). You should have written either:
printf("%s\n", &p[-1]); producing output
est or
printf("%c\n",p[-1]); , producing output
e
THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?!
-- C++ FQA Lite
|
|
|
|
|
But, when i executed the program in Visual studio i got run time error
Thanks,
Sakthi
|
|
|
|