So I have a project where I'm supposed to have a .txt input file of no more than ten first names, last names and birth years, and than in a menu I'm to give the user some options as to how the strings can be sorted. My problem is kind of hard to understand, so please bear with me. I've figured out how to sort the last names, first names and birth years individually, but how do I make it so that all 3 things match up correctly so that the first names, last names and birth years match up like they do in the file? Here's my code: -
-
#include <iostream>
-
#include <fstream>
-
#include <string>
-
-
using namespace std;
-
-
void sort_LastName( string[], int );
-
void sort_FirstName( string[], int );
-
void sort_BirthYear( int[], int );
-
-
void main()
-
{
-
-
string firstName[100];
-
string lastName[100];
-
int birthYear[] = {0};
-
int numElements = 10;
-
int option;
-
int count = 0;
-
int i = 0;
-
-
ifstream inputFile( "input.txt", ios::in );
-
-
if ( !inputFile )
-
{
-
cout << "File 'input.txt' could not be opened" << endl;
-
exit( 1 );
-
}
-
-
-
while ( !inputFile.eof() )
-
{
-
inputFile >> firstName[count] >> lastName[count] >> birthYear[count];
-
count++;
-
}
-
-
count--;
-
-
inputFile.close();
-
-
cout << "This program sorts names and birth years from input.txt. " << endl;
-
-
do
-
{
-
cout << "\n(1) Sort by last name " << endl
-
<< "(2) Sort by first name " << endl
-
<< "(3) Sort by birth year " << endl
-
<< "(4) Exit program " << endl << endl
-
<< "Choose an option: ";
-
cin >> option;
-
-
if ( option == 1 )
-
{
-
sort_LastName( lastName, numElements );
-
-
for ( i = 0; i < numElements; i++)
-
{
-
cout << "\n " << lastName[i] << ", " << firstName[i] << " " << birthYear[i] << endl;
-
}
-
}
-
-
if ( option == 2 )
-
{
-
sort_FirstName( firstName, numElements );
-
-
for ( i = 0; i < numElements; i++)
-
{
-
cout << "\n " << firstName[i] << " " << lastName[i] << ", " << birthYear[i] << endl;
-
}
-
}
-
-
if ( option == 3 )
-
{
-
sort_BirthYear( birthYear, numElements );
-
-
for ( i = 0; i < numElements; i++)
-
{
-
cout << "\n " << birthYear[i] << ", " << firstName[i] << " " << lastName[i] << endl;
-
}
-
}
-
-
if ( option == 4 )
-
{
-
cout << "\n\n";
-
exit(1);
-
}
-
-
} while ( option != 4 );
-
}
-
-
// sort by last name A-Z
-
void sort_LastName( string lastName[], int numElements )
-
{
-
for ( int i = 0; i < numElements-1; i++ )
-
{
-
int minIndex = i; //stores index of the min array value
-
-
for ( int j = i+1; j < numElements; j++ )
-
if ( lastName[j] < lastName[minIndex] )
-
minIndex = j;
-
-
//swap the strings at positions i and minIndex
-
string temp = lastName[minIndex];
-
lastName[minIndex] = lastName[i];
-
lastName[i] = temp;
-
}
-
-
}
-
-
// sort by first name A-Z
-
void sort_FirstName( string firstName[], int numElements )
-
{
-
for ( int i = 0; i < numElements-1; i++ )
-
{
-
int minIndex = i; //stores index of the min array value
-
-
for ( int j = i+1; j < numElements; j++ )
-
if ( firstName[j] < firstName[minIndex] )
-
minIndex = j;
-
-
//swap the strings at positions i and minIndex
-
string temp = firstName[minIndex];
-
firstName[minIndex] = firstName[i];
-
firstName[i] = temp;
-
}
-
}
-
-
// sort by birth year
-
void sort_BirthYear( int birthYear[], int numElements )
-
{
-
int startScan, minIndex, minValue;
-
-
for ( startScan = 0; startScan < numElements - 1; startScan++ )
-
{
-
minIndex = startScan;
-
minValue = birthYear[startScan];
-
-
for ( int index = startScan + 1; index < numElements; index++ )
-
{
-
if ( birthYear[index] < minValue )
-
{
-
minValue = birthYear[index];
-
minIndex = index;
-
}
-
}
-
-
birthYear[minIndex] = birthYear[startScan];
-
birthYear[startScan] = minValue;
-
}
-
}
-
Aside from matching up names/birth years, whenever I run the program I get a odd display of characters popping up. Obviously it's something weird with whatever's stored in the memory. How do I fix that? Thanks!!!
2 3953
Wish I could've edited my first post. =/
Anyways, I think I've figured out my main problem. Now can anyone tell me what I need to fix so that it doesn't output a load of random garbage characters everywhere? Here's the code, (it should compile and run fine): -
#include <iostream>
-
#include <fstream>
-
#include <string>
-
-
using namespace std;
-
-
void sort_LastName( string[], string[], int[], int );
-
void sort_FirstName( string[], string[], int[], int );
-
void sort_BirthYear( string[], string[], int[], int );
-
-
void main()
-
{
-
-
string firstName[100];
-
string lastName[100];
-
int birthYear[] = {0};
-
int numElements = 10;
-
int option;
-
int count = 0;
-
int i = 0;
-
-
ifstream inputFile( "input.txt", ios::in );
-
-
if ( !inputFile )
-
{
-
cout << "File 'input.txt' could not be opened" << endl;
-
exit( 1 );
-
}
-
-
-
while ( !inputFile.eof() )
-
{
-
inputFile >> firstName[count] >> lastName[count] >> birthYear[count];
-
count++;
-
}
-
-
count--;
-
-
inputFile.close();
-
-
cout << "This program sorts names and birth years from input.txt. " << endl;
-
-
do
-
{
-
cout << "\n(1) Sort by last name " << endl
-
<< "(2) Sort by first name " << endl
-
<< "(3) Sort by birth year " << endl
-
<< "(4) Exit program " << endl << endl
-
<< "Choose an option: ";
-
cin >> option;
-
-
if ( option == 1 )
-
{
-
sort_LastName( lastName, firstName, birthYear, numElements );
-
-
for ( i = 0; i < numElements; i++)
-
{
-
cout << "\n " << lastName[i] << ", " << firstName[i] << " " << birthYear[i] << endl;
-
}
-
}
-
-
if ( option == 2 )
-
{
-
sort_FirstName( lastName, firstName, birthYear, numElements );
-
-
for ( i = 0; i < numElements; i++)
-
{
-
cout << "\n " << firstName[i] << " " << lastName[i] << ", " << birthYear[i] << endl;
-
}
-
}
-
-
if ( option == 3 )
-
{
-
sort_BirthYear( lastName, firstName, birthYear, numElements );
-
-
for ( i = 0; i < numElements; i++)
-
{
-
cout << "\n " << birthYear[i] << ", " << firstName[i] << " " << lastName[i] << endl;
-
}
-
}
-
-
if ( option == 4 )
-
{
-
cout << "\n\n";
-
exit(1);
-
}
-
-
} while ( option != 4 );
-
}
-
-
// sort by last name A-Z
-
void sort_LastName( string lastName[], string firstName[], int birthYear[], int numElements )
-
{
-
for ( int i = 0; i < numElements-1; i++ )
-
{
-
int minIndex = i; //stores index of the min array value
-
-
for ( int j = i+1; j < numElements; j++ )
-
if ( lastName[j] < lastName[minIndex] )
-
minIndex = j;
-
-
//swap the strings at positions i and minIndex
-
string temp = lastName[minIndex];
-
lastName[minIndex] = lastName[i];
-
lastName[i] = temp;
-
-
string temp2 = firstName[minIndex];
-
firstName[minIndex] = firstName[i];
-
firstName[i] = temp2;
-
-
int temp3 = birthYear[minIndex];
-
birthYear[minIndex] = birthYear[i];
-
birthYear[i] = temp3;
-
}
-
-
}
-
-
// sort by first name A-Z
-
void sort_FirstName( string lastName[], string firstName[], int birthYear[], int numElements )
-
{
-
for ( int i = 0; i < numElements-1; i++ )
-
{
-
int minIndex = i; //stores index of the min array value
-
-
for ( int j = i+1; j < numElements; j++ )
-
if ( firstName[j] < firstName[minIndex] )
-
minIndex = j;
-
-
//swap the strings at positions i and minIndex
-
string temp = firstName[minIndex];
-
firstName[minIndex] = firstName[i];
-
firstName[i] = temp;
-
-
string temp2 = lastName[minIndex];
-
lastName[minIndex] = lastName[i];
-
lastName[i] = temp2;
-
-
int temp3 = birthYear[minIndex];
-
birthYear[minIndex] = birthYear[i];
-
birthYear[i] = temp3;
-
}
-
}
-
-
// sort by birth year youngest-oldest
-
void sort_BirthYear( string lastName[], string firstName[], int birthYear[], int numElements )
-
{
-
int startScan, minIndex, minValue;
-
-
for ( startScan = 0; startScan < numElements - 1; startScan++ )
-
{
-
minIndex = startScan;
-
minValue = birthYear[startScan];
-
-
for ( int index = startScan + 1; index < numElements; index++ )
-
{
-
if ( birthYear[index] < minValue )
-
{
-
minValue = birthYear[index];
-
minIndex = index;
-
}
-
}
-
-
birthYear[minIndex] = birthYear[startScan];
-
birthYear[startScan] = minValue;
-
-
firstName[minIndex] = firstName[startScan];
-
firstName[startScan] = minValue;
-
-
lastName[minIndex] = lastName[startScan];
-
lastName[startScan] = minValue;
-
}
-
}
-
-
Here are the contents of my 'input.txt', if someone wants to copy and paste it into their own .txt file to help me figue this out, (just a bunch of random names/dates):
John Doe 1968
Luke Smith 1974
Tyler Ray 1988
Mary Johnson 1982
Matt Leary 1955
Amanda Beckett 1970
Raymond Doriano 1960
Benjamin Brooke 1980
Maria Nunez 1948
Elizabeth Taylor 1966
D'oh! all I had to do was change the int[] to a string[]... What a pointless post.
Admin, please feel free to delete this post.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: VJ |
last post by:
Hello All,
The xml document below describes the contents of a folder in a
Document Managment System. I need to retrieve the DocId for the most
recently added (<Add_Date>) spreadsheet file(<name>...
|
by: baustin75 |
last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie
only when debugging in php designer 2005
--------------------------------------------------------------------------------
...
|
by: Pokerkook |
last post by:
Hello,
If anybody could help me with this I would greatly appreciate it. Or at least
tell me why I get the output of this garbage:
49
49
10
49
52
|
by: steve smith |
last post by:
Hi I'm still having some problems getting my head round this language.
A couple of things don't seem to work for me. First I am trying to
obtan a count of the number of words in a sting, so am...
|
by: hothead098 |
last post by:
ASSIGNMENT (4) USING AND MANIPUPATING ARRAYS (Chapter 10 material)
For this assignment you are to:
1) Create and manage arrays
a) One of type integers (containing 10 elements).
b) One of...
|
by: Rahul |
last post by:
Hi Everybody
I have some problem in my script. please help me. This is script file.
I have one *.inq file. I want run this script in XML files. But this
script errors shows . If u want i am...
|
by: Alex Chudnovsky |
last post by:
I have come across with what appears to be a significant performance bug in
..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same
data. Same data on the same CPU gets sorted a...
|
by: XML Beginner |
last post by:
I have an XML file that contains values that my application needs, so it
knows which database to connect to. It also contains a configuration option
so that I can specify which node to return...
|
by: DJ Dharme |
last post by:
Hi,
I really like to use stl as much as possible in my code. But I
found it really hard to understand by looking into there source code.
I have no idea about what iterator traits, heaps and...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |