Hey all, I'm having trouble with an assignment for my C++ class.

The .pdf has the assignment parameters, and the .cpp file has what I've written so far.

Right now I've written the program enough to make sure the two strings (LastName and FirstName) get swapped correctly and put into the main string (FullName). Then I'm trying to test and make sure I can output them based on the subscript, but every time I run the program it shuts down.

I'm just wondering what I need to do.

I can't do:

const int contacts = numOfContacts;

FullName = new string[numOfContacts]

Because that just creates errors and such.

I think I need something related to that to predefine the number of elements.

Any help with this would be wonderful. :)

I swear the answer is right under my nose, and when I find out what I'm doing wrong, I'm going to feel like a dork. :P

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;

void SortContacts(string [], string [], int);
void swap(string *, string *);

int main()

	string FirstName, LastName, Number;
	string namePunc = ", ";
	string hyphen = "-";

	string *FullName = new string;
	string *PhoneNum = new string;

	int numOfContacts;

	cout << "\t\tContacts Management Program" << endl << endl;
	cout << "How many contacts will you be recording?: ";

	cin >> numOfContacts;

	string[] FullName = new string[numOfContacts];
	for (int n = 0; n < numOfContacts; n++)
		cout << "\n\nContact " << n + 1 << "!" << endl;
		cout << "\tFirst Name: ";
		cin >> FirstName;

		cout << "\tLast Name: ";
		cin >> LastName;

		swap(FirstName, LastName);

		cout << "\tPhone Number: ";
		cin >> Number;

		int nLength = LastName.size();

		// Add the space and comma/switch the names around
		*(FullName + n) = FirstName + LastName;

		*(PhoneNum + n) = Number.insert(3, hyphen);
		*(PhoneNum + n) = Number.insert(7, hyphen);

	return 0;

/*void SortContacts(string FullName[], string Phone[], int size)
	bool swap;
	string temp1, temp2;

		swap = false;
		for (int n = 0; n < (size - 1); n++)
			if (FullName[n].compare(0, 3, FullName[n + 1]) < 0); 
				swap(&FullName[n], &FullName[n + 1]);
				swap(&Phone[n], &Phone[n + 1]);
	} while (swap);*/

void swap(string * str1, string * str2)
	string temp;
	temp = *str1;
	*str1 = *str2;
	*str2 = temp;

You've declared FullName twice.
Get rid of the first one, and make the second string* FullName = new string[numOfContacts]; Then do something about PhoneNum not being an array of strings as well.

Got rid of the first one and declared the second like you said.

Now when I enter more than one contact and have my test cout run, the program shuts down with the "homework8.exe has stopped working. Windows is searching for a solution to the problem"

It's done this every time I've had the program building with no errors. Does it have to do with how I'm making the array for the fullnames?

This article has been dead for over six months. Start a new discussion instead.