|  | |
03-28-2006, 02:19 PM
|
#1 (permalink)
|
Join Date: Sep 2005 Location: Toronto, Canada Posts: 5,484
| 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 |
| |
03-28-2006, 02:50 PM
|
#2 (permalink)
|
Ultra Techie Join Date: Jul 2005 Posts: 530
| 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) |
| |
03-28-2006, 05:43 PM
|
#3 (permalink)
|
Software Developer Join Date: Mar 2006 Location: Columbus, OH Posts: 569
| Why not use the string class? |
| |
03-28-2006, 05:59 PM
|
#4 (permalink)
|
Ultra Techie Join Date: Jul 2005 Posts: 530
| 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) |
| |
03-29-2006, 01:59 AM
|
#5 (permalink)
|
Join Date: Sep 2005 Location: Toronto, Canada Posts: 5,484
| would sscanf work? |
| |
03-29-2006, 08:31 AM
|
#6 (permalink)
|
Monster Techie Join Date: Jan 2005 Location: Canada Posts: 1,522
| 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.. |
| |
03-29-2006, 10:55 AM
|
#7 (permalink)
|
Ultra Techie Join Date: Jul 2005 Posts: 530
| 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) |
| |
03-29-2006, 09:57 PM
|
#8 (permalink)
|
Monster Techie Join Date: Jan 2005 Location: Canada Posts: 1,522
| 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;
}
|
| |
03-29-2006, 11:07 PM
|
#9 (permalink)
|
Ultra Techie Join Date: Jul 2005 Posts: 530
| 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) |
| |
03-29-2006, 11:58 PM
|
#10 (permalink)
|
Monster Techie Join Date: Jan 2005 Location: Canada Posts: 1,522
| 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.......... |
| |  | | | Thread Tools | | | | Display Modes | Linear Mode |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | |