Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 03-28-2006, 02:19 PM   #1 (permalink)
Baez's Avatar
 

Join Date: Sep 2005

Location: Toronto, Canada

Posts: 5,484

Baez is a glorious beacon of lightBaez is a glorious beacon of lightBaez is a glorious beacon of lightBaez is a glorious beacon of lightBaez is a glorious beacon of light

Default C++ String Problem

Alright I'm having trouble with this school assignment. Im on the last step and cannot figure out why it isnt working so hopefully u can tell me whats wrong. This is the code:


void Account::getFirstName(char st[])
{
int i = 0;

strcpy(st, customer);
while (st[i] != ',')
{
i++;
}

i++;
while (st[i] != ',')
{
strcpy(st, customer);
i++;
}
st[i]='\0';
}

Ok let me explain. I have a string with this:

Smith,Ralph,Toronto,656-894-8574

So it goes through each letter to check if its a comma and if not keep going.

The purpose is to get rid of the last name and end chars and only keep the first name, which in this case would be Ralph without any commas. I have looked at this logic for a long time but cannot figure out where Im going wrong.

It looks so simple. But instead, I end up with Smith,Ralph left instead of only Ralph. But I don't know why it is still grabbing Smith with it.

Thanks for Your Help

Chris
__________________

Baez is online now  
Old 03-28-2006, 02:50 PM   #2 (permalink)
 
Ultra Techie

Join Date: Jul 2005

Posts: 530

TheHeadFL

Send a message via AIM to TheHeadFL
Default

You should not be using strcpy as this copies the entire string and not just a portion.
__________________
Desktop machine: 2 x Opteron 246, Asus K8N-DL, 2GB PC3200 ECC Reg., XFX GeForce 6600GT, 74gb WD Raptor, 2 x 19\" LCDs, Windows XP x64
Server machine: Intel P4 3.0GHz 2MB EM64T, ECS i865pe, 1GB PC3200, 36gb WD Raptor, Windows Server 2003
Laptop: Dell Inspiron 9100 (Intel P4 3.2GHz 1MB Prescott, i865pe, 512MB PC3200, Mobility Radeon 9700, DVD+R/DL Burner), Windows XP
Linux: P3 450Mhz, 386MB ram, Slackware 10.1 (Running mySQL/Apache)
TheHeadFL is offline  
Old 03-28-2006, 05:43 PM   #3 (permalink)
 
Software Developer

Join Date: Mar 2006

Location: Columbus, OH

Posts: 569

jaeusm is on a distinguished road

Default

Why not use the string class?
jaeusm is offline  
Old 03-28-2006, 05:59 PM   #4 (permalink)
 
Ultra Techie

Join Date: Jul 2005

Posts: 530

TheHeadFL

Send a message via AIM to TheHeadFL
Default

cause that would defeat the purpose of the assignment
__________________
Desktop machine: 2 x Opteron 246, Asus K8N-DL, 2GB PC3200 ECC Reg., XFX GeForce 6600GT, 74gb WD Raptor, 2 x 19\" LCDs, Windows XP x64
Server machine: Intel P4 3.0GHz 2MB EM64T, ECS i865pe, 1GB PC3200, 36gb WD Raptor, Windows Server 2003
Laptop: Dell Inspiron 9100 (Intel P4 3.2GHz 1MB Prescott, i865pe, 512MB PC3200, Mobility Radeon 9700, DVD+R/DL Burner), Windows XP
Linux: P3 450Mhz, 386MB ram, Slackware 10.1 (Running mySQL/Apache)
TheHeadFL is offline  
Old 03-29-2006, 01:59 AM   #5 (permalink)
Baez's Avatar
 

Join Date: Sep 2005

Location: Toronto, Canada

Posts: 5,484

Baez is a glorious beacon of lightBaez is a glorious beacon of lightBaez is a glorious beacon of lightBaez is a glorious beacon of lightBaez is a glorious beacon of light

Default

would sscanf work?
__________________

Baez is online now  
Old 03-29-2006, 08:31 AM   #6 (permalink)
Chankama's Avatar
 
Monster Techie

Join Date: Jan 2005

Location: Canada

Posts: 1,522

Chankama will become famous soon enough

Default

I think it's ok to use functions in the C library. Why not?.. If the assignment wanted an implementation of any of the fundamental string functions, it could've asked for them. Or it could've said "do not use any functions other than user defined functions"..

That being said, take a look at the strtok function. You'll be done in no time ..

http://www.cplusplus.com/ref/cstring/strtok.html

In fact, there is similar source code in the link I gave..
Chankama is offline  
Old 03-29-2006, 10:55 AM   #7 (permalink)
 
Ultra Techie

Join Date: Jul 2005

Posts: 530

TheHeadFL

Send a message via AIM to TheHeadFL
Default

string class != string functions
__________________
Desktop machine: 2 x Opteron 246, Asus K8N-DL, 2GB PC3200 ECC Reg., XFX GeForce 6600GT, 74gb WD Raptor, 2 x 19\" LCDs, Windows XP x64
Server machine: Intel P4 3.0GHz 2MB EM64T, ECS i865pe, 1GB PC3200, 36gb WD Raptor, Windows Server 2003
Laptop: Dell Inspiron 9100 (Intel P4 3.2GHz 1MB Prescott, i865pe, 512MB PC3200, Mobility Radeon 9700, DVD+R/DL Burner), Windows XP
Linux: P3 450Mhz, 386MB ram, Slackware 10.1 (Running mySQL/Apache)
TheHeadFL is offline  
Old 03-29-2006, 09:57 PM   #8 (permalink)
Chankama's Avatar
 
Monster Techie

Join Date: Jan 2005

Location: Canada

Posts: 1,522

Chankama will become famous soon enough

Default

Quote:
Originally posted by TheHeadFL
string class != string functions
Of course it isn't. But how is that applicable to this thread?....

Anyways, I just got home. Here's the code snippet you need..

Code:
char * getName(char * pStr)
{
    strtok(pStr, ",");
    return strtok(NULL,",");
}
Use it small function in whatever main() you have, and you should be good to go ..

Code:
#include <stdio.h>
#include <string.h>

char * getName(char * pStr);

int main ()
{
  char str[] ="Smith,Ralph,Toronto,656-894-8574";
  printf("\r\n%s\r\n", getName(str));
  return 0;
}

Chankama is offline  
Old 03-29-2006, 11:07 PM   #9 (permalink)
 
Ultra Techie

Join Date: Jul 2005

Posts: 530

TheHeadFL

Send a message via AIM to TheHeadFL
Default

Because the guy suggested he use the string classes which implies he would be using the standard template library string class.

Using the C libraries is not the same thing at all.
__________________
Desktop machine: 2 x Opteron 246, Asus K8N-DL, 2GB PC3200 ECC Reg., XFX GeForce 6600GT, 74gb WD Raptor, 2 x 19\" LCDs, Windows XP x64
Server machine: Intel P4 3.0GHz 2MB EM64T, ECS i865pe, 1GB PC3200, 36gb WD Raptor, Windows Server 2003
Laptop: Dell Inspiron 9100 (Intel P4 3.2GHz 1MB Prescott, i865pe, 512MB PC3200, Mobility Radeon 9700, DVD+R/DL Burner), Windows XP
Linux: P3 450Mhz, 386MB ram, Slackware 10.1 (Running mySQL/Apache)
TheHeadFL is offline  
Old 03-29-2006, 11:58 PM   #10 (permalink)
Chankama's Avatar
 
Monster Techie

Join Date: Jan 2005

Location: Canada

Posts: 1,522

Chankama will become famous soon enough

Default

Think it was another guy talking about the string class.. cvb724 seems to be asking about the C libraries since he was talking about sscanf and such. And he does have a character array.

Of course, his "function" definition is still in C++ form..........
Chankama is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On