Friday, 15 February 2013

arrays - C++ segmentation fault pointers -



arrays - C++ segmentation fault pointers -

a programme writing takes array of names , ages , alphabetizes them. programme work. of time segmentation fault halfway through printing alphabetized list.

this sample header file

public: static const int maxstu = 50; student(); void inputdata(); void sortdata(); void printoriginaldata(); void printalphadata(); private: int arraytotal; int *temp1; std::string *temp2; std::string originalname[maxstu]; std::string *alphaname[maxstu]; int originalage[maxstu]; int *alphaage[maxstu];

these functions causing issues.

void student::sortdata() { for(int i=0;i < arraytotal;i++) { if (originalname[i] > originalname[i+1] ) { temp1 = alphaage[i]; alphaage[i] = alphaage[i + 1]; alphaage[i + 1] = temp1; temp2 = alphaname[i]; alphaname[i] = alphaname[i + 1]; alphaname[i+1]= temp2; } } } void student::printalphadata() { cout << "\nalphabetized list\n"; cout << "---------------------------\n"; (int = 0; < arraytotal; ++i) { cout << left << setw(30) << *alphaname[i]; cout << right << setw(10) << *alphaage[i] << endl; } cout << endl; }

any help figuring out issue appreciated. there improve way sort array alphabetical order?

c++ arrays pointers

No comments:

Post a Comment