I am working on an assignment and was wanting to know if one of u guys could let me know if I have 1. correct before I go to 2.

Here are the instructions...


1. read in the data then displayed the data.
2 Add the following
a. add a data structure to hold the data
b. read in the data from the input file
c. store the data in the data structure
d. write the data to a output file!


here is my code

#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;

//Start main
int main()
{

	string filename = "exam.txt"; // Create Var to hold file name
	char inputline[80]; // Buffer to hold line just read
	ifstream inFile; // Input file object


	// Open the input file.
	inFile.open("exam.txt");

	// Test for errors.
	if (!inFile)
	{
		cout << "Error opening " << filename << endl;
		exit(EXIT_FAILURE);
	}

	// Read through file & list names
	// Read and process the input file.

	while (!inFile.eof())
	{
		// Read the string from inFile.
		inFile.getline(inputline, 80, '\n');

		// Display the line.
		cout << inputline << endl;
	}

return 0;
}

I apologize for coming off as a complete idiot when i say this but I dont know much about c++ and not really sure what u mean

It was a link to another thread that explains why not to use eof() in your while loop condition. Click the link, read the thread and implement the change.

here is my problem...i am not able to make changes to the following code and i can only add from it and it seems like there is alot of missing information but could be wrong..

#include
#include
#include

using namespace std;


int main()
{

string filename = "names.txt"; // Create Var to hold file name
char inputline[80]; // Buffer to hold line just read
ifstream inFile; // Input file object


// Open the input file.
inFile.open("name.txt");

// Test for errors.
if (!inFile)
{
cout << "Error opening "
<< filename << endl;
exit(EXIT_FAILURE);
}

// Read through file & list names


// Read and process the input file.

while (!inFile.eof())
{

// Read the string from inFile.
inFile.getline(inputline, 80, '\n');


// Display the line.
cout << inputline << endl;




}


return 0;

}

First thing I noticed was:

string filename = "names.txt"; // Create Var to hold file name
   inFile.open("name.txt");

Why are you creating a variable and then not using it?

About your include "cmath", so far you don't really need it.

Part 1 works fine. I was thinking that you could simplify your code in the long run by reading everything into your data structure and then from there you can use a single loop to to display the contents to the screen and output it to a file. Otherwise you'll have to read the file again so that you can put the data into the data structure, which seems like unnecessary work if you ask me.

inFile.open(filename.c_str()); will open up the file using the filename (that c_str() method provides the C string representation)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.