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++;
if(found)
{
cout << "The value, " << soughtValue << " was found at index "
<< midpoint << endl;
}
else
{
cout << "The value " << soughtValue << " was not found." << endl;
}
#include <cstdlib>
#include <iomanip> // Include this library to custom format cout output.
#include <iostream>
#include <string> // Some compilers need this for strings.
usingnamespace 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 namewhile(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;
} elseif(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;
}
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!