- #1
yungman
- 5,755
- 293
I run into something strange when I step through debug. This is the program to add names one by one, and each name entered, it will sort and put in ascending order into vector DirV[]. I cannot explain why the if statement in line 51 doesn't work.
First name I enter is just 'r', I just put a single digit number for the phone eg. 5. Second name is 'e', third name is 't'.
I put break point at line 51. This will break only after I enter the third name 't'. I verified after the program stopped at line 51 using immediate window. Dir.name='t', and DirV[midPt]='e'. Line 51 is if(DirV[midPt].name > Dir.name) which should be FALSE. The program should jump to line 61. BUT instead, it goes to line 54 as if it's TRUE!
I don't know what to make of it, I must be missing something. Please take a look.
Thanks
First name I enter is just 'r', I just put a single digit number for the phone eg. 5. Second name is 'e', third name is 't'.
C++:
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
const static int nameLen = 25, phoneLen = 12;
struct Directory { char name[nameLen]; char phone[phoneLen]; };
std::vector<Directory>DirV;
Directory Dir;
int main()
{
char more, displayAll;
void add_sort();
do
{ cout << " Enter name: "; cin.getline(Dir.name, nameLen);
cout << " Enter phone: "; cin.getline(Dir.phone, phoneLen);
add_sort();
cout << " Do you want to enter another name? "; cin.get(more);
cin.ignore();
} while (tolower(more) == 'y');
int index = 0;
do
{ cout << "Name stored in DirV is: " << DirV[index].name <<
" phone: " << DirV[index].phone << "\n\n";
index++;
} while (index < DirV.size());
return 0;
}
void add_sort()
{
int size, element = 0;
int startPt = 0, midPt, endPt;
DirV.push_back(Dir);//Push to next available element of DirV.
size = DirV.size();
if (size > 1)//if size = 1, it's done
{ endPt = size - 2; midPt = (startPt + endPt) / 2;//endPt is last element before push_back
do//loop until startPt = midPt = endPt.
{ if (Dir.name > DirV[midPt].name)//next loop startPt = midPt + 1
{ startPt = midPt + 1; midPt = (startPt + endPt) / 2; }
else { endPt = midPt; midPt = (startPt + endPt) / 2; }//next loop endPt = midPt
} while (startPt != endPt);//Repeat until startPt = midPt = endPt
if(midPt == size - 2)// if midPt is the last original element.
{
if (Dir.name < DirV[midPt].name)
{ DirV[size - 1] = DirV[midPt]; DirV[midPt] = Dir; }
else { DirV[endPt] = Dir; }
}
else//midPt < (size - 2)
{
if (DirV[midPt].name > Dir.name)
{//Dir fit into DirV[midPt] if DirV[midPt].name > Dir.name
//Move DirV[midPt] to DirV[size-2] down one element
for( int ind = 1; ind < (size - midPt - 2); ind++)
{ DirV[size - ind] = DirV[size - ind - 1]; }
DirV[midPt] = Dir;
}
else
{//DirV[midPt].name <= Dir.name, DirV[midPt+1] = Dir
//Move DIrV[midPt+1 to DirV[size-2] down one element.
for (int ind = 1; ind < (size - midPt - 3); ind++)
{ DirV[size - ind] = DirV[size - ind - 1];}
DirV[midPt] = Dir;
}
}
}// end if(size > 1)
}// end of add_sort
I put break point at line 51. This will break only after I enter the third name 't'. I verified after the program stopped at line 51 using immediate window. Dir.name='t', and DirV[midPt]='e'. Line 51 is if(DirV[midPt].name > Dir.name) which should be FALSE. The program should jump to line 61. BUT instead, it goes to line 54 as if it's TRUE!
I don't know what to make of it, I must be missing something. Please take a look.
Thanks
Last edited by a moderator: