Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6890551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:17:48+00:00 2026-05-27T06:17:48+00:00

I’ve been studying everything I’ve gone over in my first semester of programming. I

  • 0

I’ve been studying everything I’ve gone over in my first semester of programming. I have a final coming up so I’ve been trying to write sample programs combining everything I’ve learned to prepare. The program below is supposed to read in names from a file, sort them via bubble search, and then prompt the user to enter a name, which the binary search will look for and tell you if the person is a friend or not.

My problem is, when I type a name, I am only prompted to type the name again. There is no output.

Please keep in mind that everything in here is mostly what I’ve learned so far (so I do not know how to use vectors, pointers, etc).

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void bubblesort(string[], const int);
void search(string[], const int);
int sub = 0;

int main()
{
 const int maxsize = 100;
 string friendArray[maxsize];

 ifstream friends;
 friends.open("myFriends.dat");

 while (sub < maxsize && getline(friends, friendArray[sub]))
   sub++;


 bubblesort(friendArray, sub);
 search(friendArray, maxsize);


 system("pause");
 return 0;
}



void bubblesort(string *array, const int size)
{
    bool swap;
    string temp;

    do
    {
        swap = false;
        for (int count = 1; count < (size - 1); count++)
        {
            if(array[count-1] >array[count])
            {
                temp = array[count-1];
                array[count-1] = array[count];
                array[count] = temp;
                swap = true;
            }
        }
    }
    while(swap);

}

void search(string *array, int size)
{
    int first = 0;
    int last = size - 1;
    int middle;
    string name;
    bool friends = false;

    do
    {
     cout<<"Please enter a name or END to terminate:";
     cin>>name;
    }
    while(!friends && first <= last && name != "END");
    {
        middle = (first + last) / 2;
        if (array[middle] == name)
            {
                friends = true;
                cout<<array[middle]<<" is my friend."<<endl;
            }
        else if (array[middle] > name)
            last = middle - 1;
        else
            last = middle + 1;
    }
 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:17:48+00:00Added an answer on May 27, 2026 at 6:17 am

    I don’t want to give too much away, since it’s homework, so I’ll move some of your code around, keeping it as close as possible, and show you why it currently won’t work.

    At the moment you’re kind of thinking the do...while loop is some sort of double block, it’s not. The code after the while(...); in your code will only be executed once, after you break out of the do...while loop, it’s in no way connected. You’re going to need two loops, an outer one that prompts for names, and an inner one that looks for that name in your list.

    You’re also not resetting friends and last after asking the user to enter another name. An easy fix is to move your declarations (which contain initialisations) inside the first loop.

    This is what your code will look like after mostly rearranging it and apply the above changes:

    void search(string *array, int size)
    {
       string name;
    
       cout<<"Please enter a name or END to terminate:";
       cin>>name;
    
       while (name != "END")
       {
          bool friends = false;
          int first = 0;
          int last = size - 1;
          int middle;
    
          while(!friends && first <= last)
          {
             middle = (first + last) / 2;
             if (array[middle] == name)
             {
                 friends = true;
                 cout<<array[middle]<<" is my friend."<<endl;
             }
             else if (array[middle] > name)
                last = middle - 1;
             else
                last = middle + 1;
          }
    
          cout<<"Please enter another name or END to terminate:";
          cin>>name;
       }      
    }
    

    There’s two different prompts this time, so that if the user enters "END", the outside loop terminates immediately, rather than having to add an extra check inside the loop.

    Also, as with your other question, search(friendArray, maxsize); should be search(friendArray, sub);, for the reason I told you last time – sub is a count of valid items in the array, maxsize is the capacity of the array.

    NOTE: If the name doesn’t exist in your list, it’ll cause an infinite loop. I’ll let you work that out since it’s homework and I don’t want to change any of your actual logic. A hint though is to think about what’s actually happening – if a value doesn’t exist you’ll just keep incrementing and decrementing last around the area where the value should be if it existed.

    Perhaps if your logic incorporated first being modified somewhere, so that the condition first <= last would fail and you’d break out of the loop…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to loop through a bunch of documents I have to put
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.