New Posts  All Forums:Forum Nav:

Lots of C++ Q's - Page 2

post #11 of 24
Thread Starter 
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

post #12 of 24
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
post #13 of 24
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.
post #14 of 24
Thread Starter 
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.
post #15 of 24
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
My System
(13 items)
 
  
CPUMotherboardOSPower
Intel 2.8GHz ESG smth FreeBSD Antec TruePower 350Watt 
  hide details  
Reply
My System
(13 items)
 
  
CPUMotherboardOSPower
Intel 2.8GHz ESG smth FreeBSD Antec TruePower 350Watt 
  hide details  
Reply
post #16 of 24
Thread Starter 
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


post #17 of 24
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.
My System
(13 items)
 
  
CPUMotherboardOSPower
Intel 2.8GHz ESG smth FreeBSD Antec TruePower 350Watt 
  hide details  
Reply
My System
(13 items)
 
  
CPUMotherboardOSPower
Intel 2.8GHz ESG smth FreeBSD Antec TruePower 350Watt 
  hide details  
Reply
post #18 of 24
Thread Starter 
Quote:
Originally Posted by MrSmiley
sorry try for( int n=0; i[n]!='\\0'; n++) //Just use the escape seq. instead.
It's better

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

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

post #19 of 24
Quote:
Originally Posted by kris_cs1
It's better

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;
post #20 of 24
Thread Starter 
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╠╠╠╠╠╠╠╠╠╠╠ ╠╠╠╠╠╠╠╠< ╠╠╠╠╠╠╠╠'

Code:

char* ChangeCase(char Sentence[80], char Case)
{
char newSentence[80];
for( int n=0; Sentence[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
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming