First time working with a .txt to an array. I need help figuring out how to be able to get all the numbers in said file to be integers for me to use for things like basic math.
1 2 3 4 5 6 7 8 9
int main()
{
int value;
int max[40];
int sum;
ifstream file("filename.txt");
string pops;
while (getline(file, pops, '|'));
}
#include <iostream>
#include <iomanip>
#include <limits>
//#include <string>
//#include <cctype>
#include <fstream>
int main()
{
const std::string inFileName{ "Numbers.txt" }; // <--- Put File name here.
std::ifstream inFile(inFileName);
if (!inFile)
{
std::cout << "\n File " << std::quoted(inFileName) << " did not open." << std::endl;
//std::cout << "\n File \"" << inFileName << "\" did not open." << std::endl;
return 1;
}
constexprint MAXSIZE{ 40 };
int arrayCountIdx{};
int numsArray[MAXSIZE]{};
int sum{};
while (arrayCountIdx < MAXSIZE && inFile >> numsArray[arrayCountIdx])
{
arrayCountIdx++;
sum += numsArray[arrayCountIdx]; // <--- If needed here.
}
for (int idx = 0; idx <arrayCountIdx; idx++)
{
std::cout << ' ' << numsArray[idx];
}
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.
}
The first task is to get the numbers in the file into an array. After that you can use the array to do any math that you would like.
Line 33 is just a thought, but if you need to sum all the numbers in the array you can do this when you read the file then you will have the total in "sum" for later use.
The for loop at line 36 is not necessary as is, but to show the contents of the array. This can be changed to step through the array for other uses.
ALWAYS a good idea to initialize your variables when first defined.
OK so now how do I use that to use said numbers for math and such?
Right now you are the only one that can answer this. You have give no one any idea of what you want to do once you have the numbers in the array. Work up some code and post it. You can always some ideas of what to do or corrections if something is wrong.