- #1
yungman
- 5,755
- 293
I have something I cannot fix in this program. Also I don't know how to test EOF in vector to jump out of do-while loop.
I try to enter 3 names. The first time ran ok, but it will skip asking the Last Name on the 2nd and 3rd name and you can see in the image of output captured. Somehow the program skip line22 the second and third time. I cannot figure this out as it's inside the do-while loop, why it work only the first go around?
Also, if you look at line39, I put a 3 to stop the loop. I really want to have a way to detect the EOF of the vector to stop the do-while loop. I cannot find this in the book. Please advice what can I do as this is only an experiment program. In my real program, I want to put in different number of names and I need to detect EOF to stop the operation. I don't know how to push_back an EOF to the last element of the vector array.
Thanks
Code:
//Vector length test
#include<iostream>
#include <vector>
#include <cstring>
#include <iomanip>
using namespace std;
const int ln = 21, al = 31, eA = 21, pN = 15;
struct empName
{ char lastName[ln]; char fstName[ln];};
struct Directory
{ empName name; int phone; };
int main()
{
char more;
Directory Info;//temporary structure
vector<Directory>IVar;
do
{
cout << setw(21) << left << " Enter Last name: ";
cin.getline(Info.name.lastName, ln); cout << endl;
cout << setw(21) << left << " Enter First name: ";
cin.getline(Info.name.fstName, ln); cout << endl;
cout << setw(21) << left << " Enter phone: ";
cin >> Info.phone; cout << "\n\n";
//Save into vector array.
IVar.push_back(Info);
cout << " Type 'y' if You want to enter more "; cin >> more;//Type 'y' to enter another one
} while (tolower(more) == 'y');
int index = 0;
cout << "This is what you entered:\n\n";
do
{
cout << " Name #" << (index + 1) << ": " <<
IVar[index].name.fstName << " " << IVar[index].name.lastName << endl;
cout << " Phone number: " << IVar[index].phone << "\n\n";
index++;
} while (index < 3);
return 0;
}
I try to enter 3 names. The first time ran ok, but it will skip asking the Last Name on the 2nd and 3rd name and you can see in the image of output captured. Somehow the program skip line22 the second and third time. I cannot figure this out as it's inside the do-while loop, why it work only the first go around?
Also, if you look at line39, I put a 3 to stop the loop. I really want to have a way to detect the EOF of the vector to stop the do-while loop. I cannot find this in the book. Please advice what can I do as this is only an experiment program. In my real program, I want to put in different number of names and I need to detect EOF to stop the operation. I don't know how to push_back an EOF to the last element of the vector array.
Thanks
Last edited: