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
Anyone, please? :(
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cmath>

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <fstream>
#include <cstdlib>
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;
}
1
2
3
cout << "Unable to open file: " << filename<< endl;
		return 1;
	}


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

1
2
3
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.
You need to follow your professor's guidelines in assignments or you may get a lower grade.

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <cmath>
#include <iomanip>
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)<<showpoint << left;
	while (!fin.eof())   // check end of file
	{
		fout << setw(10) <<i << setw(20) <<i*i << setw(20) << i*i*i <<endl ;
		fin >> 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:
58
59
60
61
62
63
64
65
66
	float  i ;
	fin >> i ;  // initialize 
	fout << fixed << setprecision(3)<<showpoint << left;
	while (!fin.eof())   // check end of file
	{
		fout << setw(10) <<i << setw(20) <<i*i << setw(20) << i*i*i <<endl ;
		fin >> i ;	// reset condition

	}


Please don't use eof() in a while condition, it will only end in tears.
That code should look like this:
1
2
3
4
5
6
    float  i ;
    fout << fixed << setprecision(3)<<showpoint << left;
    while (fin >> i)   // get value from file
    {
        fout << setw(10) <<i << setw(20) <<i*i << setw(20) << i*i*i <<endl ;
    }


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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    double  i ;

    int count = 0;
    double total = 0;
    double sumsquares = 0;
    
    while (fin >> i) 
    {
        fout << setw(10) <<i << setw(20) <<i*i << setw(20) << i*i*i <<endl ;
        
        count++;
        total += i;
        sumsquares += i*i;
    }
    
    double mean = total / count;


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

1
2
3
4
5
6
float  i ;
    fout << fixed << setprecision(3)<<showpoint << left;
    while (fin >> i)   // get value from file
    {
        fout << setw(10) <<i << setw(20) <<i*i << setw(20) << i*i*i <<endl ;
    }


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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <cmath>
#include <iomanip>
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 << setw(20) <<i*i << setw(20) << i*i*i <<endl ;
        
        count++;
        total += i;
        sumsquares += i*i;
    }
    
    double mean = total / count;

	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);
	}

	sumsquares = total;
	mean = sum / total; 
	deviation = sqrt ( ( (sumsquares) / total) ) - pow(mean, 2); 

	fout <<"My first output file.\n" ;
	fout << "input.txt" << endl;
	
	cout << "Number of values read: " << total << "\n\n";
	cout << "Mean of the values: " << mean << "\n\n";
	cout << "Standard deviation using method 1: " << deviation << "\n\n";
	cout << "Greatest value: " << total << "\n\n"; //what should I put here?//
	cout << "Least value: " << total << "\n\n"; //What should I put here?//

	fout <<"Part 2 output file.\n" ;
	fout << "input.txt" << endl ;

	cout << "Number of values read: " << total << "\n\n";
	cout << "Mean of the values: " << mean << "\n\n";
	cout << "Standard deviation using method 1: " << deviation << "\n\n";
	cout << "Greatest value: " << total << "\n\n"; //what should I put here?//
	cout << "Least value: " << total << "\n\n"; //What should I put here?//

    float  i ;
    fout << fixed << setprecision(3)<<showpoint << left;
    while (fin >> i)   // get value from file
    {
        fout << setw(10) <<i << setw(20) <<i*i << setw(20) << i*i*i <<endl ;
    }

	fout.close();

    return 0;
}
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.