MHB Fix Mistakes in Project: Sorted Names

  • Thread starter Thread starter needOfHelpCMath
  • Start date Start date
  • Tags Tags
    Project
AI Thread Summary
The program is intended to collect first and last names from user input, store them in vectors, and then sort and display them. However, it incorrectly prints names multiple times due to an issue in the swapping logic within the sorting loop. The swapping of names occurs too frequently, specifically within the inner loop, leading to repeated entries in the output. To resolve this, the swapping should only happen after determining the correct minimum index based on the sorting conditions, rather than during each iteration of the inner loop. This adjustment will ensure that names are printed correctly without duplication.
needOfHelpCMath
Messages
70
Reaction score
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.

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; }
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
 
Last edited:
Technology news on Phys.org
needOfHelpCMath said:
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.

Your problem is with your position swapping code. How many times for each position in the name list do you want to be swapping things in your vector? Fix that issue and everything will work correctly.
 
Specifically, you only want to swap when either of the conditions in the [m]for (j...[/m] have been met.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Thread 'Project Documentation'
Trying to package up a small bank account manager project that I have been tempering on for a while. One that is certainly worth something to me. Although I have created methods to whip up quick documents with all fields and properties. I would like something better to reference in order to express the mechanical functions. It is unclear to me about any standardized format for code documentation that exists. I have tried object orientated diagrams with shapes to try and express the...
Back
Top