### Input file assignment.

Hi guys,

I just learned about input file, and while I sort of get the basic concept, I am still confused about how it's done. I don't understand what I have to do for my assignment. Apparently, beside the input file part, there's some calculation involved as well. Here's the assignment, can someone explain to me what I have to do?

http://i41.tinypic.com/2edxwk8.png

http://i44.tinypic.com/25sueqg.png
Well, how much of it have you done so far, which part are you stuck on?
This is how my program is supposed to look like.

http://i44.tinypic.com/e83jae.png

So far, I only did the declaring values, the first statement, and basic steps of file input and output. I don't know what exactly what I need to do with those math calculation or set up those input or output.

 123456789101112131415161718 #include #include #include #include using namespace std ; int main() { string FileName ; ifstream STATNUMS.DAT ; ofstream STATNUMS.out; cout << "My name" << setw(4) << "Class Section" << setw(4) << "Program 3"; return 0; }
Last edited on
I'm stuck. I really have no way of doing this assignment. It doesn't help that I have to do a bunch of Calculus homework and other C++ questions as well. :(
I think that there is an example of a very similar problem, and some of the solution, here: http://www.cplusplus.com/forum/beginner/113992/#msg622885
 12345678910111213141516171819202122232425262728293031323334353637383940414243 #include #include #include #include #include using namespace std ; int main() { string FileName ; ifstream fin; ofstream fout; cout << "(Name)" << setw(6) << "(Section)" << setw(14) << "(Assignment)" << "\n\n"; fin.open(FileName.c_str()); while (fin.fail()) { cout <<"Bad file name or location.\n" ; fin.clear(); cout <<"Enter file name and location: " ; getline(cin, FileName); fin.open( FileName.c_str()); } cout <<"Enter output file name and location: " ; getline(cin, FileName); fout.open(FileName.c_str()); if (fout.fail()) { cout << "Bad file name.\n"; exit(0); } fout.close(); return 0 ; }

This is all I could do at the moment, based on that sample he gave me. I don't know where else am I supposed to get the number values from. He posted a text file for assignment 4, but we are still on assignment 3. If that was indeed the file for the current assignment, I still have no clue what to do with it.
Last edited on
So these are the numbers in the text file that I have to use. I think I need to input these into a text file, and then do the calculation. Where do I even begin?

100 90 80
78
67

56 20 78 99 54 89
100 90
80
Well the first thing to do is to read the values from the file. Then do whatever processing is required according to the specifications of the task.
 12345678910111213141516171819202122232425262728293031323334 #include #include #include using namespace std; int main() { string filename = "input.txt"; ifstream fin(filename.c_str()); if (!fin) { cout << "Unable to open file: " << filename<< endl; return 1; } int total = 0; int count = 0; int number; while (fin >> number) { total += number; count++; } cout << "Number of values: " << count << endl; cout << "Total: " << total << endl; if (count > 0) cout << "Average: " << double(total)/double(count) << endl; return 0; }
 123 cout << "Unable to open file: " << filename<< endl; return 1; }

There is an error where it says << filename. Can you check that part?

 123 cout << "Total: " << total << endl; if (count > 0) cout << "Average: " << double(total)/double(count) << endl;

Is it okay to use space? My professor usually force us to use setw. Is it acceptable in this case?
Last edited on
Looks like #include <string> is missing, not sure how that happened.

When you write code for your own amusement, use whichever makes the most sense. I often use setw() too, particularly for columns of data.
Oh man... The assignment is due at the end of today, and I have been struggling with this for the last few days. I still can't get a firm grip with this assignment. This is my pathetic attempt at trying, based on a sample program the professor gave us, and still got an error. I don't even know what to do anymore.

 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 #include #include #include #include #include #include using namespace std; int main() { string FileName ; ofstream fout ; ifstream fin ; int int1, int2, int3, int4, int5, int6, int7, int8, int9, int10, int11, int12, int13, int14, int15; float sum, sum2, sumsquares, sum2squares, mean, deviation; cout <<"Enter input file name and location: " ; getline(cin, FileName); fin.open(FileName.c_str()); while (fin.fail()) { cout <<"Bad file name or location.\n" ; fin.clear(); cout <<"Enter file name and location: " ; getline(cin, FileName); fin.open( FileName.c_str()); } cout <<"Enter output file name and location: " ; getline(cin, FileName); fout.open(FileName.c_str()); if (fout.fail()) { cout << "Bad file name.\n"; exit(0); } sum = int1 + int2 + int3 + int4 + int5; sum2 = int6 + int7 + int8 + int9 + int10 + int11 + int12 + int13 + int14 + int15; sumsquares = (sqrt(int1)) + (sqrt(int2)) + (sqrt(int3)) + (sqrt(int4)) + (sqrt(int5)); mean = sum; // divided by what?// deviation = sqrt ( (sumsquares)); //divided by what?// - (pow *(mean,2)); fout <<"My first output file.\n" ; fout << "input.txt" << endl; cout << sum; fout <<"Part 2 output file.\n" ; fout << "input.txt" << endl ; cout << sum2; float i ; fin >> i ; // initialize fout << fixed << setprecision(3)<> i ; // reset condition } fout.close(); return 0; }

:(
Last edited on
Well, most of this program is just making sure the files are ok. The important stuff starts at line 58:
 585960616263646566 float i ; fin >> i ; // initialize fout << fixed << setprecision(3)<> i ; // reset condition }

Please don't use eof() in a while condition, it will only end in tears.
That code should look like this:
 123456 float i ; fout << fixed << setprecision(3)<> i) // get value from file { fout << setw(10) <

Note, lines 43 to 47 give a clue as to what you would like the program to do, but don't actually achieve anything, as there is no data read from the file at that point.

What you need to do is to count how many values have been read, by adding 1 to a count, inside the while loop, and accumulate totals as each value is read too.
Something like this perhaps:
 12345678910111213141516 double i ; int count = 0; double total = 0; double sumsquares = 0; while (fin >> i) { fout << setw(10) <

Note, I'd recommend double rather than float for general use, as it is more precise.
Last edited on
What I was trying to do is use these numbers to do the calculation in the screenshot.

100 90 80
78
67

56 20 78 99 54 89
100 90
80

I put them into a text file named "input", and call them out using fout << "input.txt"; Not sure if what I did was correct.

When I replaced my code with yours, I got an error around this part

 123456 float i ; fout << fixed << setprecision(3)<> i) // get value from file { fout << setw(10) <

It says >> and i are illegal for class. This is the entire thing so far, is the format right?

 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 #include #include #include #include #include #include using namespace std; int main() { string FileName ; ofstream fout ; ifstream fin ; double i ; int count = 0; double sum = 0; double deviation = 0; double total = 0; double sumsquares = 0; while (fin >> i) { fout << setw(10) <> i) // get value from file { fout << setw(10) <
Last edited on
Your code gives me the following compilation error:
 [Error] conflicting declaration 'float i' [Error] 'i' has a previous declaration as 'double i'

At line 81, float i is trying to declare another variable with the same name as at line 14 double i ;
After I deleted line 81, the code compiled successfully.

Note, you may find it useful to look at this thread, seems to be on exactly the same question:
http://www.cplusplus.com/forum/beginner/114636/

In your code above, things are done in the wrong order. At line 22 to 31, you do some stuff with the input file, then after that, you ask the user what is the name of the file, and then open it.

You need to move the code around, so you don't start trying to read from it until after it has been opened.
Last edited on
Topic archived. No new replies allowed.