Search won't work after sort in Array

My code works - It asks for 3 names and tel#'s then prints them in ascending order. It is next supposed to "ask for a name to search" but it stops after it prints and doesn't ask. Can anyone help? Please and thank you!

#include <cstdlib>
#include <iostream>
#include <string> // Some compilers need this for strings.
#include <iomanip> // Include this library to custom format cout output.
using namespace std;
int main()
{

//Declare and initialize arrays:
// Define arrays of 3 names and telephone numbers
int index = -1; // As a standard practice you should declare all
string lastName[3]{};
string firstName[3]{};
string teleNumber [3]{};

int soughtValue = -2147483648;
int leftIndex = 0; //left index of the currently examined array
int rightIndex = 10; //right index of the currently examined array
int midpoint = (rightIndex + leftIndex)/2; //int division drops remainder
bool found = false;

cout<<"Please Enter 3 Customer Names with 10-digit Telephone Numbers\n"<<endl;
index=0;
do
{
cout<<"Please Enter firstName"<<(index+1)<<":"<<endl;
cin>>firstName[index];
cout<<"Please Enter lastName"<<(index+1)<<":"<<endl;
cin>>lastName[index];
cout<<"Please Enter teleNumber"<<(index+1)<<":"<<endl;
cin>>teleNumber[index];
index++;

}
while(index<3);
{
cout<<endl;
}

for(index=0;index<3;index++)
{
cout<<left<< setw(20)<<firstName[index]
<<setw(20)<<lastName[index]
<<setw(9)<<teleNumber[index]<<endl;
}
for(int row=0;row<3;row+=1)
{
for(int col=0;col<3;col+=1)
cout<<firstName[3][3]<<" ";
cout<<lastName[3][3]<<" ";
cout<<teleNumber[3][3]<<" ";
//end for
cout<<endl;
}//end for
//system ("pause");

int myArray[3]={};
int pass = -1;

for (pass = 0; pass < 2; pass++)
{
for (index = 0; index < 2; index++)
{
if ( myArray[index] > myArray[index + 1] )
{

myArray[index] = myArray[index + 1];
myArray[index + 1] = {};
}
}
}

for(index = 0; index < 10; index++)
{
cout << myArray[index];
if (index < 9) cout << " ";
}


cout << "Please enter a name to search for and press enter.\n\n";
cin >> soughtValue;

while (leftIndex <= rightIndex)
{
midpoint = (rightIndex + leftIndex)/2;
cout << "leftIndex=" << leftIndex << " rightIndex=" << rightIndex
<< " midpoint =" << midpoint << endl;
if (myArray[midpoint] == soughtValue)
{
found = true;
break;
}
else if (soughtValue < myArray[midpoint])
{
rightIndex = midpoint - 1;
}
else
{
leftIndex = midpoint + 1;
}
}

if(found)
{
cout << "The value, " << soughtValue << " was found at index "
<< midpoint << endl;
}
else
{
cout << "The value " << soughtValue << " was not found." << endl;
}

return 0;

}
your code, indented (my comments are in italics)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <cstdlib>
#include <iomanip> // Include this library to custom format cout output.
#include <iostream>
#include <string> // Some compilers need this for strings.
using namespace std;
int main() {

	// Declare and initialize arrays:
	// Define arrays of 3 names and telephone numbers
	int index = -1; // As a standard practice you should declare all (¿all what? and you should declare at the point of use)
	string lastName[3]{};
	string firstName[3]{};
	string teleNumber[3]{};

	int soughtValue = -2147483648;
	int leftIndex = 0;   // left index of the currently examined array
	int rightIndex = 10; // right index of the currently examined array
	int midpoint = (rightIndex + leftIndex) / 2; // int division drops remainder
	bool found = false;

	cout << "Please Enter 3 Customer Names with 10-digit Telephone Numbers\n"
	     << endl;
	index = 0;
	do {
		cout << "Please Enter firstName" << (index + 1) << ":" << endl;
		cin >> firstName[index];
		cout << "Please Enter lastName" << (index + 1) << ":" << endl;
		cin >> lastName[index];
		cout << "Please Enter teleNumber" << (index + 1) << ":" << endl;
		cin >> teleNumber[index];
		index++;

	} while(index < 3);
	{ cout << endl; }

	for(index = 0; index < 3; index++) {
		cout << left << setw(20) << firstName[index] << setw(20)
		     << lastName[index] << setw(9) << teleNumber[index] << endl;
	}
	for(int row = 0; row < 3; row += 1) {
		for(int col = 0; col < 3; col += 1)
			cout << firstName[3][3] << " "; //out of bounds
		cout << lastName[3][3] << " ";
		cout << teleNumber[3][3] << " ";
		// end for (wrong, check the indentation)
		cout << endl;
	} // end for
	// system ("pause");

	int myArray[3] = {};
	int pass = -1;

	for(pass = 0; pass < 2; pass++) { //¿what's this for?
		for(index = 0; index < 2; index++) {
			if(myArray[index] > myArray[index + 1]) { //out of bounds

				myArray[index] = myArray[index + 1]; //out of bounds
				myArray[index + 1] = {}; //out of bounds
			}
		}
	}

	for(index = 0; index < 10; index++) { //out of bounds...
		cout << myArray[index];
		if(index < 9)
			cout << " ";
	}

	cout << "Please enter a name to search for and press enter.\n\n";
	cin >> soughtValue; //`soughtValue' is an int, but you're asking for a name

	while(leftIndex <= rightIndex) {
		midpoint = (rightIndex + leftIndex) / 2;
		cout << "leftIndex=" << leftIndex << " rightIndex=" << rightIndex
		     << " midpoint =" << midpoint << endl;
		if(myArray[midpoint] == soughtValue) {//guess what, out of bounds.  also, ¿when did you put values in `myArray'?
			found = true;
			break;
		} else if(soughtValue < myArray[midpoint]) {
			rightIndex = midpoint - 1;
		} else {
			leftIndex = midpoint + 1;
		}
	}

	if(found) {
		cout << "The value, " << soughtValue << " was found at index "
		     << midpoint << endl;
	} else {
		cout << "The value " << soughtValue << " was not found." << endl;
	}

	return 0;
}

Last edited on
Wow, that was fast. Thank you. I like that "out of bounds" I guess, haha. I can fix this now. Thank you for your patience, tolerance, and kindness in helping me. I appreciate it!
Topic archived. No new replies allowed.