I have to read in each line individually with cin.get() then convert the char to an int. The problem is, I don't know how to discard the newline character and get the number 9 after the number 7.
#include <iostream>
#include <fstream>
#include <cstdlib>
usingnamespace std;
constint NUMBERS = 26;
ifstream infile;
ofstream outfile;
void read(int storage[])
{
int i = 0, k = 0, convert = 0;
char check;
infile.open("MyNumbers.txt");
if (!infile)
{
cout << "Failed to open data file!\n";
cin.get();
exit(0);
}
while (infile.get(check))
{
if (check != '\n' && check != '\r')
{
convert = check - '0';
cout << "Convert Variable: " << convert << endl;
}
else
{
break;
}
}
infile.close();
}
void add_arrays(int first_array[], int second_array[], int sum_array[])
{
for (int i = 0; i < NUMBERS; i++)
{
}
}
int main()
{
int first_array[NUMBERS] = {0}, second_array[NUMBERS] = {0}, sum_array[NUMBERS] = {0},
storage[NUMBERS] = {0};
read(storage);
add_arrays(first_array, second_array, sum_array);
// cin.get();
return 0;
}
I need to get the first two lines, add them together in an array, then output that result. Then, I need to get the next two numbers in the file, add them together, and output the result and so on. I'm not sure how to ignore the newline and get the next numbers in the file. I'm kind of lost, what would be the best way to do this? Thanks if you can help.