Help with people sorter

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
Last edited on
The very first words in your assignment are:

Write a C++ program that keeps track of an array of Person objects


I don't see any such objects in your code. It seems to me that the first thing you need to do is write a Person class that matches what you've been told to create. Once you've done that, then you can think about writing the program that manages an array of such objects.
1
2
3
4
5
6
struct People
{
	std::string lname;
	std::string fname;
	
};

//Would this be ok?
It depends on how strict your teacher is going to be about you following their instructions, since you've decided to use different names from the ones in the instructions.

I suspect that, really, your first step might be to actually read the instructions CAREFULLY :)

Also, is there a reason you decided to use a struct rather than a class?
Also, is there a reason you decided to use a struct rather than a class?

That is too obvious. The OP's struct only has two very simple members. There are no methods, constructors or any complicated stuff.
I was asking the OP, as only the OP knows what additional instructions might have been given by their teacher. If the teacher is using this exercise to help teach the principles of OOP, then obviously using a struct isn't going to be the right way to go.
Last edited on
All the instructions are listed. We can use structs or classes I am really new so I figured structs would be easier. Plus the program has so many different things it is suppose to do I do not need to complicate things. The name examples given were just examples. I read the instructions I'm just having a hard time incorporating everything that needs to be in it.
Topic archived. No new replies allowed.