I am trying to take data from one file, save it into an array and output it into another file. The problem I am having is the output displays some garbage 6 times value. I want it to run the loop 6 times but I cant figure out why I keep getting a garbage value. I am in the my first programming class so i do not know much about programming.
This is the information
3
35.9 147.74 -24.15 -5.62 -25.67 42.22
-36.32 70.53 49.71 121.29 96.63 94.97
-9.95 -29.67 -49.14 55.2 10.31 82.2
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
const int size = 6;
double numbers[size];
int count = 0;
What are you getting as your "garbage"? From what I can tell, your output file should have: "335.9147.74-24.15-5.62-25.67" after you run this program.
Right now what you're doing is just reading six numbers from the input file and then writing them to the output file. The first number in the input file seems to be the number of lines that follow, but you are not using this information. You need to read this number into a variable and then create a loop that will run that many times. In each iteration of the loop, you need to read six numbers from the input file and write them to the output file. I believe your code for reading from/writing to the files is OK, though you need to add formatting to the output file (space between numbers and a newline after each row).
It should be a new variable, called numLines or something. Then you need a for loop like: for (int i = 0; i < numLines; i++) { /* do stuff */ }
Inside the loop, you should paste your current input/output loops, and make a few changes to them. You need to format your output as I said above, and you need to either reset your "count" variable before you read from the input file, or just use a for loop with a new variable.
The "lines" variable should not appear anywhere inside your loop. Change it back to "count" in your while loop, and set count = 0; right before your loop (otherwise when it gets to the loop the second time, count will already be 6, and it won't loop).
Edit: Forgot to mention, please use code tags around your code. Put [ code] at the start of your code and [ /code] at the end, without the spaces inside the brackets.