MHB Fix Mistakes in Project: Sorted Names

  • Thread starter Thread starter needOfHelpCMath
  • Start date Start date
  • Tags Tags
    Project
Click For Summary
SUMMARY

The forum discussion centers on a C++ program designed to sort names but contains a critical error causing names to be printed twice. The issue lies in the position swapping logic within the sorting algorithm, specifically in the nested loop where swaps occur. The user is advised to only perform swaps when the conditions for sorting are met, ensuring that names are not duplicated in the output. Correcting this logic will resolve the problem and yield the expected output.

PREREQUISITES
  • Understanding of C++ programming language
  • Familiarity with vectors in C++
  • Knowledge of sorting algorithms, specifically selection sort
  • Experience with console input/output in C++
NEXT STEPS
  • Review C++ vector manipulation techniques
  • Study selection sort algorithm implementation in C++
  • Learn about debugging techniques for C++ programs
  • Explore best practices for handling user input in console applications
USEFUL FOR

C++ developers, software engineers, and students learning about sorting algorithms and vector manipulation in C++. This discussion is particularly beneficial for those troubleshooting similar issues in their programs.

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.
 
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
5K