|
||||||
| C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
Hi.
Im trying to do a Class that does several functions that emulate string.h library. I did fine the first three, but im having trouble on the fourth (CString::tolower()). Apparently the program is skipping that instruction when i run it, and i have no idea why. Can you please help me? thanks for your time: Code:
#include <iostream>
#include <string.h>
#include <conio.h>
#include <stdlib.h>
using namespace std;
class CString{
private:
char *pstr;
int nchar;
public:
CString( char * = "" ); // constructor predeterminado
CString( const CString & ); // constructor de copia
void asigna(char *);
char *tochar();
int lenght();
void tolower();
void toupper();
};
CString::CString( char *c ){
this->nchar = strlen(c);
this->pstr = new char[this->nchar + 1];
strcpy( this->pstr, c );
}
CString::CString( const CString &arg ){
this->nchar = arg.nchar;
this->pstr = new char[nchar + 1];
strcpy( pstr, arg.pstr );
}
void CString::asigna(char *vec){
int i;
nchar=strlen(vec);
pstr=new char[nchar];
for(i=0; i<nchar; i++){
pstr[i]=vec[i];
}
pstr[i]='\0';
}
char * CString::tochar(){
return pstr;
}
int CString::lenght(){
return nchar;
}
void CString::tolower(){
int a, i;
cout<<pstr<<endl;
for(i=0; i<nchar; i++){
a=atoi(&pstr[i]);
cout<<pstr[i];
cout<<a<<endl;
getch();
if((a>=65)&&(a<=90)){
a=a+22;
}
pstr[i]=a;
}
}
int main(){
int x=1;
char b;
CString a;
a.asigna("HoLa");
cout<<a.tochar()<<endl;
cout<<a.lenght();
a.tolower();
getch();
return 0;
}
|
| Sponsored Links |
|
|
|
|||
|
There are several things wrong with this code. For starters, you misspelled length throughout the entire program and you used the variable a for two different things.
Functionally, I don't think that any of the script works, but since you asked me to look at the tolower... I'm not entirely sure what you are trying to do here since you are outputting values. I am going to assume that you are trying to create a function that will make all letter in the given CString lower-case. This means, don't cout anything and don't use getch(). Again, don't reuse a. You don't need to use atoi here, because that is only used when you are converting digits 0-9 into their appropriate number equivalent; to do what you are trying to do, you can do math with the characters as is. Also, in the same line as the atoi, you write &pstr[i], which is nothing, you don't need the ampersand here, which is why your function was outputting a lot of zeros. Next, you say plus 22, it should be plus 32. The fact that the alphabet alone has over 22 letters should have been a signal here. The use of a in this function is redundant since you say a=pstr[i] only to later say pstr[i]=a. Here's how I rewrote it: Code:
void CString::tolower(void){
int i;
for(i=0; i<nchar; i++){
if(65<=pstr[i] && pstr[i]<=90)
pstr[i]+=32;
}
}
If you execute this code you should see how it works: Code:
b.tochar(); b.tolower(); b.tochar(); Hope it all helps, T.J. Gaffney |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Peculiar UI Problem Needs Tackling | adriyel | C# Programming | 2 | 04-06-2008 08:46 AM |
| i have a problem please help me!!!???? | stack | Java Help | 8 | 09-22-2007 04:17 PM |
| [C] Comparison problem | Alhazred | C and C++ | 1 | 08-29-2007 05:58 AM |
| Issue writing to file: pointer to a class which contains pointers to other classes | Sheemer | C and C++ | 0 | 08-21-2007 02:17 AM |
| A small problem in the output | The_Master | C and C++ | 3 | 12-13-2006 01:04 PM |
| WingedPanther | ........ | 2753.6 |
| Xav | ........ | 2704 |
| Brandon W | ........ | 1702.32 |
| John | ........ | 1207.73 |
| marwex89 | ........ | 1175.24 |
| morefood2001 | ........ | 966.05 |
| dcs | ........ | 655.75 |
| Steve.L | ........ | 475.59 |
| orjan | ........ | 418.58 |
| Aereshaa | ........ | 383.54 |