Click to See Complete Forum and Search --> : String Manipulation


Stacy
11-22-2001, 10:17 PM
Hey! I am a little bit lost on a project I am working on. Basically I have
to prompt the user for 10 first and last names, and then their birth dates.
Then I use a menu to see what they want this sorted by. I have an idea on
how to do the first and last name sorts, but I have no idea how to manipulate
the birthdate entries to elimate the / when they enter mm/dd/yyyy and then
make it sort by year. If anyone knows I would really appreciate a point in
the right direction! Thanks! Here is the code I have so far.

/*

Project #4
*/

#include <iostream.h>
#include <stdlib.h>
#include <iomanip.h>
#include <conio.h>

int menu ();
void sortfirst();
void sortlast();
void sortbirth();

struct Info
{
char fname [20]; //First Name
char lname [20]; //Last Name
char bdate [20]; //Unsorted Birthday
char sdate [20]; //Sorted Birthday

};

void main(void)
{

Info person[10];

for(int ctr=0; ctr<10; ctr++)
{
cout<<"Please enter a name (first then last): ";
cin>> person[ctr].fname;
cin>> person[ctr].lname;
cout<<"Now enter their birth-date (mm/dd/yyyy): ";
cin>>person[ctr].bdate;

}

int selection;

do
{
selection = menu();

switch(selection)
{
case 1: sortfirst();
break;
case 2: sortlast();
break;
case 3: sortbirth();
break;
case 4: cout<<"Exiting Program.\n\n";
cout<<"Press any key to continue";
getch();

}

}while (selection!=4);


}

int menu ()
{
int choice;

do
{
cout<<endl;

cout<<"How would you like the information sorted?\n";
cout<<" 1) Sorted by first name\n";
cout<<" 2) Sorted by last name\n";
cout<<" 3) Sorted by birth-date\n";
cout<<" 4) Exit Program\n\n";
cout<<"Enter 1, 2, 3, 4: ";
cin>> choice;

if(choice < 1 || choice > 5)
{
cout<< "Invalid Selection\n";
break;
}


} while (choice < 1 || choice > 5);


return choice;
}

void sortfirst()
{

cout<<"\nYou got to sort by first name";
}

void sortlast()
{
cout<<"\nYou got to sort by last name";
}

void sortbirth()
{
cout<<"\nYou got to sort by birth-date";
}

ch0rlt0n
11-23-2001, 08:18 AM
Have a look at the strtok function in your C++ help. Pass in "/" as the token
string and subsequent calls should return the date, the month and then the
year as character buffers. You can then use atoi() to convert the string
to an integer and sort...

"Stacy" <swagner001@yahoo.com> wrote:
>
>Hey! I am a little bit lost on a project I am working on. Basically I have
>to prompt the user for 10 first and last names, and then their birth dates.
>Then I use a menu to see what they want this sorted by. I have an idea on
>how to do the first and last name sorts, but I have no idea how to manipulate
>the birthdate entries to elimate the / when they enter mm/dd/yyyy and then
>make it sort by year. If anyone knows I would really appreciate a point
in
>the right direction! Thanks! Here is the code I have so far.
>