|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
#1 |
|
Member (10 bit)
|
C++ problem
First of all, i wish you guys could meet my teacher. I know some of you are very good programmers, but would your opinion that pointers and dynamic memory allocation are kind of complex topics that should be taught? he just gives us this assignment expecting that we know what to do... and i am stuck right now.
here is what i need to do: The program will read in a list of names from a file, print the list, sort the list and print it again. 1. to implement the problem, declare a 15 element array of type pointer to char. Declare an integer variable to keep track of the number of names that are read from the file. Also, declare an array of char of size 30 named tempName to store the most recently read name temporarily. 2.Open a data file named "names.dat". the file contains a list of names. in a loop which tests for end of file, read a name into tempName. Then dynamically allocate an array of the right size to hold the name and assign the address of the array to the next available array element in the pointer array. then copy the name from tempName into the dynamically allocated array. 3. print the list using a separate function. i keep getting a segmentation fault when i do this, for reasons beyond me, so hopefully someone can enlighten me. Thanks in advance ^dan |
|
|
|
|
|
#2 |
|
Professional gadfly
|
A segmentation fault usually occurs when you are reading past the end of an array. Check your code to make sure this isn't happening, and remember that arrays are zero-based, so the last element in an array of 15 elements is a[14].
If I have time later, I will take a look at your code. |
|
|
|
|
|
#3 |
|
Professional gadfly
|
Change this line by removing the asterisks:
*Names[numNames]=*ptrstring; ptrstring is a pointer, and Names stores char pointers, so there is no need to dereference anything. You want to directly store the pointer in Names. |
|
|
|
|
|
#4 |
|
Member (10 bit)
|
Ok i got that working and the names will print out in the main function, but i have no idea how to pass a pointer array. attached is my code and it compiles, but does not work.
thanks again ^dan |
|
|
|
|
|
#5 |
|
Professional gadfly
|
This corrected code works for me:
Code:
void Print(char* Names[],int);
int main()
{
char* Names[15]={0};
int numNames=0;
char tempName[30];
int size=0;
ifstream infile;
char* ptrstring;
infile.open("names.dat");
if(infile.fail()){
cerr << "Error: File not opened!" << endl;
exit(1);
}
while(!infile.eof()){
// get name from file
infile.getline(tempName, 30);
// get size of string
size=strlen(tempName);
// dynamically allocating array
ptrstring=new char[size];
// copy tempname to ptrstring
strcpy(ptrstring, tempName);
// assigning the next available element to the string
Names[numNames]=ptrstring;
numNames++;
}
Print(Names,numNames);
}
void Print(char* Names[],int numNames){
for(int i=0;i |
|
|
|
|
|
#6 |
|
Member (10 bit)
|
Ok, it didnt work for me, but i got it fixed a different way. Is there anything special need to sort a character array? I am trying to use a selection sort, but it isnt working. here is the code for the sort:
Code:
void Sort(char * Names[15], int numNames)
{
int i, j, min, minidx, temp;
for(i=0;i<(numNames-2);i++){
min=*Names[i];
minidx=i;
for(j=1;j<(numNames-1);j++){
if(*Names[j]thanks again ^dan |
|
|
|
|
|
#7 |
|
Member (10 bit)
|
i just had a thought... would using the strcmp() function be useful in this situation? I have to go to class now, but i will try to write the function in class and post it when i get out.
^dan |
|
|
|
|
|
#8 |
|
Professional gadfly
|
If the sort algorithm doesn't matter, here is one that will work. Because you are just switching around pointers, you only need to dereference on the strcmp:
Code:
void Sort(char* Names[], int numNames)
{
int i, j;
char* temp;
bool exchanges;
do {
exchanges = false; // assume no exchanges
for (int i=0; i
|
|
|
|
|
|
#9 |
|
Member (10 bit)
|
during class today i was looking at a bubble sort and came up with this, which i got to work:
Code:
void Sort(char * Names[15], int numNames)
{
int last, next, test;
char * temp;
for(int i=1;i
![]() ^dan |
|
|
|
|
|
#10 |
|
Professional gadfly
|
Yep, that looks pretty similar to what I had.
One comment on how you write things. When declaring functions and variables that use pointers, it makes it easier to read to put the asterisk next to the type, instead of next to the variable name. In C++, the lines "dim char* pChar", "dim char *pChar" and even "dim char * pChar" are all the same. However, "dim char* pChar" makes it very easy to see immediately that pChar is a pointer to a char, because they are right next to each other. |
|
|
|
|
|
#11 |
|
Member (10 bit)
|
i see what you mean. always looking for ways to improve, so thanks for the input
![]() ^dan |
|
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|