@dutch,
Thank you for the input. I can see the truth in what you are saying and I will remember that for the future. Though in my defense I was recalling several examples I have seen here.
@orichalcum,
jonnin's post came through before I finished what I was working on and I missed out on his insight.
jonnin's for loop would work, but I was thinking ahead to a time whenyou might have to read more than 500 names. In which case you would reach "eof" before you wanted to. Say the file should contain 1000 names, but actually contains 999 names you would reach "eof" before you expected to.
I did a little rework of your code here:
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
|
//#include "pch.h"
//#include <cstdlib> // <--- In VS this is included through <iostream>.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
//#include "Qqueue.h" //documenton is in the header file
//using namespace std; // <--- Best not to use.
int main()
{
const int CAP{ 506 }; // <--- Increased to allow the for loop to work.
//const std::string PATH{ "F:/Text Files/" };
std::string arrayLast[CAP];
int userCap{};
//int* queue = new int[CAP];
std::ifstream file_;
std::string lastname;
file_.open("all.last.txt");
//file_.open(PATH + "500 Random Names.txt");
if (file_.fail())
{
std::cout << "\nError opening file";
return 1;
}
std::cout << "\n Type the number of of names you want to insert into the queue.";
std::cout << "\n The max capacity is 500 and the input must be atleast >= 100: ";
std::cin >> userCap;
std::cout << '\n';
//if (userCap > CAP)
//{
// std::cout << "\nQueue requested is too large.";
// return 2;
//}
//else if (userCap < 100)
//{
// std::cout << "\nQueue is too little. Must be atleast 100 or more.";
// return 3;
//}
int count = 0;
while (count < userCap && getline(file_, arrayLast[userCap]))
//while (count < userCap && getline(file_, arrayLast[count]))
{
std::cout << std::setw(3) << count + 1 << " " << arrayLast[count] << std::endl;
count++;
}
std::cout << "\n\n The array contains:\n Element Contents" << std::endl;
for (int lc = 0; lc < userCap + 5; lc++)
{
std::cout << std::setw(5) << lc << " " << arrayLast[lc] << std::endl;
}
file_.close();
// <--- Used to as a replacement for "system("pause")".
// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue: ";
std::cin.get();
return 0;
}
|
I had to comment out some of the code as I either did not need it or have it. Your program uses the header file "Qqueue.h", but you neglected to provide it. But that is OK there appears to be nothing in the code that uses anything from this header file. In the future if a program needs a header file that is not a standard C++ header file you need to include it in your post.
For the header files.
It has been said that the order of the "#include" files should not make any difference, but sometimes it does.
I believe that VS likes to have the "#include "pch.h" first. It has been awhile since I have used VS with the "pch.h" file. These days I just create an empty solution/project.
Putting the (#include "Qqueue.h") last allows for the preceding header files to cover anything that "Qqueue.h" may need. Since that code is loaded after the other header files.
For the purpose of this code I had to change the size of "CAP". Later you can change it back to 500.
Lines 15 and 25 I left as an example of what you can do should you ever need to provide a path to a file.
I had to comment out the if/else if as it is more of a hindrance right now.
Lines 59 - 64 I added to show what is actually in the array and where the names are actually being stored.
After you have run this switch the comments on lines 51 and 52 and see the difference in what is in your array.
The for loop and line 59 can be removed when you are done with your testing.
Hope that helps,
Andy