|
Hi Roger,
Thank You Very Much for giving me a solution.
Thanks & Regards,
Uday.
|
|
|
|
|
Hello Everybody,
I am new to C programming not yet even C++ experienced. I am following instructions Beginning C (from novice to Professional) Forth Edition from Ivor Horton. I am working on Program and 7.14.
I am compiling with Visual Studio 2010 Express and NetBeans 6.9.1
The program compiles without errors or warnings with NetBeans.
I really like the VS 2010 Express package because of the ease of looking at locals and would like to know more about VS 2010 Ex. This is why I come to you.
This programs has:
#include <stdbool.h> for a header and
bool sorted = false; for a declaration.
I get errors building the program in VS 2010 Ex. the stdbool.h header is not in any sub folder of the main VS 2010 Ex. directory.
I tried to build it as a cpp and c file and same results.
I found a stdbool.h from the Bloodshed DevC++ or Bloodshed and that did not work.
I downloaded the pdf of C99 standard for c dated 2005 could not understand how to make the 6 lines of code to work correctly. seemed way to advance for a begginer.
I tried using #include <windows.h> that was recommended from a user here at codeproject.com.
I have created a small program just to test bool true and false pulled from Program 7.14 of the C programming book I am learning from.
I made this:
#include <stdio.h>
int main(void)
{
int false = 0;
int true = 1;
int i = 0;
int bool = false;
char sorted = ' ';
sorted = false;
return 0;
}
but as you experience will see that false will not be zero but rather a decimal of 32
Nothing worked.
HELP!! Please..
rarael
|
|
|
|
|
Hi,
Visual Studio is capable of building C programs as well as C++ programs.
The distinction between C and C++ by default is made based on the file extension (.c versus .cpp).
In C, I have never seen or needed a stdbool.h header file (I've used dozens of C compilers for all kinds of microprocessors).
Most C programs I ever wrote had
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
for starters. That was sufficient to get the symbols TRUE and FALSE predefined (note the upper-case in C, not in C++).
Basically, one of those header files contains:
#define FALSE 0
#define TRUE 1
BTW: I must advice against confusing names for variables; in your snippet that would be true, false, bool as those sound like something special but really aren't. C has no actual bool type (other languages do), so whenever you need a boolean value, you normally use a numeric type, mostly an int .
Finally, I see only one way your code will ever hold or show a value of 32: the space character has a value of 0x20, that is 32 in decimal. So "sorted" will be 32, but nothing else will.
Hope this helps.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
modified on Sunday, December 19, 2010 11:18 PM
|
|
|
|
|
Thank you very much!
I took your insight and experience and applied them. Thank you very much again.
It worked out great. Just had to put the sorted = false; to the bottom of all
the declarations and had no errors or warnings in the build. All the things I tried through searches, a
build would recommend to change the string function (strcpy) to another string function.
I really appreciate the help and insight. I was getting frustrated with
google searching and the dead ends that resulted in many forums.
I changed this from the book:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#define BUFFER_LEN 100 /* Length of input buffer */
#define NUM_P 100 /* maximum number of strings */
int main(void)
{
char buffer[BUFFER_LEN];
char *pS[NUM_P] = { NULL };
char *pTemp = NULL;
int i = 0;
bool sorted = false;
int last_string = 0;
To this from your experience:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFER_LEN 100 /* Length of input buffer */
#define NUM_P 100 /* maximum number of strings */
#define false 0
#define true 1
int main(void)
{
char buffer[BUFFER_LEN];
char *pS[NUM_P] = { NULL };
char *pTemp = NULL;
int i = 0;
int sorted = 0;
int last_string = 0;
sorted = false;
|
|
|
|
|
You're welcome. I'm glad you're making progress.
I have two comments:
1.
if it is C code you want, you should use TRUE and FALSE (upper-case), and those really should already have be defined (as in #define...) somewhere in one of those header files for you, no need to do it yourself inside your code. You can define your own constants, and use those, but that would deviate from the conventions, and might become confusing later on. BTW: Under no condition you should change the values representing TRUE/true and FALSE/false!
2.
I don't know the book you are referring to; if it is causing too much hassle, I'd suggest you look for another one. On the other hand, if you made it to chapter 7, it may be just fine. Or maybe you skipped a lot?
|
|
|
|
|
improper use a #define in one of the files
|
|
|
|
|
Just to add to Luc's answer - stdbool.h is only defined for C99, Microsoft's C compiler only understands C89 and shows no sign of ever becoming C99 compliant.
Cheers,
Ash
|
|
|
|
|
I'm trying to get a VC 2008 project running on a Windows 7 machine. It builds fine but won't run. The worst thing is that it doesn't even give any useful message about what's wrong, just "The application was unable to start properly". The same project works fine on a Windows XP machine. The development environments are exactly the same on both machines. Anyone have any ideas?
|
|
|
|
|
Download dependency walker; it may be that you need to distribute files such as msvcp100.dll and msvcr100.dll (if you used VC 2010)
|
|
|
|
|
Thanks for the answer.
It's funny, dependancy walker is telling me there are missing dlls on the system that's working. Just suspicious how accurate it is.
|
|
|
|
|
Yes, it does that on my machine too. You have to compare the list on the one that is working to the one that's not.
|
|
|
|
|
I have designed a owner draw button. But I need to calculate the width of button at run time according to text length.
So I used
<br />
m_Font.CreateFont(15,0,0,0,FW_BOLD ,0,0,0,0,0,0,0,0,_T("Arial"));<br />
SetFont(&m_Font);<br />
dc->GetTextMetrics(&tm);<br />
int nWidth = tm.tmAveCharWidth + tm.tmInternalLeading;<br />
nWidth = nWidth * m_szCaption.GetLength(); <br />
<br />
But I am not gwtting the correct width. Whats wrong, pleas guide......
|
|
|
|
|
use CDC::DrawText with the DT_CALCRECT flag to compute the effective RECT the text will take.
go have a look at the documentation.
Watched code never compiles.
|
|
|
|
|
in the following code:
double a;
sscanf_s("0.02", "%.2f", &a);
i get Invalid Input Format exception! what's the problem? i also tried %1.2f and %4.2f, but no change.
|
|
|
|
|
double a;
sscanf_s("0.02", "%lf", &a);
|
|
|
|
|
yes, it works. but since the format is the same as what is used for showing the property (CMFCPropertyGridProperty::m_strFormatDouble), i've to specify the number of digits i want to be shown after the decimal point, so i can't use %lf.
|
|
|
|
|
Your format string is wrong, see here[^] for the width specification in sscanf .
Just say 'NO' to evaluated arguments for diadic functions! Ash
|
|
|
|
|
thank u, i read it, but i couldn't figure out what to choose as format for a floating point variable with read part of variable digits number and floating point of always two digits.
|
|
|
|
|
As it says in the link I showed: The width field is a positive decimal integer controlling the maximum number of characters to be read for that field. No more than width characters are converted and stored at the corresponding argument. Fewer than width characters may be read if a whitespace character (space, tab, or newline) or a character that cannot be converted according to the given format occurs before width is reached.
So in your case you would code it as
double a;
sscanf_s("0.02", "%4f", &a);
since you have 4 characters in your string value. The number of digits after the decimal point is largely irrelevant for floats and doubles when reading in, it is only important when formatting for output. In general you are better off not specifying a width value as it can lead to loss of precision in input strings - unless your users all know exactly what format to use for their input data.
Just say 'NO' to evaluated arguments for diadic functions! Ash
|
|
|
|
|
thank u for the description. the problem is that format strings for sprintf_s and sscanf_s differ. for sscanf_s including a . in format causes Invalid Input Format exception in anyway. as u mentioned, the input string is read by the number of bytes specified in the format b4 f and the chunk string is processed to be converted into a float. while in sprintf_s . has meaning. %4.2f for example means that the output string must contain at least 4 characters and exactly 2 digits after decimal point. if it be needed this may increase, eg. %2.2f or even %1.2f lead to the same output 0.02 or even 123456789.00.
this is while we has only one format for a grid ctrl, not two separate formats, one for sprintf_s and one for sscanf_s. as i said b4 it's stored in CMFCPropertyGridProperty::m_strFormatFloat or CMFCPropertyGridProperty::m_strFormatDouble. this uniqueness in the source of the problem.
now what's ur suggestion?
i've also another problem. spin ctrls are designated for integral fields. i need to attach a spin ctrl to a field of type float. for example when i've 12.34 in the field it may get 11.34 or 13.34 with up and down arrows. if i specify it to limit to 20, after 19.34 it gets 20.00 with arrow up. when i specify low limit of zero it gets 0.00 after 0.34 with arrow down. the spin ctrl doesn't expect the input field to be a float while it doesn't contrast with its behavior.
so the summary of the two problems are:
1. how can i overcome the uniqueness of floating point fields format in a grid ctrl which is both used for showing and inputting?
2. how can i have a spin ctrl for a floating point input field?
thx
|
|
|
|
|
1. I'm not sure I understand the problem, if you require different format strings for input and output then you need to code round the problem.
2. Don't use a floting point number, use an integer and multiply by 100 and display in the form you want. You may need to use a separate text box for the display rather than one that is directly connected to the spin control. Thus to display 1.05, you would use a value of 105 and use division and mod to separate the two halves, then display using a format of "%d.%.2d".
Just say 'NO' to evaluated arguments for diadic functions! Ash
|
|
|
|
|
all of the problem is that i like to least code, otherwise i would code another CSpinCtrl class as well as another CMFCPropertyGridProperty!
2. i've also to set steps of up and down arrows to 100 instead of increment and decrement. i prefer to use a different class than the original CSpinCtrl instead.
1. i tried to explain accurately! the problem is that the format strings for sprintf_s and sscanf_s do not process similarly. dot in the format string for sscanf_s has no sense while in the format string of sprintf_s has meaning. since the format string for type double or float is only one for both presenting the value or evaluating it, i've to replace the class with another one which i alter by code rounding it and this is what exactly i wanted to avoid.
2. it's the same for a spin ctrl. instead of calling EnableSpinControl for the property, i've to define another function to create another spin ctrl class with the desired functionality and this is also what i wanted to avoid.
i wonder how such great class are coded with such fool limitations!
thanx anyway
|
|
|
|
|
ilostmyid2 wrote: i wonder how such great class are coded with such fool limitations!
Sorry, no idea, perhaps you should direct your comments to Microsoft, as they are the ones responsible for it.
Just say 'NO' to evaluated arguments for diadic functions! Ash
|
|
|
|
|
yeah, good idea, although i get my answers mostly from codeproject rather than MS!
and u mean there's really no option other than replacing codes?
|
|
|
|
|
ilostmyid2 wrote: i wonder how such great class are coded with such fool limitations!
The origins are from C or even earlier.
And myself I don't see the problem.
First since the methods do not in fact do the same thing it isn't surprising that they use different idioms.
Second the point of scanf is to read/parse data. And the solution you were given seems to me to do exactly that. Could you explain what exactly it isn't doing that you want it to do in terms of reading/parsing data?
|
|
|
|