Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming

Reply
 
LinkBack Thread Tools
Old 06-30-05   #11 (permalink)
PC Gamer
 
kris_cs1's Avatar
 
amd ati

Join Date: Apr 2005
Location: Chester, Cheshire
Posts: 218

Rep: 27 kris_cs1 is acknowledged by some
Unique Rep: 24
FAQs Submitted: 1
Trader Rating: 0
Default

Quote:
Originally Posted by Melraidin
If I have time tonight I may be able to get back into some C++ coding and post some of my code cleaned up a bit, but at the moment a bit busy. Seeing C++ again is really reminding me why I like C# so much! ie: string.ToUpper ()
LOL, that sounds so easy: string.ToUpper ()
That is why I'm trying to make my own functions you see. It is a bit of practise and they will make my life a lot easier in my future programs.

Xaimus, thanks. Is that meant to chop off characters from the beginning and end because I can't get it to work hmmm I'm not very good am I?

Anyone know what i'm doing wrong in my functions yet and how to fix them? Well... I know it is to do with the pointers.... i think... but I really don't know what to do with it to make it work. I'm tring to use the same arguments as the functions in Liberty Basic

__________________
Kris



System: My System
CPU
AMD Sempron 2800+
Motherboard
ASUS A7N8X-VM
Sound Card
5.1 SURROUND SOUND
Power Supply
350W
Case
Black
OS
Windows XP Home SP2
Monitor
19"
kris_cs1 is offline   Reply With Quote
Old 06-30-05   #12 (permalink)
<3 TB303
 
Xaimus's Avatar
 
amd nvidia

Join Date: Dec 2004
Location: Manhattan, KS
Posts: 575

Trader Rating: 0
Default

Quote:
Originally Posted by kris_cs1
Xaimus, thanks. Is that meant to chop off characters from the beginning and end because I can't get it to work :( hmmm I'm not very good am I?
Y'know, I'm not quite sure anymore. :|

It's supposed to shift around the characters inside the string and then seal off the appropriate area with a NULL to remove certain areas of the string, but I can not test it here. bleh, work
__________________
System: Turd
CPU
Sempron 2800+ :(
Motherboard
GA7NF-RZ :(
Memory
1.25GB
Graphics Card
geForce 6800XT
Hard Drive
80GB + 120GB
Sound Card
M-Audio Audiophile 192 :D
Power Supply
500W Seasonic S12
Case
Black Antec P180 :D
OS
FreeBSD 6.2-RELEASE :D :D
Monitor
Two 17" CRTs
Xaimus is offline Overclocked Account   Reply With Quote
Old 06-30-05   #13 (permalink)
Overclocker
 
Join Date: Dec 2004
Posts: 401

Rep: 65 Melraidin is acknowledged by some
Unique Rep: 48
FAQs Submitted: 4
Folding Team Rank: 264
Trader Rating: 1
Default

Quote:
Originally Posted by kris_cs1
LOL, that sounds so easy: string.ToUpper ()
That is why I'm trying to make my own functions you see. It is a bit of practise and they will make my life a lot easier in my future programs.

Xaimus, thanks. Is that meant to chop off characters from the beginning and end because I can't get it to work hmmm I'm not very good am I?

Anyone know what i'm doing wrong in my functions yet and how to fix them? Well... I know it is to do with the pointers.... i think... but I really don't know what to do with it to make it work. I'm tring to use the same arguments as the functions in Liberty Basic

Quote:
Code:
char* Mid(constchar *Sentence, int FromNumber, int ToNumber)
{
char *newSentence;
if (ToNumber == 0)
ToNumber = KrisIO::Len(Sentence);
for (int i = (FromNumber--); i <= ToNumber; i++)
{
char *newSentence;
newSentence += Sentence[i];
}
return newSentence;
}
Can't go playing with this right now, but a few things:

- You haven't allocated any memory for newSentence to point to, use malloc and free to allocate and deallocate the memory. Also, you may want to consider other options for returning the value, as currently your calling code will have to free the returned memory on its own or it will lead to a memory leak.

- You shouldn't be using the += operator to copy the character to the new string, instead simply use the = operator. Otherwise you're relying on the compiler to initialize the memory pointed to by newSentence to 0, not terribly robust.

- Don't redeclare newSentence inside the for loop, it's already been declared outside the loop.

Hopefully this will get you on the road to success.
Melraidin is offline I fold for Overclock.net   Reply With Quote
Old 06-30-05   #14 (permalink)
PC Gamer
 
kris_cs1's Avatar
 
amd ati

Join Date: Apr 2005
Location: Chester, Cheshire
Posts: 218

Rep: 27 kris_cs1 is acknowledged by some
Unique Rep: 24
FAQs Submitted: 1
Trader Rating: 0
Default

Code:
 
char* Mid(char *Sentence, int FromNumber, int ToNumber) 
{ 
char* newSentence = (char *) malloc(1000); 
if (ToNumber == 0) 
ToNumber = KrisIO::Len(Sentence); 
for (int i = (FromNumber--); i <= ToNumber; i++) 
{ 
*(newSentence+(i-FromNumber)) = *(Sentence+i); 
} 
return newSentence; 
}
 
Hey cool! This works!! Thanks a lot. well..... if gets the letters but it also includes a lot of
══════════ before and after the string. It is also not very efficient and probably uses up lots of memory. Where would I free the memory? Any help on clearing the string of ══════════ ?
Thanks, I'm going to work now but keep posting!!
Still got the Upper() function to do after this lol.
__________________
Kris



System: My System
CPU
AMD Sempron 2800+
Motherboard
ASUS A7N8X-VM
Sound Card
5.1 SURROUND SOUND
Power Supply
350W
Case
Black
OS
Windows XP Home SP2
Monitor
19"
kris_cs1 is offline   Reply With Quote
Old 06-30-05   #15 (permalink)
Programmer
 
MrSmiley's Avatar
 
amd nvidia

Join Date: Oct 2004
Location: Manhattan, Kansas
Posts: 1,060

Rep: 72 MrSmiley is acknowledged by some
Unique Rep: 57
FAQs Submitted: 1
Folding Team Rank: 817
Hardware Reviews: 1
Trader Rating: 0
Default

Free the variable before the return.
on upper casing a whole array of characters it'd be nothing more than...
Code:
  char i[80],b[80];
  for(int n=0; i!='NULL'; n++)
  	 b[n]=toupper(i[n]);
for a whole string
Code:
  string blue, you;
  you = toupper(blue);
Header File: ctype.h and stdlib.h
__________________
System: My System
CPU
Intel 2.8GHz
Motherboard
ESG smth
Power Supply
Antec TruePower 350Watt
OS
FreeBSD
MrSmiley is offline I fold for Overclock.net   Reply With Quote
Old 07-01-05   #16 (permalink)
PC Gamer
 
kris_cs1's Avatar
 
amd ati

Join Date: Apr 2005
Location: Chester, Cheshire
Posts: 218

Rep: 27 kris_cs1 is acknowledged by some
Unique Rep: 24
FAQs Submitted: 1
Trader Rating: 0
Default

Quote:
Originally Posted by MrSmiley
Free the variable before the return.
on upper casing a whole array of characters it'd be nothing more than...
Code:
char i[80],b[80];
for(int n=0; i!='NULL'; n++)
	 b[n]=toupper(i[n]);
for a whole string
Code:
string blue, you;
you = toupper(blue);
Header File: ctype.h and stdlib.h
Thank you for your time.

Umm.. the first one says:
Quote:
'!=' : no conversion from 'int' to 'char *'
'char [80]' differs in levels of indirection from 'int'


And the second one says says:
Quote:
cannot convert parameter 1 from 'std::string' to 'int'


Will freeing the memory get rid of all of the ========='s in my Mid() function?

Sorry, I'm probably getting annoying now


__________________
Kris



System: My System
CPU
AMD Sempron 2800+
Motherboard
ASUS A7N8X-VM
Sound Card
5.1 SURROUND SOUND
Power Supply
350W
Case
Black
OS
Windows XP Home SP2
Monitor
19"
kris_cs1 is offline   Reply With Quote
Old 07-01-05   #17 (permalink)
Programmer
 
MrSmiley's Avatar
 
amd nvidia

Join Date: Oct 2004
Location: Manhattan, Kansas
Posts: 1,060

Rep: 72 MrSmiley is acknowledged by some
Unique Rep: 57
FAQs Submitted: 1
Folding Team Rank: 817
Hardware Reviews: 1
Trader Rating: 0
Default

Quote:
Originally Posted by kris_cs1
Thank you for your time.

Umm.. the first one says:
[/size][/size]

And the second one says says:


Will freeing the memory get rid of all of the ========='s in my Mid() function?

Sorry, I'm probably getting annoying now


[/size][/size]
sorry try for( int n=0; i[n]!='\0'; n++) //Just use the escape seq. instead.
__________________
System: My System
CPU
Intel 2.8GHz
Motherboard
ESG smth
Power Supply
Antec TruePower 350Watt
OS
FreeBSD
MrSmiley is offline I fold for Overclock.net   Reply With Quote
Old 07-01-05   #18 (permalink)
PC Gamer
 
kris_cs1's Avatar
 
amd ati

Join Date: Apr 2005
Location: Chester, Cheshire
Posts: 218

Rep: 27 kris_cs1 is acknowledged by some
Unique Rep: 24
FAQs Submitted: 1
Trader Rating: 0
Default

Quote:
Originally Posted by MrSmiley
sorry try for( int n=0; i[n]!='\0'; n++) //Just use the escape seq. instead.
It's better :P

If i assign "hello" to the variable i, the result is:

HELLO╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠ ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠hello

__________________
Kris



System: My System
CPU
AMD Sempron 2800+
Motherboard
ASUS A7N8X-VM
Sound Card
5.1 SURROUND SOUND
Power Supply
350W
Case
Black
OS
Windows XP Home SP2
Monitor
19"
kris_cs1 is offline   Reply With Quote
Old 07-01-05   #19 (permalink)
Overclocker
 
Join Date: Dec 2004
Posts: 401

Rep: 65 Melraidin is acknowledged by some
Unique Rep: 48
FAQs Submitted: 4
Folding Team Rank: 264
Trader Rating: 1
Default

Quote:
Originally Posted by kris_cs1
It's better :P

If i assign "hello" to the variable i, the result is:

HELLO╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠ ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠hello

First problem, do not free that memory at the end of the function. If you were to do so you would not be able to access it outside of the function. Instead, as you've written it, the only code that can free that memory safely is the calling code.

Second, the string of ='s or other symbols is probably put there by your compiler in debug mode. You're not putting a null terminator on the end of the new string you create. Before you return the new string try putting something along the lines of:

*( newSentence + ( toNumber - fromNumber ) ) = 0;
Melraidin is offline I fold for Overclock.net   Reply With Quote
Old 07-01-05   #20 (permalink)
PC Gamer
 
kris_cs1's Avatar
 
amd ati

Join Date: Apr 2005
Location: Chester, Cheshire
Posts: 218

Rep: 27 kris_cs1 is acknowledged by some
Unique Rep: 24
FAQs Submitted: 1
Trader Rating: 0
Default

Quote:
Originally Posted by Melraidin
First problem, do not free that memory at the end of the function. If you were to do so you would not be able to access it outside of the function. Instead, as you've written it, the only code that can free that memory safely is the calling code.

Second, the string of ='s or other symbols is probably put there by your compiler in debug mode. You're not putting a null terminator on the end of the new string you create. Before you return the new string try putting something along the lines of:

*( newSentence + ( toNumber - fromNumber ) ) = 0;
Dam! I got it to work perfectly using ^^^ but now when i tried to convert it to a function that I can use I can't get it to work It says: '╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠©ÛU╠╠╠╠╠╠╠╠╠╠╠ ╠╠╠╠╠╠╠╠< ╠╠╠╠╠╠╠╠'

PHP Code:
charChangeCase(char Sentence[80], char Case)
{
char newSentence[80];
for( 
int n=0Sentence[n]!='\0'n++)
{
switch (Case)
{
case 
'u':
newSentence[n]=toupper(Sentence[n]);
break;
case 
'l':
newSentence[n]=tolower(Sentence[n]);
break;
default:
newSentence[n] = Sentence[n];
};
}
*( 
newSentence KrisIO::Len(Sentence)) = 0;
return 
newSentence;

Also you say the first problem is that i shouldn't free the memory at the end of the function... have i actually done that because i don't know how to lol

Plus, thanks to this code from you:
Quote:
*( newSentence + ( toNumber - fromNumber ) ) = 0;
My Mid() function also neary works. There is just a preceding = sign I need to get rid of from the result somehow and I need to free the memory somehow.

It's all going well
Thank you
__________________
Kris



System: My System
CPU
AMD Sempron 2800+
Motherboard
ASUS A7N8X-VM
Sound Card
5.1 SURROUND SOUND
Power Supply
350W
Case
Black
OS
Windows XP Home SP2
Monitor
19"
kris_cs1 is offline   Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools



All times are GMT -4. The time now is 03:18 AM.


Overclock.net is a Carbon Neutral Site Creative Commons License Internet Security By ControlScan

Terms of Service / Forum Rules | Privacy Policy | Advertising | Become an Official Vendor
Copyright © 2008 Shogun Interactive Development. Most rights reserved.
Page generated in 0.38061 seconds with 8 queries