Using c++ nite a main routine in which the program will request the user to ente
ID: 3787322 • Letter: U
Question
Using c++
Explanation / Answer
// C++ code
#include <iostream>
#include <string>
#include <fstream>
#include <stdlib.h>
#include <math.h>
#include <vector>
#include <iomanip> // std::setprecision
using namespace std;
void readIntFile(ifstream &inFile, int intArray[], int size, int &length)
{
int i = 0, number;
while (true)
{
inFile >> number;
intArray[i] = number;
i++;
// protect overflow
if( i == size)
break;
// break at end of file
if(inFile.eof())
break;
}
inFile.close();
length = i;
}
void printFilevalue(int intArray[], int &length)
{
cout << "Array: ";
for (int i = 0; i < length; ++i)
{
cout << intArray[i] << " ";
}
cout << endl;
}
int main()
{
int size = 20;
int intArray[size];
int length;
ifstream inFile ("input.txt");
if (inFile.is_open())
{
if ( inFile.peek() == std::ifstream::traits_type::eof() )
{
cout << "FIle is empty ";
return 0;
}
readIntFile(inFile, intArray, size, length);
}
else cout << "Unable to open file";
printFilevalue(intArray, length);
return 0;
}
/*
input.txt
10245
10672
30403
20034
10334
29776
31123
11267
32001
10998
10003
29984
25650
32761
10099
22222
output:
Array: 10245 10672 30403 20034 10334 29776 31123 11267 32001 10998 10003 29984 25650 32761 10099 22222
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.