stuck converting a char to an integer

Programming, for all ages and all languages.
cjhawley001
Member
Member
Posts: 29
Joined: Mon Jun 30, 2008 9:51 am

Re: stuck converting a char to an integer

Post by cjhawley001 »

sorry. was i that unclear? hm, maybe i should start working on my english instead of my programming #-o
but anyways thanks for helping, this forum is great!

best resource i have seen in a long time!!

thanks!!!!!

p.s i will make my self clearer with source code next time and take more consideration with my posts!

-chris
User avatar
suthers
Member
Member
Posts: 672
Joined: Tue Feb 20, 2007 3:00 pm
Location: London UK
Contact:

Re: stuck converting a char to an integer

Post by suthers »

Well feel free to ask any questions, but do make them clearer next time...
Jules
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: stuck converting a char to an integer

Post by Solar »

cjhawley001 wrote:any help or comments(bad or good) welcome..
Just to be sure, I would look at the standard functions strtol() and sprintf() - perhaps you are making your life more difficult than necessary.
User avatar
AndrewAPrice
Member
Member
Posts: 2299
Joined: Mon Jun 05, 2006 11:00 pm
Location: USA (and Australia)

Re: stuck converting a char to an integer

Post by AndrewAPrice »

Omega wrote:Cargo Cult is just being nice. The Uni CS classes (at least where I am at) are just ridiculously weak. I don't buy books or even go to my CS classes, except for the exams (we turn our work in via Black Board) and I still get A's and B's.
As a game programmer we're entirely C++. Even the animators take an introductory course (they only get as far as STL), although the game design students have to do prototyping in Dark Basic :roll:.

Oh, and we have Rapid Application Development which is C# (the WinForms designer makes it simple to make level editor interfaces and the such rapidly), but altogether C# only constitutes 4.16% of the entire degree.
My OS is Perception.
User avatar
AndrewAPrice
Member
Member
Posts: 2299
Joined: Mon Jun 05, 2006 11:00 pm
Location: USA (and Australia)

Re: stuck converting a char to an integer

Post by AndrewAPrice »

Combuster wrote:From my own experience, university C++ courses tend to teach cargo cult programming :(
Thankfully that won't help here :) I knew a guy who handed in an assignment which was basically copied and pasted bits of our lecturer's sample code put together. He barely got a pass and the comment was something like:
Excellent quality code..
...
..If only you went beyond submitting the code I gave you in class.

So basically, the lecturer was commenting that his own code was of excellent quality! :lol:

I think it was him who was working on a physics demo (also made up of copy-and-pasted code he didn't know what it did) and wanted to make a block shoot out from the screen and I managed to convince him the code to do that was:

Code: Select all

throw new Block();
and he was wondering why it was crashing, so he called the lecturer over to look at his code :lol:

It's a pity that the fun ones don't last very long. Our programming class has dropped down to around 12 people now.
My OS is Perception.
User avatar
Omega
Member
Member
Posts: 250
Joined: Sun May 25, 2008 2:04 am
Location: United States
Contact:

Re: stuck converting a char to an integer

Post by Omega »

Hope this doesn't come too late, but I just needed an atoi myself, so here you go:

Code: Select all

int atoi(char* string)
{
	int i = 0;
	int value = 0;
	int zeroValue = '0';       
	const int slen = len(string);
	for (i = 0; i < slen; ++i)
	{
		char c = string[i];
		if (c < '0' || c > '9')
		{
			return -1;
		}
		value = 10 * value + (c - zeroValue);
	}
    return value;
}

Code: Select all

printf("Return = %d","1337");
Output: 1337

Hope you enjoy. :)
Free energy is indeed evil for it absorbs the light.
DeletedAccount
Member
Member
Posts: 566
Joined: Tue Jun 20, 2006 9:17 am

Re: stuck converting a char to an integer

Post by DeletedAccount »

Solar is correct , make use of the standard functions , Do not reinvent wheel unecesserily .
Post Reply