WritePrivateProfileString with int variable as value - variables

I'm quite new to C++.
Right at the moment I'm trying to write and read an ini file. As I'm working with VS 2010 I d'like to use the WritePrivateProfileString function.
testwise it works like this
WritePrivateProfileString("testsection","testkey","testvalue","testfile.ini");
I can run this successfully.
My idea now is to hand overthe value within a variable of course which represents an int value.
WritePrivateProfileString("testsection","testkey",testvalue,"testfile.ini");
I understand that I would have to cast it first to LPCSTR or char so it works.
int testvalue =12;
WritePrivateProfileString("testsection","testkey",intToChar(testvalue),"testfile.ini");
//convert int to LPCSTR
char* intToChar(int temp){
char wert[8];
itoa(temp, wert, 10);
cout << wert;
return wert;
it does write to the file. but instead of a number i get random signs like "<ยบ-" and stuff.
I've been trying for an hour now. Using different convertion (to LPCSTR, to char, to string) without any success.
what am I doing wrong? I really stat to get desperate.
thanks for any hint.
}

WritePrivateProfileString function can handle only "string", not "Integer Value".
You can make string from integer using itoa().

Related

Expected expression error assigning value in Objective-C

I am a new Objective-C programmer and I am working on a project.
I created a function which returns a float value and I declared some variables into it as shown here :
-(float)WN8Calculation{
int avgDAMAGE,avgFRAGS,avgSPOT,avgDEF,avgWIN;
long battles;
double expDAMAGE,expFRAGS,expSPOT,expDEF,expWIN;
double rDAMAGE,rFRAG,rSPOT,rDEF,rWIN;
float rDAMAGEc,rFRAGc,rSPOTc,rDEFc,rWINc;
float wn8;
int tank_id;
avgDAMAGE =2;
return wn8;
}
When I assign the value for the variable avgDAMAGE I get an error in that line saying "expected expression". Can someone help?
Well i solved this error (i guess) what i had to do is to just delete the line a rewrite it..
Thanks for your help guys.

Convert type* to type: cli::array>type,1>^

I need help converting a parameter of type
double* testVar
to a parameter of type
array<double> ^%parentVar.
I`m still trying to learn CLI C++ so this might be quite easy but I don't seem to figure it out.
Regards
Based on #Alex Farber comment the answer to this question is:
double *testVar; // This testVar takes a value from a function that I will not post here
int vectSize = 10;
array<double>^ TempVector= gcnew array<double>(vectSize);
Marshal::Copy(IntPtr(testVar),TempVector,0,vectSize);
parentVar=TempVector;
That was all!

Store an NSString as a fixed length integer?

having a bit of trouble finding a solution to this.
I want to take a large ordered text file of words and create - in the same order - a text file of fixed length numeric values.
For example:
Input File Output File
AAA -> 00000001
AAH -> 00002718
AAZ -> 71827651
Initially it seemed a hash function would do the trick. However they are one way. Also perhaps they are a bit "heavyweight" for this. After all, I don't need any cryptography. Plus, it's a reference file. It will never change.
Any compression is a bonus not essential. That said, I don't want the file to get any bigger than it already is. Which is why I don't just want to write out the words as text but with fixed lengths.
So, bottom line; input is a NSString of variable length, output is an integer of fixed length. And, I must be able to take the integer and figure out the string.
Any help much appreciated!
Thanks!
xj
Well, this would be a bit of a brute force method, but here's my guess.
Start by making a custom function to convert one letter of text to an integer less than 100. (I'm not sure if such a function already exists, if so then great!) You might need to just go to stuff like "if ([input isEqual: #"a"]){ return 1;}
Then, run that function on each letter of text, and get the final integer by combining the previous results.
For example:
int myVal1 = [intConverter firstLetter];
int myVal2 = [intConverter secondLetter];
int myVal3 = [intConverter thirdLetter];
int finalValue =100^3 + 100^2*myVal1 + 100*myVal2 + myVal3;
Then, finalValue would be of the form 1(myVal1)(myVal2)(myVal3), which is what I think you're looking for.
To get back the original string, simply use the mod (%) and division functions to get the individual values back, then run the intConverter function backwards. (This would probably mean writing a new function that basically runs those if statements in reverse, but oh well.)
I hope this helps.

WxWidgets using wxString

I'm taking a computer sciences class and we have to make a calculator using a GUI. I have been banging my head into a wall trying to even get input though.
Understand how to use pointers and GetValue(), to take in the input as a wxString but that does me no good, If I can't get the string as a double or integer then I can't perform operations on it.
Does anyone know how to convert wxString to Double? or even int?
Thanks in Advance
For this purpose, there are the two functions ToDouble and ToLong.
For details take a look at the offical documentation:
http://docs.wxwidgets.org/stable/wx_wxstring.html#wxstringtolong
http://docs.wxwidgets.org/stable/wx_wxstring.html#wxstringtodouble
EDIT: Example:
wxTextCtrl ctrl;
// user has entered a number
double number;
if( !ctrl.GetValue().ToDouble( &number ) )
// handle error
else
// continue...
Please note: It will only work if you enter a number. If you enter a term like 2+3 the function should return false. In this case you have to split the string up and interpret all numbers seperately.

simple question about assigning float to int

This is probably something very simple but I'm not getting the results I'm expecting. I apologise if it's a stupid question, I just don't what to google for.
Easiest way to explain is with some code:
int var = 2.0*4.0;
NSLog(#"%d", 2.0*4.0);//1
NSLog(#"%d", var);//2
if ((2.0*4.0)!=0) {//3
NSLog(#"true");
}
if (var!=0) {//4
NSLog(#"true");
}
This produces the following output:
0 //1
8 //2
true //3
true //4
The one that I don't understand is line //1. Why are all the others converting (I'm assuming the correct word is "casting", please correct me if I'm wrong) the float into an int, but inside NSLog it's not happening. Does this have something to do with the string formatting %d parameter and it being fussy (for lack of a better word)?
You're telling NSLog that you're passing it an integer with the #"%d" format specifier, but you're not actually giving it an integer; you're giving it a double-precision floating-point value (8.0, as it happens). When you lie to NSLog, its behavior is undefined, and you get unexpected results like this.
Don't lie to NSLog. If you want to convert the result of 2.0*4.0 to an integer before printing, you need to do that explicitly:
NSLog(#"%d", (int)(2.0*4.0));
If, instead, you want to print the result of 2.0*4.0 as a double-precision floating-point number, you need to use a different format specifier:
NSLog(#"%g", 2.0*4.0);
More broadly, this is true of any function that takes a variable number of arguments and some format string to tell it how to interpret them. It's up to you to make sure that the data you pass it matches the corresponding format specifiers; implicit conversions will not happen for you.
First, you never used floats in your program. They are doubles.
Second, the arguments of NSLog, printf and the likes are not automatically converted to what you specify using %d or %f. It follows the standard promotion rule for untyped arguments. See the ISO specification, sec 6.5.2.2.6 and 6.5.2.2.7. Note the super weird rule that inside these functions,
a float is automatically promoted to double,
and any integer smaller than an int is promoted to int. (see 6.3.1.1.2)
So, strictly speaking, the specification %f is not showing a float, but a double. See the same document, Sec. 7.19.6.1.8.
Note also that in your case 1 and 3, promotions are to double.
In examples 2, 3 and 4, the float is either being assigned to an int (which converts it) or compared with an int (which also converts it). In 1, however, you're passing the float as an argument to a function. The printf function allows all the arguments after the initial format string to be of any type, so this is valid. But since the compiler doesn't know you mean for it to be an int (remember, you haven't done anything to let the compiler know), the float is passed along as a floating-point value. When printf sees the %d formatting specifier, it pops enough bytes for an int from the argument list and interprets those bytes as an int. Those bytes happen to look like an integer 0.
The format string %d expects a decimal number, meaning a base 10 integer, not a floating point. What you want there is %f if you're trying to get it to print out 8.0
The first parameter to NSLog is a format string, then the second (and subsequent) parameters can be any types. The compiler doesn't know what the types should be at compile time and so doesn't try to cast them to anything. At run time NSLog assumes the second (and subsequent) parameters are as specified in the format string. If there's a mismatch unexpected and generally unhappy things happen.
Summary; Make sure you pass variables of the right type in the second (and subsequent) parameter.