| | |
What's wrong with this strcpy and this pointer
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
•
•
That's not true, character arrays can be handled equally by either language.
My point was that it's like this. Say you want to input a string using getline():
C++ Syntax (Toggle Plain Text)
#include <string> std::string line; getline(cin, line);
C++ Syntax (Toggle Plain Text)
#include <cstring> char name[20]; strcpy(name, line.c_str()); // should error check this
"Technological progress is like an axe in the hands of a pathological criminal."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
•
•
•
•
If you're going to use char arrays, you might as well be using C. And no, I know it's not your fault, so I blame your teacher for that.
earlyriser, stop using any and all functions that have a definition of string and use only char* functions. Help us save Joe's sanity.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
He's already said his instructor says he must use char arrays. He should be writing a pure C program, not a C++ program basterdised with C. If that is what his instructor is teaching then the instructor needs to take a few more programming courses.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Hi. In fact the Homework is in C++.
Maybe the error is mine (I dont want to embarras my teacher's reputation).
The structs provided in the HW are below, with the chnages I did because I wnated to reduce the difficulty and have less pointers. Maybe that was the thing that brought the insanity.
By the way the point of the HW are classes and not the string manipulation
Maybe the error is mine (I dont want to embarras my teacher's reputation).
The structs provided in the HW are below, with the chnages I did because I wnated to reduce the difficulty and have less pointers. Maybe that was the thing that brought the insanity.
struct Professeur{ char* nom; // changed to char nom[20] char* prenom; Cours* listecours; Etudiants* listetudiants; Professeur* suivant; }; struct Cours { char * sigle; // changed to char sigle[10] Cours * suivant; }; struct Etudiant { char* nom; //changed to char nom[20] Etudiant* apres; };
By the way the point of the HW are classes and not the string manipulation
>If that is what his instructor is teaching then the instructor needs to take a few more programming courses.
Not really, like Mr disney said it's nice to know about char[] style strings. In fact you'll find a lot of institutions teach kids c++ this way.
Will they come out knowing true c++? Nup, but there ya go. :lol:
Not really, like Mr disney said it's nice to know about char[] style strings. In fact you'll find a lot of institutions teach kids c++ this way.
Will they come out knowing true c++? Nup, but there ya go. :lol:
*Voted best profile in the world*
>>By the way the point of the HW are classes
Well, so much for your teacher's reputation, it just went into the gutter :eek: But we should be discussing your HW, not your teacher.
Had you left the pointers there it would have made your program just a small tad more complex but a whole lot safer because you could have allocated memory of the correct size for the strings, something like this:
Don't forget to delete[] to memory when done with it.
Well, so much for your teacher's reputation, it just went into the gutter :eek: But we should be discussing your HW, not your teacher.
Had you left the pointers there it would have made your program just a small tad more complex but a whole lot safer because you could have allocated memory of the correct size for the strings, something like this:
c Syntax (Toggle Plain Text)
PCourant->nom = new char[line.size()+1]; strcpy (PCourant->nom, line.c_str());
Don't forget to delete[] to memory when done with it.
Last edited by Ancient Dragon; Feb 19th, 2007 at 4:56 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Thanks for all your tips. I will close this thread, because I have started another one with a less specific topic:
http://www.daniweb.com/techtalkforum...tml#post317987
http://www.daniweb.com/techtalkforum...tml#post317987
•
•
•
•
After reading a line in a text file I need to read another line which contains an INT. I thought that the bold line in this code will be ok to retreive the data, but it doesnt.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <string> #include <cstdlib> using namespace std; struct Professeur { char nom[20]; int ancien; }; class DossierProfesseur { public: Professeur *tete; DossierProfesseur(); ~DossierProfesseur(); }; DossierProfesseur::DossierProfesseur() { Professeur *PCourant, *PTete=NULL; string line; ifstream myfile ("FP.txt"); if (myfile.is_open()) { while (! myfile.eof() ) { getline (myfile,line); PCourant = new Professeur; strcpy (PCourant->nom, line.c_str()); cout << PCourant->nom; [B]getline (myfile, PCourant->ancien);[/B] } myfile.close(); } else cout << "Unable to open file"; } void main () { DossierProfesseur *dossierProfesseur; dossierProfesseur= new DossierProfesseur; }
c Syntax (Toggle Plain Text)
PCourant = (struct Professeur*)malloc(sizeof(struct Professeur));
Or you can define a macro something similar to new such as:
c Syntax (Toggle Plain Text)
#define Professor Professeur #define new(x) (x*)malloc(sizeof(x)) PCourant = new(Professor);
c Syntax (Toggle Plain Text)
#define MAX_LINE 100 char line[MAX_LINE];
This is just an alternative way, but I guess it boils down to the specifications of your assignment.
Good luck, Lamabot
Last edited by Lazaro Claiborn; Feb 20th, 2007 at 8:14 pm.
•
•
•
•
Being the nerd I am, I'll give my opinion on this particular issue. If you're going to use a structure data type, I'd just use malloc instead of new to allocate memory on the heap:
c Syntax (Toggle Plain Text)
PCourant = (struct Professeur*)malloc(sizeof(struct Professeur));
This is a C++ program. So then use C++ memory allocation methods. The OP's method of using
new was fine. (Aside: 2 things the OP did do wrong; didn't use delete and there's a memory leak in the loop as I mentioned previously.)•
•
•
•
And for the string variable line, you might as well use a character array of say 100 characters or etc:
c Syntax (Toggle Plain Text)
#define MAX_LINE 100 char line[MAX_LINE];
And while I'm critcizing people's code (sorry, I can't help it!), trash that void main(). It's not standard, nor is it good practice.
http://users.aber.ac.uk/auj/voidmain.shtml
"Technological progress is like an axe in the hands of a pathological criminal."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
![]() |
Similar Threads
- problems with pointers (C++)
- help!! about ADT list please (C)
- Errors help (C++)
- total newb - "passing arg 2 of `strcpy' makes pointer from integer without a cast" (C++)
- Array troubles? (C++)
Other Threads in the C++ Forum
- Previous Thread: IO design problem
- Next Thread: Need some help on my program!!!!!!
Views: 5569 | Replies: 26
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






