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 <iostream>
#include <iomanip>
#include <string>
#include <limits> // <--- Added.
#include <fstream>
int main()
{
constexpr int MAXROWS{ 5 }, MAXCOLS{ 2 };
std::string fileName, language, french, english; // <--- Changed. Added last 2.
std::ifstream inFile;
int numRows;
//ask user input for file and open file
//cout << "Name of language file to be read in: ";
//cin >> fileName;
inFile.open("french.txt");
//inFile.open( fileName.c_str() ); // <--- Not needed from C++11 on.
std::cout << "File successfully processed!" << '\n';
//take Language info and number of entries from top of .txt file
inFile >> language >> numRows;
inFile.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>. Added.
//create a 2D array for the languages in size of number of entries
std::string langArray[MAXROWS][MAXCOLS];
//for (int i = 0, j = 0; i < numRows && inFile; i++, j = 0)
//{
// if (std::getline(inFile, french, ','))
// {
// langArray[i][j++] = french;
// std::getline(inFile, english);
// langArray[i][j] = english;
// std::cout << i + 1 << ". " << french << " " << english << '\n';
// }
//}
for (int i = 0, j = 0; i < numRows && std::getline(inFile, french, ',') && std::getline(inFile, english); i++, j = 0)
{
langArray[i][j++] = french;
langArray[i][j] = english;
std::cout << i + 1 << ". " << french << " " << english << '\n';
}
std::cout << "\n\n For checking array contents.\n";
for (int row = 0; row < 4; row++)
{
std::cout << row + 1 << ". ";
for (int col = 0; col < 2; col++)
{
std::cout << langArray[row][col] << " ";
}
std::cout << '\n';
}
// <--- Keeps console window open when running in debug mode on Visual Studio. Or a good way to pause the program.
// 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; // <--- Not required, but makes a good break point.
}
|