|
How can I convert a string (char* ) to a integer or a float with error checking?
I want to convert a string to a integer or a float:
<br />
"1" --> int 1<br />
If the string is something like that "%"&"/ssdd" I want to know that --> ERROR!
Daniel
---------------------------
Never change a running system!
|
|
|
|
|
static _Convert ConvertLong (
const char *pszValue, long *plValue)
{
errno = 0;
char *pszEnd = NULL;
long lReturn = strtol (pszValue, &pszEnd, 10);
if (pszEnd)
{
while (isspace (*pszEnd))
pszEnd++;
if (pszValue == pszEnd || *pszEnd != 0)
return Convert_Invalid;
}
if (errno == ERANGE)
{
if (lReturn < 0)
return Convert_Underflow;
else
return Convert_Overflow;
}
*plValue = lReturn;
return Convert_Ok;
}
Tim Smith
I'm going to patent thought. I have yet to see any prior art.
|
|
|
|
|
Thanks !!!
Daniel
---------------------------
Never change a running system!
|
|
|
|
|
Here's some old code of mine that checks if a string value is numeric according to how you need it. For example, if you don't want it to be negative, you pass the Unsigned flag; if it can have a decimal point, you pass it the Decimal flag; etc. Notice that it assumes that a decimal point is a dot.
enum NumericFlag
{
Unsigned,
Signed,
Decimal,
SignedDecimal
};
bool IsNumeric(const char* szValue, NumericFlag flag ) const
{
assert(szValue);
int nLen = strlen(szValue);
for (int iPos = 0, nDecimalPos = -1; iPos < nLen; iPos++)
{
char c = szValue[iPos];
if (isdigit(c))
continue;
if (c == '-' && iPos == 0 && (flag & Signed))
continue;
if (c == '.' && nDecimalPos < 0 && (flag & Decimal))
{
nDecimalPos = iPos;
continue;
}
return false;
}
return !!nLen;
}
Regards,
Alvaro
Well done is better than well said. -- Benjamin Franklin
(I actually prefer medium-well.)
|
|
|
|
|
Thanks !!!
Daniel
---------------------------
Never change a running system!
|
|
|
|
|
I was about to suggest VarI4FromStr, but I see you're doing char* and not wchar_t* .
--
Only in a world this sh*tty could you even try to say these were innocent people and keep a straight face.
|
|
|
|
|
Thanks!!!
Daniel
---------------------------
Never change a running system!
|
|
|
|
|
Hi,
GetTextExtentExPoint and methods like that only work fine with fotns that have no escapement..., How can I get that size for a 90º rotated font ?
Is there any method, trick or whatever to do that ? ( swaping the values is not good enoug some pixels of differences).
Greetings
Braulio
|
|
|
|
|
I'm not sure this is what you need, but the function below tells you how many pixels are taken up by a piece of text when it's displayed on a window using a given font. It's taken out of my CAMSWnd article here[^] on CP.
int CAMSWnd::GetTextExtent(CWnd* pWnd, const CString& strText, CFont* pFont )
{
ASSERT(IsWindow(pWnd->GetSafeHwnd()));
int nExtent = 0;
int nLen = strText.GetLength();
if (nLen)
{
TEXTMETRIC tm;
CDC* pDC = pWnd->GetDC();
CFont* pFontOld = pDC->SelectObject(pFont ? pFont : pWnd->GetFont());
pDC->GetTextMetrics(&tm);
CSize size = pDC->GetTextExtent(strText, nLen);
nExtent = size.cx + tm.tmAveCharWidth;
pDC->SelectObject(pFontOld);
pWnd->ReleaseDC(pDC);
}
return nExtent;
}
Regards,
Alvaro
Well done is better than well said. -- Benjamin Franklin
(I actually prefer medium-well.)
|
|
|
|
|
I need assistance with the following code for factorials. The program is outputting incorrect answers.
// DESCRIPTION
// This program is designed to read in non-negative integers n
// and to output the value of n! = n(n-1)(n-2)...(3)(2)(1).
// Note 0! is defined as 1.
// The program should continue to read in and process new
// values of n until:
//
// 1) The value of the previous n! exceeds MAXFACT
// or
// 2) A total of MAXCOUNT input values have been processed.
//
// WARNINGS
// This program has some syntax errors and some semantic errors
// User must not enter data less than 0 or large numbers
//
#include <iostream.h>
#include <stdlib.h>
using std::cout;
using std::cin;
using std::endl;
//Place any function prototypes here:
void factorial (int n);
const int MAXCOUNT = 4;
const int MAXFACT = 10000;
int main()
{
bool done = false; //i.e. not done yet
int n, counter=0;
unsigned int n_fact = 0;
while ((counter < MAXCOUNT) || (!done))
{
cout << "Enter a non-negative integer:";
cin >> n;
//compute n factorial and display the result
factorial(n);
cout << "The value of " << n << "! is " <<fact(n) <<="" endl;
="" if="" (n_fact=""> MAXFACT) //catch large factorial
done = true;
counter++; //count this iteration
}
//upon loop exit, one of the following must be true:
//counter reached MAXCOUNT or done (due to large factorial)
system("PAUSE");
return 0;
}
void factorial(int n)
{ //compute n! by multiplying together 1, 2, 3, ..., n
int counter;
unsigned int fact=0;
cout << "The value of n received is:" << n << endl;
cout << "At the start of the loop, fact is: " << fact << endl;
for (counter = n; counter > 0; counter--) {
fact *=counter;
return fact;
}
cout << "The calculated factorial is " << fact << "\n";
}
|
|
|
|
|
for (counter = n; counter > 0; counter--) {
fact *=counter;
return fact; // I think this is your problem
}
also if n > 12, then "unsigned int fact=0;" should realy be a double or something
|
|
|
|
|
The code makes it look like it would always return 0:
unsigned int fact=0;
should be
unsigned in fact=1;
|
|
|
|
|
The code does return 0 all of the time and I am having trouble grasping how to return the value obtained in the factorial function to the main function. Any advice?
|
|
|
|
|
I am a beginner and I am trying to write a program that takes a five digit inputed value and adds the digits together. I am trying to use a for loop to do this but I am having trouble. Please help.
|
|
|
|
|
Say more !
What are your problems ?,
Where are you stuck ?
Can you describe how you would do this in your own word ?
Can you formalise it into code ?
Show some code ...
Max.
|
|
|
|
|
So far I have:
int digit; //digit is the five digit number to be inputed by user
cin<
|
|
|
|
|
Is this what you are tring to do?
#include <stdio.h>
int main(int argc, char* argv[])
{
if(argc > 1)
{
double nval = 0;
for(size_t i = 0, count = strlen(argv[argc - 1]);
i < count;++i)
nval += argv[argc - 1][i] - '0';
printf("sum is : %f\n", nval);
}
return 0;
}
Is this some kind of homework or something?
|
|
|
|
|
How are you storing the five digits?
Are they in a char [] or an int []?
Michael
Fat bottomed girls
You make the rockin' world go round -- Queen
|
|
|
|
|
Post the code you have already begun to write and we will help you. If you have not already started to code, tell us more precisely how you would like to do it. You wno't learn anything if we throw the solution on this forum.
~RaGE();
|
|
|
|
|
Talk you your teacher or teacher's aid; you are clearly missing basic concepts that were discussed in class.
|
|
|
|
|
//I have now come up with the following but it still is not working properly:
int main()
{
int digit;
int sum;
int Total;
int num;
cout << "Enter a five digit number followed by enter: " ;
cin >> digit;
for (int i=4; i>=0; i--)
{
int p = pow(10,i);
num=digit/p;
digit=digit%p;
sum = sum + num;
}
cout << "The sum is " << sum << endl;
//Something is wrong with the sum = sum + num line. The previous lines work properly.
|
|
|
|
|
Key problem:
- Sum is not initialized to zero. (Set your warnings to level 4 if using VC7. If using VC6, you'll have to use pragma to ignore several warnings.)
Minor problems:
- Total is not used
- num should be a local variable.
Algorithm problem:
- Using pow() is inefficient. (Remember, it doesn't matter in which order you add the digits!)
|
|
|
|
|
So do you advise me putting cin >> digit into the for loop and then adding the digits to sum?
|
|
|
|
|
Only if you are allowed to have the user enter one digit at a time.
Otherwise you can process the digits from right to left:
while (digit)
{
sum += digit%10;
digit /= 10;
}
Of course this assumes: digit >= -99999 && digit <= 99999.
|
|
|
|
|
Hi, I have a problem (I think a big problem).
The language is C++. The operation sistem is Windows 2000.
If I create two application. In the first application fill a structure, open a file and send this information (pointer to file) at second application which fill the file. When the second application end your job, the control switch at first application which close the file.
The question is:
can I open a file in a application sending the pointer at second applicaton to fill the file.
|
|
|
|