Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > C/C++
You last visited: Today at 10:04

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



[Code Snippet] getFloatFromString [C++

Discussion on [Code Snippet] getFloatFromString [C++ within the C/C++ forum part of the Coders Den category.

Reply
 
Old   #1
 
NikM's Avatar
 
elite*gold: 0
Join Date: Aug 2010
Posts: 972
Received Thanks: 1,583
[Code Snippet] getFloatFromString [C++

I wrote a small function for getting a float from a string.
I know that this function isn't perfect.
It isnt very hard to fix it
I belive in you guys :P

Code:
using namespace std;

float getFloatFromString (char *szBuffer);

int main ()
{
	float fCount = getFloatFromString ("Ich wasche meinen Hans Günter jeden Tag 2412.1234");

	printf ("%.4f\n", fCount);

	getchar ();
	return (0);
}

float getFloatFromString (char *szBuffer)
{
	float fGewicht = 0.0f;
	float fOperator = 1.0f;
	int iTemp = 0;
	int iCounter = 0;

	while (*szBuffer != 0 && *szBuffer != '.')
	{
		if ((*szBuffer >= '0' && *szBuffer <= '9'))
			fOperator /= 10.0f;
		
		szBuffer ++;
		iCounter ++;
	}

	szBuffer -= iCounter;

	for ( ; *szBuffer != 0 ; szBuffer++)
	{
		if ((*szBuffer >= '0' && *szBuffer <= '9'))
		{
			fOperator *= 10.0f;
			iTemp = (int)(*szBuffer) - 48;
			fGewicht += (float)(iTemp / fOperator);
		}
	}

	return (fGewicht);
}
Have fun

Credits: NikM
NikM is offline  
Old 02/15/2012, 14:03   #2
 
elite*gold: 0
Join Date: Feb 2012
Posts: 37
Received Thanks: 10
Netter Code, ich würde aber nicht empfehlen, in diesem Fall das Rad neu zu erfinden.

Unter C bieten sich die Funktion der strtod() Familie an (siehe )

und unter C++ würde man eher einen std::stringstream verwenden, oder falls man C mag / errno verwenden möchte / mit char arrays arbeitet, auch strtod().

hier ein std::stringstream Beispiel:
Code:
#include <sstream>
#include <iostream>

int main()
{
  std::string foo("3.141592");
  std::stringstream s(foo);

  float f;
  s >> f;

  std::cout << f << std::endl;
  return 0;
}
Das ist eleganter und vermutlich auch effizienter als eine eigene Implementierung

Grüße,
cheesecake
käsekuchen11elf is offline  
Thanks
1 User
Old 02/15/2012, 15:44   #3
 
elite*gold: 5
Join Date: Sep 2006
Posts: 385
Received Thanks: 218
This would be even better:
Code:
template<typename _TO, typename _FROM>
_TO ConvertTo(_FROM val)
{
	_TO retValue;
	std::stringstream ss;
	ss << val;
	ss >> retValue;
	return retValue;
}
allows you:

Code:
double foo = 1.23;
int bar = ConvertTo<int>(foo);
std::string str = ConvertTo<std::string>(bar);
Edit:
Oh, this guy speaks english! Sorry, I'll translate my posting!

@SkyneT:
Oh, haven't seen that! Well spotted!
Nightblizard is offline  
Thanks
4 Users
Old 02/15/2012, 16:23   #4
 
.SkyneT.'s Avatar
 
elite*gold: 273
Join Date: Sep 2010
Posts: 1,831
Received Thanks: 786
Code:
	system ("pause");
... Dont use that !

Use this:
Code:
	std::cin.get();
or:
Code:
	getchar();
.SkyneT. is offline  
Thanks
1 User
Old 02/15/2012, 22:05   #5
 
NikM's Avatar
 
elite*gold: 0
Join Date: Aug 2010
Posts: 972
Received Thanks: 1,583
Meine Muttersprache ist Deutsch ^^
Ihr könnt also ebenso gerne auf Deutsch anworten :P

@Nightblizard
Deine Methode mit der Template gefällt mir

@.SkyneT.
Why not :O
NikM is offline  
Old 02/15/2012, 22:17   #6
 
elite*gold: 5
Join Date: Sep 2006
Posts: 385
Received Thanks: 218
Darum:

Der Aufwand/Nutzen Faktor ist viel zu gering.
Nightblizard is offline  
Thanks
2 Users
Reply


Similar Threads Similar Threads
[Snippet] Varianten zum Umgehen von VAC3
12/16/2011 - C/C++ - 3 Replies
Hi Com, hier ein paar Methoden um eine auf HL1 & HL2 Engine basierende .dll secure hingegen VAC3 zu kriegen. - Professional protection & bypass methods -> String destruction -> Polymorphism -> PEB hiding -> FULL HideProcess
[Snippet] Anti Idle in Chat Lobby
12/01/2011 - Diablo 2 Programming - 0 Replies
Ich hatte hier irgendwo schonmal mein Problem erwähnt, das ich die Battle.net Connection verliere wenn meine Chars zulange (>3min) im lobby chat rumlümmeln. Daher habe ich folgendes in Muddy´s Leecher Startpunkt (NTBotLeech.ntj) eingefügt, damit er alle 60 Sekunden kurz mal die Join Liste öffnet und wieder schliest, solange er auf das nächste Spiel wartet. Zu dem If Block bei Zeile ~185 if(SayChatMsgAfterGame) { folgende Zeile hinzufügen ChatJoinedTime = GetTickCount(); damit wissen...
[Snippet]Neue Funktion in C++, um AutoIT & Co. auf 32Bit.
12/04/2010 - Coding Releases - 5 Replies
Hey, ich hab für euch mal ein kleinen C++ Code. Wer C++ kann, weiß wozu er gut ist. Hier mit es möglich, Autoit und andere Sprachen auf 32Bit Systemen laufen zu lassen. #include <windows.h> HINSTANCE hInst; DWORD GetPixelDLLFunc; HWND hWnd;
[New] Alba Kingz Album Snippet!! [Rap]!!
08/10/2010 - Main - 3 Replies
Hallo Leute, http://img576.imageshack.us/img576/6324/henwdseu1 .jpg Ich wollte euch mal Alba Kingz vorstellen!! Alba Kingz ist eine Albanische Musik Gruppe mit sehr viel Sehr viel Talent. Ich Hoffe das ich euch nicht Störe oder so aber wollte nur mal Zeigen!!
[Code Snippet]Facing Character to X , Y
10/27/2009 - Aion Hacks, Bots, Cheats & Exploits - 4 Replies
Hey community, i want to share some code snippets to you because NCsoft changed a little bit in Aion and now i want to share a new Facting to X Y method. ;) Thanks to PharmerPhale for some help. :) C# Code private double RotNeeded(float X, float Y) {



All times are GMT +1. The time now is 10:04.


Powered by vBulletin®
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2025 elitepvpers All Rights Reserved.