Your basic algorithm is wrong for this task.
infile
does not read the number in a single digit at a time; you get the whole number at once into
inputnumber
.
You must read in the number, and then break it into individual digits, as you have indicated you want to put one digit into each element of the array.
1 2 3 4 5 6 7 8 9 10 11
|
// Converts the text file into a 2D array
ifstream infile("output.txt");
for (int i = 1; i < 101; i++)
{
infile >> inputnumber;
// Here add code to examine the number.
// If the number if less than one, put a zero into [i][0]
// If the number is equal to 1, put a one into [i][0]
// Now put a decimal point into [i][1]
// Now figure out the first number to put in after the decimal point etc etc.
}
|
It seems a very strange thing to do. Why do you choose to manually create every digit in the output yourself? If you want every number to be output to 11 decimal places, you can control that easily and you don't need to manually build every decimal place yourself.
Alternatively, don't read in a whole number; read in single
char
, and you can then place those
char
s into your grid one at a time. Every so foten, you will need to spot when the number has ended, and fill the remaining elements of that grid line with zero.
Here is code that produces the output I think you want, but it doesn't manually build every deciaml place into a big grid; it just uses the provided library functions. Is this what you're looking for?
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
|
#include <iostream>
#include <iomanip>
#include <conio.h>
#include <fstream>
//Using the standard namespace for files.
using namespace std;
//starting code for main program.
int main()
{
// Setting the variables.
int hold;
double inputnumber = 0;
double decimal[101] = {0};
double grid[101] = {0};
// Outputs a message on the screen.
cout << "Here are the special numbers in the range of 1-100n";
// Loop to create the array of the numbers in decimal form.
for(int i = 1; i < 101; i++)
{
decimal[i] = 1./i;
}
// Inputs array into a text file
ofstream output("output.txt");
for(int i = 1;i < 101;i++)
{
output << fixed << setprecision(11); // Fix decimal places
output << decimal[i]<<endl;
}
// Converts the text file into a 2D array
ifstream infile("output.txt");
for (int i = 1; i < 101; i++)
{
infile >> inputnumber;
grid[i] = inputnumber;
}
// Displays the array
for (int i = 1; i < 101; i++)
{
cout << fixed << setprecision(11); // Fix decimal places
cout << grid[i] << endl;
}
// Holds the screen up.
cin >> hold;
// Returns a value to end the code.
return(0);
}
|