## ans025 1

Here is the actual homework problem. I am stuck on parts 1 and 2 and I really don't know how to make this right. Any help is greatly appreciated.

1. Write a C++ program that reads following data from an input file called “in.txt”, And stores them to an array called Grade, you need to use a function to read this data:
89 56 75 48 34 93 62

2. Display this data on screen to make sure you read data correctly
a. You must use a function to display this data

3. Write a function that gets the array of grades (numbers you read from input file), Size of array as two value parameters, and returns sum of those grade, as reference parameter to main, then print it to the output file.

4. Write a function to sort this array. Now print unsorted & sorted array to an output file, called “output.txt”

5. Write a function to search for one of the grade above (like 34), then print the location of the item and say you found it

#include <iostream>
#include <fstream>

using namespace std;

//===========  Prototypes for functions =======================

int main()

{

cout<<"These are the grades stored in the array: "

//========== Functions Definition Read File  ===============================

{

ofstream      out;                 //Creates an instance of ofstream

ifstream       in;                  //Creates an instance of ifstream

in.open("c:\\in.txt");      //opens inputfile.txt
out.open("c:\\out.txt");    //Opens outputfile.txt

in.close();                              //Close input file

out.close();                             //Close outputfile

}

}

I keep getting the error:
error C2601: 'Read' : local function definitions are illegal

I don't know what else is wrong pass that point so could you help me figure that out. Thank you.

## abhimanipal 91

1. In the code that you have posted, you have defined the function read in the function main ..... Put the brackets properly

2. Check how you have called the read function. Is that the correct way to call a function

3. I would recommend changing the name of the function from read to read_file or something of that sort . Read is a in built function to read data from a file... You may get some messy errors later. Why take chance ?

## Ancient Dragon 5,243

Your Read() function does not need an ofstream object because that is intended to write something. Read() function should only use ifstream to read the data.

After opening the file you have to add a few lines to read each of the numbers in the file. Opening the file doesn't do that for you -- you have to write it yourself. Use the >> operator to do that.

## ans025 1

Thank you for your replies. I have made some changes and here is my code:

#include <iostream>
#include <fstream>

using namespace std;

//===========  Prototypes for functions =======================

int main()

{
int i;

cout<<"These are the grades stored in the array: ""\n";
for (i=0; i<7; i++){
cout<<Read_file<<"\t""\n";       //Displays the elements of the array
}
}
//========== Functions Definition Read File  ===============================

{

ifstream	  indata;				// indata is like cin

indata.open("in.txt"); // opens the file
if(!indata) { // file couldn't be opened
cerr << "Error: file could not be opened" << endl;
exit(1);

ofstream      out;                  //Creates an instance of ofstream

out.open("out.txt",ios::app);    //Opens outputfile.txt

indata.close();                              //Close input file

out.close();                             //Close outputfile

return 0;

}
}

Regarding the previous post, I am supposed to have these values print into another output file. Is it not correct if I'm doing this? Each question at the top ask for it to print to an output file.

What the problem is this time:
I'm having trouble opening the .txt file. I've entered the correct name for the .txt file which is saved under the same folder, but whenever I compile and run, it will just show some random numbers under the function called Read_file something like 00EA1037 it changes every time.

I'm also getting this error:
warning C4715: 'Read_file' : not all control paths return a value

I know that means it doesn't return a value but I don't understand why. Please help. Thank you.

I'm sorry if these questions seem basic but I am very new to C++ programming.