heey guys.. i have this h.w.. i solved the first part which is based on the second part.. but not sure whether i did it right or not.. can someone check plz..

this is the Q.. i only did the first part!

First create a one dimensional array called numbers that contains the following data
(Initialize the array with the numbers listed below).
6,7,-3,-4,5,-5,10
Output the array data to a file called data.txt.
==============================================
Then:
1) Calculate how many items are there in the file.
2) Calculates the number of the positive numbers and negative numbers
3) Calculate the sum of the positive numbers only.
4) Get a target from the user to search for; e.g. 7 (as an input from the screen)
5) Sort the array’s elements in ascending order and follow exactly the file snapshot next page.
6) Then append the result of 1) to 5) to the same file.

==============================================
now i created the input file and added the numbers to it.. but after running the program.. the numbers didn't appear in the output file.. dunnu why.. is smthn wrong with my code?

``````#include <iostream>
#include <cstdlib>
#include <fstream>

using namespace std;

int main ()
{

int NUMBERS [7] = {6,7,-3,-4,5,-5,10};

//declare file:
ofstream data_out;

//initialize file:
data_out.open ("data.txt");

//check file:

if (data_out.fail())
cout<<"Exit data";

//use file:

for (int i=0;i>=6;i++)
{
data_out<<NUMBERS [i];
}

//close file:
data_out.close ();

return 0;

}``````
3
Contributors
6
Replies
7
Views
9 Years
Discussion Span
Last Post by ArkM

Firstly from the question arent you supposed to read from "data.txt" and not "read_data.txt"

Secondly. I am quiet confused with your code.

You should first post all the Numbers into the file and then read in from the file again to perform math calculations ?

i dunnu.. i did from both.. but didn't work..
Is something wrong with my code?? i don't know

Let's start from the real code...
See for loop condition: `i >= 6` . In other words, while i is greater than or equal to 6. The initial i value is 0...

This code confuses me . I donot understand whether read_data.txt has numbers in it or According to the problem statement. The file must be filled.

i filled th file read_data first and no output was shown in the data file.. it's mistake i know.. then i deleted what i filled and filled data instead.. but nothing appeared in read_data :S

i filled th file read_data first and no output was shown in the data file.. it's mistake i know.. then i deleted what i filled and filled data instead.. but nothing appeared in read_data :S

You wrote nothing to the read_data file!!! Read my previous post again!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.