- #1
needOfHelpCMath
- 72
- 0
May you guys look over my program I can't figure out my mistake. It looks i am printing the names two times but can't find the issue in the program.
What I bold is what is what I got wrong.
Compare output
Input:
david
ruby
john
doe
steve
smith
Your output:
Enter First Name: Enter Last Name: david
ruby
Enter First Name: Enter Last Name: john
doe
Enter First Name: Enter Last Name: steve
smith
Enter First Name:
--Sorted Names--
john doe
john doe
steve smith
Expected output:
Enter First Name: Enter Last Name: david ruby
Enter First Name: Enter Last Name: john doe
Enter First Name: Enter Last Name: steve smith
Enter First Name:
--Sorted Names--
john doe
david ruby
steve smith
Compare output
Your output:
Enter First Name:
--Sorted Names--
Compare output
Input:
Patricia
Ruby
David
Ruby
Jon
Smith
Eugene
Doe
Your output:
Enter First Name: Enter Last Name: Patricia
Ruby
Enter First Name: Enter Last Name: David
Ruby
Enter First Name: Enter Last Name: Jon
Smith
Enter First Name: Enter Last Name: Eugene
Doe
Enter First Name:
--Sorted Names--
Eugene Doe
David Ruby
David Ruby
Jon Smith
Expected output Enter First Name: Enter Last Name: Patricia Ruby
Enter First Name: Enter Last Name: David Ruby
Enter First Name: Enter Last Name: Jon Smith
Enter First Name: Enter Last Name: Eugene Doe
Enter First Name:
--Sorted Names--
Eugene Doe
David Ruby
Patricia Ruby
Jon Smith
Compare output
Input:
David C.
Ruby
Tina
Priddy
Your output Enter First Name: Enter Last Name: David C.
Ruby
Enter First Name: Enter Last Name: Tina
Priddy
Enter First Name:
--Sorted Names--
Tina Priddy
David C. Ruby
Expected output Enter First Name: Enter Last Name: David C. Ruby
Enter First Name: Enter Last Name: Tina Priddy
Enter First Name:
--Sorted Names--
Tina Priddy
David C. Ruby
Compare output
Input:
Sarah
Wolf
Maureen
Smithz
Jon
Smith
Angela
Abram
Your output:
Enter First Name: Enter Last Name: Sarah
Wolf
Enter First Name: Enter Last Name: Maureen
Smithz
Enter First Name: Enter Last Name: Jon
Smith
Enter First Name: Enter Last Name: Angela
Abram
Enter First Name:
--Sorted Names--
Angela Abram
Jon Smith
Maureen Smithz
Sarah Wolf
Expected output:
Enter First Name: Enter Last Name: Sarah Wolf
Enter First Name: Enter Last Name: Maureen Smithz
Enter First Name: Enter Last Name: Jon Smith
Enter First Name: Enter Last Name: Angela Abram
Enter First Name:
--Sorted Names--
Angela Abram
Jon Smith
Maureen Smithz
Sarah Wolf
What I bold is what is what I got wrong.
Code:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main () {
vector<string>firstNames;
vector<string>lastNames;
string temp1;
string temp2;
int j;
int i = 0;
int minIndex;
string minValuelastNames;
string minValuefirstNames;
bool done = false;
while(!done){
// Prompt user with "Enter First Name: "
cout << "Enter First Name: ";
getline(cin, temp1);
if (!temp1.empty()){
// Add to vector of First Names
firstNames.push_back(temp1);
// Prompt user with "Enter Last Name: "
cout << "Enter Last Name: ";
getline(cin, temp2);
lastNames.push_back(temp2);
// Add to vector of Last Names
// Print out First Name and Last Name ending with newline (endl)
cout << temp1 << " " << endl;
cout << temp2 << endl;
}
else {
done = true;
}
}
for ( i = 0; i < lastNames.size(); ++i) {
minValuelastNames = lastNames.at(i);
minValuefirstNames = firstNames.at(i);
minIndex = i;
for ( j = i; j < lastNames.size(); ++j) {
if ( lastNames.at(j) < minValuelastNames) {
minIndex = j;
minValuefirstNames = firstNames.at(j);
minValuelastNames = lastNames.at(j);
}
else if ( lastNames.at(j) == minValuelastNames)
{
if ( firstNames.at(j) < minValuefirstNames) {
minIndex = j;
minValuefirstNames = firstNames.at(j);
minValuelastNames = lastNames.at(j);
}
}
firstNames.at(minIndex) = firstNames.at(i);
firstNames.at(i) = minValuefirstNames;
lastNames.at(minIndex) = lastNames.at(i);
lastNames.at(i) = minValuelastNames;
}
}
cout << endl << " --Sorted Names-- " << endl;
for ( i = 0; i < firstNames.size(); ++i) {
cout << firstNames.at(i);
cout << " " << lastNames.at(i) << endl;
}
return 0; }
Input:
david
ruby
john
doe
steve
smith
Your output:
Enter First Name: Enter Last Name: david
ruby
Enter First Name: Enter Last Name: john
doe
Enter First Name: Enter Last Name: steve
smith
Enter First Name:
--Sorted Names--
john doe
john doe
steve smith
Expected output:
Enter First Name: Enter Last Name: david ruby
Enter First Name: Enter Last Name: john doe
Enter First Name: Enter Last Name: steve smith
Enter First Name:
--Sorted Names--
john doe
david ruby
steve smith
Compare output
Your output:
Enter First Name:
--Sorted Names--
Compare output
Input:
Patricia
Ruby
David
Ruby
Jon
Smith
Eugene
Doe
Your output:
Enter First Name: Enter Last Name: Patricia
Ruby
Enter First Name: Enter Last Name: David
Ruby
Enter First Name: Enter Last Name: Jon
Smith
Enter First Name: Enter Last Name: Eugene
Doe
Enter First Name:
--Sorted Names--
Eugene Doe
David Ruby
David Ruby
Jon Smith
Expected output Enter First Name: Enter Last Name: Patricia Ruby
Enter First Name: Enter Last Name: David Ruby
Enter First Name: Enter Last Name: Jon Smith
Enter First Name: Enter Last Name: Eugene Doe
Enter First Name:
--Sorted Names--
Eugene Doe
David Ruby
Patricia Ruby
Jon Smith
Compare output
Input:
David C.
Ruby
Tina
Priddy
Your output Enter First Name: Enter Last Name: David C.
Ruby
Enter First Name: Enter Last Name: Tina
Priddy
Enter First Name:
--Sorted Names--
Tina Priddy
David C. Ruby
Expected output Enter First Name: Enter Last Name: David C. Ruby
Enter First Name: Enter Last Name: Tina Priddy
Enter First Name:
--Sorted Names--
Tina Priddy
David C. Ruby
Compare output
Input:
Sarah
Wolf
Maureen
Smithz
Jon
Smith
Angela
Abram
Your output:
Enter First Name: Enter Last Name: Sarah
Wolf
Enter First Name: Enter Last Name: Maureen
Smithz
Enter First Name: Enter Last Name: Jon
Smith
Enter First Name: Enter Last Name: Angela
Abram
Enter First Name:
--Sorted Names--
Angela Abram
Jon Smith
Maureen Smithz
Sarah Wolf
Expected output:
Enter First Name: Enter Last Name: Sarah Wolf
Enter First Name: Enter Last Name: Maureen Smithz
Enter First Name: Enter Last Name: Jon Smith
Enter First Name: Enter Last Name: Angela Abram
Enter First Name:
--Sorted Names--
Angela Abram
Jon Smith
Maureen Smithz
Sarah Wolf
Last edited: