Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 04-30-2003, 08:22 PM   #1
Member (10 bit)
 
GSXdan's Avatar
 
Join Date: Jun 2002
Location: P-burg, Ohio, USA
Posts: 555
Send a message via AIM to GSXdan
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
Attached Files
File Type: zip lab7.zip (696 Bytes, 26 views)
GSXdan is offline   Reply With Quote
Old 05-01-2003, 08:13 AM   #2
Professional gadfly
 
doctorgonzo's Avatar
 
Join Date: Jan 2002
Location: Minneapolis, MN
Posts: 6,364
Send a message via MSN to doctorgonzo
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.
doctorgonzo is offline   Reply With Quote
Old 05-01-2003, 10:05 AM   #3
Professional gadfly
 
doctorgonzo's Avatar
 
Join Date: Jan 2002
Location: Minneapolis, MN
Posts: 6,364
Send a message via MSN to doctorgonzo
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.
doctorgonzo is offline   Reply With Quote
Old 05-01-2003, 12:51 PM   #4
Member (10 bit)
 
GSXdan's Avatar
 
Join Date: Jun 2002
Location: P-burg, Ohio, USA
Posts: 555
Send a message via AIM to GSXdan
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
Attached Files
File Type: zip lab7.zip (578 Bytes, 31 views)
GSXdan is offline   Reply With Quote
Old 05-01-2003, 02:16 PM   #5
Professional gadfly
 
doctorgonzo's Avatar
 
Join Date: Jan 2002
Location: Minneapolis, MN
Posts: 6,364
Send a message via MSN to doctorgonzo
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
doctorgonzo is offline   Reply With Quote
Old 05-01-2003, 02:36 PM   #6
Member (10 bit)
 
GSXdan's Avatar
 
Join Date: Jun 2002
Location: P-burg, Ohio, USA
Posts: 555
Send a message via AIM to GSXdan
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]
what it is doing right now is just screwing up the names, as you will see in the output. is there a better method to use or does that code just need some tweaking?

thanks again ^dan
GSXdan is offline   Reply With Quote
Old 05-01-2003, 02:44 PM   #7
Member (10 bit)
 
GSXdan's Avatar
 
Join Date: Jun 2002
Location: P-burg, Ohio, USA
Posts: 555
Send a message via AIM to GSXdan
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
GSXdan is offline   Reply With Quote
Old 05-01-2003, 03:14 PM   #8
Professional gadfly
 
doctorgonzo's Avatar
 
Join Date: Jan 2002
Location: Minneapolis, MN
Posts: 6,364
Send a message via MSN to doctorgonzo
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; i0) {
             	temp = Names[i]; Names[i] = Names[i+1]; Names[i+1] = temp;
             	exchanges = true;  // after exchange, must look again
             	}
           	}
       	} while (exchanges);
        return;
}
doctorgonzo is offline   Reply With Quote
Old 05-01-2003, 07:30 PM   #9
Member (10 bit)
 
GSXdan's Avatar
 
Join Date: Jun 2002
Location: P-burg, Ohio, USA
Posts: 555
Send a message via AIM to GSXdan
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;i0)
                                {
                                        temp=Names[j];
                                        Names[j]=Names[next];
                                        Names[next]=temp;

                                }
                }
        }
once again, thanks for your help doctorgonzo

^dan
GSXdan is offline   Reply With Quote
Old 05-01-2003, 07:43 PM   #10
Professional gadfly
 
doctorgonzo's Avatar
 
Join Date: Jan 2002
Location: Minneapolis, MN
Posts: 6,364
Send a message via MSN to doctorgonzo
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.
doctorgonzo is offline   Reply With Quote
Old 05-01-2003, 07:48 PM   #11
Member (10 bit)
 
GSXdan's Avatar
 
Join Date: Jun 2002
Location: P-burg, Ohio, USA
Posts: 555
Send a message via AIM to GSXdan
i see what you mean. always looking for ways to improve, so thanks for the input

^dan
GSXdan is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 07:58 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2