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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
What percentage of programmers have learned to touch type? Have you? Do you think it's important, not just for programming, but for more-than-casual computer users generally? ChatGPT didn't have much on it ("Research indicates that less than 20% of people can touch type fluently, with many relying on the hunt-and-peck method for typing ."). 'Hunt-and-peck method' made me smile. It added, "For programmers, touch typing is a valuable skill that can enhance speed, accuracy, and focus. While...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top