I need a bit of guidance to know if I am on the right track.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
[code[code]]#include <iostream>
#include <stdio.h>
#include <cstring>
using namespace std;
struct People
{
std::string lname;
std::string fname;
};
const int numNames=3;//this makes the code more flexible, sets the amount of names to sort
int main()
{
char name [numNames][80];
char last_name[numNames][80];
char name_buff [1][80];
cout << "Please enter 3 names to be sorted.\n";
cout<< "\nEntires should be of the format\n";
cout << "First_Name Last_Name." << "\n\n";
for (int i=0; i<numNames; ++i)//use a loop to get user input
gets(name[i]);
cout<< "\n";
cout<<"***Initialise check***"<<"\n\n";
for (int i=0; i<numNames; ++i)//this loop will show the the user the data that has been input
cout << (name[i])<<"\n";
cout<< "\n***Check Complete***"<< "\n\n";
cout<< "Press ENTER to contine";
cin.get();
// This is the bubble sort.
for(int a=1; a<=numNames; a++)
{
for(int b=numNames-1; b>=a; b--)
{
if(name[b-1] [0] > name[b][0])
{ // if out of order exchange elements
strcpy(name_buff [1],name[b-1]);
strcpy(name[b-1], name[b]);
strcpy(name[b], name_buff[1]);
}
}
}
cout << "\nHere are the names in alphabetical order." << "\n\n";
for (int i = 0; i < numNames; i++)
cout << name[i] << "\n";
return 0;
}
|
[/code]
// This is what I have above and below is what I need to do.
Write a C++ program that keeps track of an array of Person objects and handles basic commands.
The Person object contains the following fields:
FirstName { string }
LastName { string }
INSERT - inserts a Person into the array based on user input, sorts the array by last name, and prints out all of the entries in the array. (Format: INSERT - LastName, FirstName)
If a given Person already exists in the array, then the program will return an error message to the user.
If the user input does not follow the LastName, FirstName format, then the program will return an error message to the user.
Example:
Input - INSERT - Smith, John
Output
Doe, Jane
Smith, John
VIEW - prints out the current array. (Format: VIEW)
Example:
Input - VIEW
Output
Doe, Jane
Smith, John
DELETE - deletes a person from the array based on user input, sorts the array, and prints out all of the entries in the new array. (Format: DELETE - LastName, FirstName)
If the Person to be deleted from the array does not exist in the array, then the program will return an error message to the user.
If the user input does not follow the LastName, FirstName format, then the program will return an error message to the user.
Example:
Input - DELETE - Smith, John
Output
Doe, Jane