Fix Mistakes in Project: Sorted Names

  • MHB
  • Thread starter needOfHelpCMath
  • Start date
  • Tags
    Project
In summary, the program has a mistake in the position swapping code, causing the names to be printed twice. This can be fixed by only swapping the names when the conditions in the for loop are met.
  • #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.

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
  • #2
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.
 
  • #3
Specifically, you only want to swap when either of the conditions in the [m]for (j...[/m] have been met.
 

1. How do I identify mistakes in my sorted names project?

To identify mistakes in your sorted names project, you can start by carefully reviewing the project requirements and comparing them to your current project. Look for any discrepancies or missing elements. You can also have a colleague or friend review your project for a fresh perspective.

2. What should I do if I find a mistake in my sorted names project?

If you find a mistake in your sorted names project, the first step is to acknowledge and take responsibility for the mistake. Next, make a plan to fix the mistake and communicate this plan to your team or supervisor. This may involve going back and correcting the mistake or finding a workaround solution.

3. How can I prevent mistakes in my sorted names project?

To prevent mistakes in your sorted names project, it is important to have a clear understanding of the project requirements and expectations. Make sure to double check your work and have someone else review it before finalizing. Additionally, maintain good organization and documentation throughout the project to easily track any potential mistakes.

4. Can I make changes to my sorted names project after it has been completed?

It is generally not recommended to make changes to a project after it has been completed, as this can cause confusion and disrupt the project's integrity. However, if the mistake is minor and does not significantly impact the project, it may be possible to make the necessary changes with proper communication and approval from your team or supervisor.

5. What should I do if I notice a mistake in my sorted names project after it has been submitted?

If you notice a mistake in your sorted names project after it has been submitted, it is important to communicate this to your team or supervisor as soon as possible. Depending on the severity of the mistake, you may need to retract the project and make necessary adjustments or find a solution to fix the mistake without affecting the project's overall progress.

Similar threads

  • Programming and Computer Science
Replies
6
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Programming and Computer Science
Replies
2
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
Back
Top