Need help with input and output using arrays

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;


ifstream inputfile;
ofstream outputfile;

inputfile.open("C:\\Users\\Nick\\Desktop\\Project2data\\proj2data.txt");

outputfile.open("C:\\Users\\Nick\\Desktop\\ProjectData\\Project.txt");


while (count < size && inputfile >> numbers[count])
count++;



for(count = 0; count < size; count++)
outputfile << numbers[count];







cin.ignore();
cin.get();

return 0;
}
Last edited on
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).
Last edited on
-9.25596e+061-9.25596e+061-9.25596e+061-9.25596e+061-9.25596e+061-9.25596e+061

This is what i am getting as a garbage value. and should the first number ( the one that tells how many lines) be my count? or a different varible
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.
Last edited on
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
const int size = 6;
double numbers[size];
int lines;
int count = 0;


ifstream inputfile;
ofstream outputfile;

inputfile.open("C:\\Users\\Nick\\Desktop\\Project2data\\proj2data.txt");

outputfile.open("C:\\Users\\Nick\\Desktop\\ProjectData\\Project.txt");

inputfile >> lines;

for (int i = 0; i < lines; i++)
{

while (lines < size && inputfile >> numbers[count])
count++;




for(count = 0; count < size; count++)
outputfile << numbers[count] << " ";
}









cin.ignore();
cin.get();

return 0;
}



Is this right? now its not outputting anything
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.
Last edited on
[code]
for (int i = 0; i < lines; i++)
{

while (lines < size && inputfile >> numbers[count])
count++;




for(count = 0; count < size; count++)
outputfile << numbers[count] << " ";
}
[code/]




it still wont output anything, I don't think it is entering this loop
while (lines < size && inputfile >> numbers[count])

Change "lines" to "count" here, and set count = 0 right before this line.

Try outputting the value in "lines" right before the loop. It should be 3. Otherwise something is wrong with your input file.
[code]
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
const int size = 6;
double numbers[size];
int lines;
int count = 0;


ifstream inputfile;
ofstream outputfile;

inputfile.open("C:\\Users\\Nick\\Desktop\\Project2data\\proj2 data.txt");

outputfile.open("C:\\Users\\Nick\\Desktop\\ProjectData\\Project.txt");

inputfile >> lines;

for (int i = 0; i < count; i++)
{

outputfile << lines;
count = 0;
while (count < size && inputfile >> numbers[count])
count++;




for(count = 0; count < size; count++)
outputfile << numbers[count] << " ";
}









cin.ignore();
cin.get();

return 0;
}
[code]

yea its still not outputting anything. I guess my input file is messed up
Topic archived. No new replies allowed.