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;
}
}
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...whileloop is some sort of double block, it’s not. The code after thewhile(...);in your code will only be executed once, after you break out of thedo...whileloop, 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
friendsandlastafter 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:
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 besearch(friendArray, sub);, for the reason I told you last time –subis a count of valid items in the array,maxsizeis 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
lastaround the area where the value should be if it existed.Perhaps if your logic incorporated
firstbeing modified somewhere, so that the conditionfirst <= lastwould fail and you’d break out of the loop…