| Pages: [1] :: one page |
| Author |
Thread Statistics | Show CCP posts - 0 post(s) |

alesta
|
Posted - 2007.03.19 21:08:00 -
[1]
its me again....this time this is of no importance we were learnign strings today....old C string, char pointers, and the new C strings.....neways he had us write a test rpogram...showing how strcat works......so i wrote my program as usual except it didnt work.....so i askedh im for help....and after 30 minutes...we couldnt figure out why it wasnt working.... neways i turn to u...this is of no importance im just trying to solve why it isnt working....and also still wanted to see the hands on exp of how it works....
#include <iostream> #include <string.h> using namespace std;
void main()
{
char fname[50]; char lname[50]; char space[1]={ ' ' }; char wholename[100];
cout << "enter your first name" << endl; cin >> fname; cout << fname; cout << "now enter your last name" << endl; cin >> lname; cout << endl;
strcat(wholename, fname); strcat(wholename, space); strcat(wholename, lname);
cout << wholename;
}
he had me print it so he could give it to some of his coworkers.......im hoping otbeat him in figueing it out hehe
the problem is...with the last cout....it displays nothing....
|

Darth Vroevl
Base Delta Zero
|
Posted - 2007.03.19 21:18:00 -
[2]
Edited by: Darth Vroevl on 19/03/2007 21:17:51 I'd venture a guess that it has something to do with wholename not being NULL terminated.
Edit: Confirmed. The lack of NULL is screwing with strcat. Also, space should have size 2 to account for its own NULL:
#include <iostream> #include <string.h> using namespace std;
void main() {
char fname[50]; char lname[50]; char space[2]={ ' ' }; char wholename[100];
wholename[0] = NULL;
cout << "enter your first name" << endl; cin >> fname; cout << fname; cout << "now enter your last name" << endl; cin >> lname; cout << endl;
strcat(wholename, fname); strcat(wholename, space); strcat(wholename, lname);
cout << wholename; } |

Dark Shikari
Caldari Imperium Technologies Firmus Ixion
|
Posted - 2007.03.19 21:20:00 -
[3]
Originally by: Darth Vroevl I'd venture a guess that it has something to do with wholename not being NULL terminated.
Isn't that done automatically by the compiler though?
--23 Member--
EVE-Trance Radio--The EVE Textboard |

Darth Vroevl
Base Delta Zero
|
Posted - 2007.03.19 21:23:00 -
[4]
Edited by: Darth Vroevl on 19/03/2007 21:20:50
Originally by: Dark Shikari
Originally by: Darth Vroevl I'd venture a guess that it has something to do with wholename not being NULL terminated.
Isn't that done automatically by the compiler though?
Should be in release mode, but isn't in debug. Edit: In Visual C++ anyway. |

Jenny Spitfire
Caldari Requiem of Hades Xelas Alliance
|
Posted - 2007.03.19 21:59:00 -
[5]
Edited by: Jenny Spitfire on 19/03/2007 22:10:50
Be careful with C and C++ strings. C++ string initialises. C string doesn't.
#include <iostream> #include <string.h> using namespace std;
void main() { string fname; string lname; string space = " "; char wholename[100];
cout << "enter your first name" << endl; cin >> fname; cout << fname; cout << "now enter your last name" << endl; cin >> lname; cout << endl;
strcat(wholename, fname.c_str()); strcat(wholename, space.c_str()); strcat(wholename, lname.c_str());
cout << wholename;
}
Another one. You need to initialise memory before you can use them.
#include <iostream> #include <string.h> using namespace std;
void main() {
char fname[50]=""; char lname[50]=""; char space[2]=" "; char wholename[100]="";
cout << "enter your first name" << endl; cin >> fname; cout << fname; cout << "now enter your last name" << endl; cin >> lname; cout << endl;
strcat(wholename, fname); strcat(wholename, space); strcat(wholename, lname);
cout << wholename;
}
Another one. Initialise wholename by strcpy.
#include <iostream> #include <string.h> using namespace std;
void main() {
char fname[50]=""; char lname[50]=""; char space[2]=" "; char wholename[100]="";
cout << "enter your first name" << endl; cin >> fname; cout << fname; cout << "now enter your last name" << endl; cin >> lname; cout << endl;
strcpy(wholename, fname); strcat(wholename, space); strcat(wholename, lname);
cout << wholename;
}
--------- Technica impendi Caldari generis. Pax Caldaria!
Kali is for KArebearLIng. I 100% agree with Avon.
Female EVE gamers? Mail Zajo or visit WGOE.Public in-game. |

Parthelon
The Pikey Rebellion
|
Posted - 2007.03.19 22:38:00 -
[6]
Quote: char fname[50]; char lname[50]; char space[1]={ ' ' }; char wholename[100];
Quote: strcat(wholename, fname); strcat(wholename, space); strcat(wholename, lname);
I don't know c or c++ but the thing that stands out to me is that your sticking 101 characters into a array of length 100.
|

alesta
|
Posted - 2007.03.20 01:41:00 -
[7]
Originally by: Parthelon
Quote: char fname[50]; char lname[50]; char space[1]={ ' ' }; char wholename[100];
Quote: strcat(wholename, fname); strcat(wholename, space); strcat(wholename, lname);
I don't know c or c++ but the thing that stands out to me is that your sticking 101 characters into a array of length 100.
it looks that way but its not..... the max length is 100 chars.... but strcat only copies to the first "\0" terminator null....so if fname becames [d][a][v][e][\0][][][][][]....... it only deals with dave instead of the 46 other slots...making the size 100 is to compensate for extremely ridcoulsy long names that people can have
to the others i will try ur methods and get back to you
|

alesta
|
Posted - 2007.03.20 02:03:00 -
[8]
thnx to Darth Vroevl ur solution worked great as i told my instructor u can tell me how to do it and i can read a book.....but at the end of the day unless i do it myself ill never learn it hehe sorry to others i didnt try yours :/ but theres more to come soon
|

Hllaxiu
Shiva Morsus Mihi
|
Posted - 2007.03.20 05:54:00 -
[9]
Just a general tip when you're doing programming: Try not to mix the C and C++ string and io handling. You'll only get a headache. --- Our greatest glory is not in never failing, but in rising up every time we fail. - Emerson |

alesta
|
Posted - 2007.03.20 14:11:00 -
[10]
Originally by: Hllaxiu Just a general tip when you're doing programming: Try not to mix the C and C++ string and io handling. You'll only get a headache.
i understand that this was jsut an exercise form my professor showing us how old c strings wroked.....after that he said dont use them use the new strings...he just wanted us o show how it worked...because in the end it can be useful 1 day if u have to use an olderversion for a client for some odd purpose wich he stated he has to do sometimes
|
| |
|
| Pages: [1] :: one page |
| First page | Previous page | Next page | Last page |