Can't get code to output numbers in input file

Write your question here.

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
  
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;

const int DATA = 30;

int main()

{
    ifstream fin; // Blaster Size Galactic Standard Units input stream
    ofstream fout; // Blaster Size Galactic Standard Units output stream
    string title; // the upper limit on the number of values can be entered
    double numbers; // Parrellel arreas = the same size, refering to the same size
    double minLow, // the minimum measure
        maxHigh, // the maximum measure
        curLow, //current low
        currHigh, //current high
        lowsSum, //sum of lows
        highSum; //sum of highs
    int count; // Number of data

    fin.open("/home/caitlin/Documents/Blaster.in");

    fin >> numbers;
    fin.ignore('\n', 100);
    getline(fin, title);

    minLow = curLow;
    maxHigh = currHigh;

    while (!fin.eof()) {
        if (minLow < curLow)
            minLow = curLow;
        else if (maxHigh > currHigh)
            maxHigh = curLow;
        lowsSum += curLow;
        highSum += currHigh;

        fin >> curLow >> currHigh;
        count++;
    }

    fout << setprecision(2) << fixed << showpoint << title << endl;
    fout << "==========================================================================" << endl;

    fout << "\nThe minimum measure is: " << maxHigh << endl;
    fout << "The maximum measure is: " << minLow << endl;
    fout << "The trimmed mean is: " << endl;

    fin.close();

    return 0;
}


Bellow is what the inputed data file is

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
Blaster Size (Galactic Standard Units) - Sample from Corscanti Vendors
17.9
24.6
10.3
19.2
6.3
20.6
15.2
22.2
20.2
23.6
7
26.5
26.9
5.7
10.2
10.7
5.4
23.6
17.1
17.8
5.1
24.6
12.3
13.1
23
8.5
12.1
15.7
16.2
22.6
18.6
23.3
18.8
18.5
6.9
16.7
18.4
Last edited on
try changing line 37 to

if (maxHigh > currHigh)
@SamuelAdams still no output. I need the numbers to display...bellow line 47
Here you can see what I did to test your code.

PS. I didn't check to see if the numbers are right, just that it had output.

PSS. next time comment your code and I'd know what your trying to do with that if else statement.


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
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;

const int DATA = 30;

int main()

{
    ifstream fin; // Blaster Size Galactic Standard Units input stream
    ofstream fout; // Blaster Size Galactic Standard Units output stream
    string title; // the upper limit on the number of values can be entered
    double numbers=0; // Parrellel arreas = the same size, refering to the same size
    double minLow=0, // the minimum measure
        maxHigh=0, // the maximum measure
        curLow=0, //current low
        currHigh=0, //current high
        lowsSum=0, //sum of lows
        highSum=0; //sum of highs
    int count=0; // Number of data

    fin.open("/temp/Blaster.in");
 if (fin.is_open())
 {cout << "File Good";} // testing
 else
 {cout << "File Bad"; return 1;}// testing


//     fin.ignore('\n', 100);
   getline(fin, title);
    cout << title << endl;
// 	fin >> numbers;
    cout << numbers << endl; // testing

    minLow = curLow;
    maxHigh = currHigh;

    while (!fin.eof()) {
        if (minLow < curLow)
            {minLow = curLow;
            cout << minLow << endl; // testing
			}
            
        if (maxHigh > currHigh)
            {
			maxHigh = curLow;
            cout << maxHigh << endl; // testing
			}
        lowsSum += curLow;
        highSum += currHigh;

        fin >> curLow >> currHigh;
        count++;
    }

    fout << setprecision(2) << fixed << showpoint << title << endl;
    fout << "==========================================================================" << endl;

    fout << "\nThe minimum measure is: " << maxHigh << endl;
    fout << "The maximum measure is: " << minLow << endl;
    fout << "The trimmed mean is: " << endl;

    fin.close();

    return 0;
}
Last edited on
@SamuelAdams I made a whole bunch of changes... But not I can't get it to print the entire line of integers...Just one random number

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
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <stdio.h>
using namespace std;

const int DATA = 100;   // No more than 

int main()

{
    ifstream fin;        // Blaster Size Galactic Standard Units input stream
    string getTitle;     // Title of ifstream file
    double getMeasures;  // All the measures in the data set
    int determinesMax;   // Determines Max numeric number in file
    int findMin;         // Determines Min numeric unmber in file

    fin.open("Blaster.in");
    if (fin.is_open()) {
        cout << "";
    }                        // testing

    else {
        cout << "File Bad";
        return 1;
    } // testing

    getline(fin, getTitle);  // Get the string of the title

    while (!fin.fail() && !fin.eof()) { // Calculate unknown amount of values in file

        fin >> getMeasures;  // Grab largest numeric value
        if (getMeasures > determinesMax) // Find Max value
            determinesMax = getMeasures;

        if (getMeasures < findMin) // Grab lowest numeric value
            findMin = getMeasures; // Find min Value
    }

    cout << getTitle << endl; // Print String
    cout << "==========================================================================" << endl; // Text
    cout << getMeasures << endl; // Print measures 
    
    // output should be floating point (adding a 0 to the end of all values
    // Need to print all number in 10 columns of width 8. ???? 

    cout << "\nThe minimum measure is: "  << setprecision(2) << findMin << endl;
    cout << "The maximum measure is: "  << determinesMax << endl;
    cout << "The trimmed mean is: " << endl;  // calcTrimmedMean - Has to find a trimmed mean....Removing largest and smallest integer

    fin.close();

    return 0;
}


It should print these number with a 0 at the end of each one...I've made comments about how it should be displayed in columns but I can't figure it out for the life of me

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
17.9
24.6
10.3
19.2
6.3
20.6
15.2
22.2
20.2
23.6
7
26.5
26.9
5.7
10.2
10.7
5.4
23.6
17.1
17.8
5.1
24.6
12.3
13.1
23
8.5
12.1
15.7
16.2
22.6
18.6
23.3
18.8
18.5
6.9
16.7
18.4
Your getting closer.
This doesn't work for the first number and I'm not sure why.

To find the trimmed mean, your going to have to keep up with the largest number that is less than determinesMax. Same for the min number.

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
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <stdio.h>
using namespace std;

const int DATA = 100;   // No more than 

int main()
{
    ifstream fin;        // Blaster Size Galactic Standard Units input stream
    string getTitle;     // Title of ifstream file
    double getMeasures;  // All the measures in the data set
    double determinesMax=0;   // Determines Max numeric number in file
    double findMin=999999999;         // Determines Min numeric unmber in file

	double almostMax=0;  // Example for trimmed mean.

    fin.open("Blaster.in");
    if (fin.is_open()) 
	{

	    getline(fin, getTitle);  // Get the string of the title
	    cout << getTitle << endl; // Print String
	    cout << "==========================================================================" << endl; // Text

	    while (!fin.fail() && !fin.eof())
			{ // Calculate unknown amount of values in file
		        fin >> getMeasures;  // Grab largest numeric value

		        if (getMeasures > determinesMax) // Find Max value
		            {
					almostMax=determinesMax; // Example for trimmed mean.
					determinesMax = getMeasures;
					}
		
		        if (getMeasures < findMin) // Grab lowest numeric value
					{
			            findMin = getMeasures; // Find min Value
			    	}
		    cout  << getMeasures << setprecision (2) << fixed  << endl; // Print measures 
		    }
	}
	
    else 
	{
        cout << "File Bad";
        return 1;
    }
    // output should be floating point (adding a 0 to the end of all values
    // Need to print all number in 10 columns of width 8. ???? 

    cout << "\nThe minimum measure is: "  << setprecision(2) << findMin << endl;
    cout << "The maximum measure is: "  << determinesMax << endl;
    cout << "The trimmed mean is: " << endl;  // calcTrimmedMean - Has to find a trimmed mean....Removing largest and smallest integer

	cout << "The second largest number should be " << almostMax << endl;
    fin.close();

    return 0;
}
Fixed the first number not showing a 0.

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

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <stdio.h>
using namespace std;

const int DATA = 100;   // No more than 

int main()
{
    ifstream fin;        // Blaster Size Galactic Standard Units input stream
    string getTitle;     // Title of ifstream file
    double getMeasures;  // All the measures in the data set
    double determinesMax=0;   // Determines Max numeric number in file
    double findMin=999999999;         // Determines Min numeric unmber in file

	double almostMax=0;  // Example for trimmed mean.

    fin.open("Blaster.in");
    if (fin.is_open()) 
	{

	    getline(fin, getTitle);  // Get the string of the title
	    cout << getTitle << endl; // Print String
	    cout << "==========================================================================" << endl; // Text

	    while (!fin.fail() && !fin.eof())
			{ // Calculate unknown amount of values in file
		        fin >> getMeasures;  // Grab largest numeric value

		        if (getMeasures > determinesMax) // Find Max value
		            {
					almostMax=determinesMax; // Example for trimmed mean.
					determinesMax = getMeasures;
					}
		
		        if (getMeasures < findMin) // Grab lowest numeric value
					{
			            findMin = getMeasures; // Find min Value
			    	}
		    cout  << setprecision (2) << fixed << getMeasures  << endl; // Print measures 
		    }
	}
	
    else 
	{
        cout << "File Bad";
        return 1;
    }
    // output should be floating point (adding a 0 to the end of all values
    // Need to print all number in 10 columns of width 8. ???? 

    cout << "\nThe minimum measure is: "  << setprecision(2) << findMin << endl;
    cout << "The maximum measure is: "  << determinesMax << endl;
    cout << "The trimmed mean is: " << endl;  // calcTrimmedMean - Has to find a trimmed mean....Removing largest and smallest integer

	cout << "The second largest number should be " << almostMax << endl;
    fin.close();

    return 0;
}
PS. Forgot to mention I moved some things around. It's the same code but I think it has better flow control.

and thanks for the comments in code.
Last edited on
I was reading your comments and made one more update to add columns.

So here's my advise summed up.
If you open a file, check for success.
Add cout statements to your code for error checking. You can comment them out when your happy all is correct.
Your code is not large but you had several errors that compounded finding the solution.
Try writing a few lines at a time (like a section) and testing the code before you add the next part. Should make it easier to troubleshoot.
When you declare a int or double, always always always set the value to 0, 1, or what ever makes sense to start with.
I may be wrong, but you seem to be confused on how to use a int and double. Read a little more about that or have someone explain the finer details just to make sure you have a good foundation going forward.


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

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <stdio.h>
using namespace std;

const int DATA = 100;   // No more than 

int main()
{
    ifstream fin;        // Blaster Size Galactic Standard Units input stream
    string getTitle;     // Title of ifstream file
    double getMeasures;  // All the measures in the data set
    double determinesMax=0;   // Determines Max numeric number in file
    double findMin=9999999999;         // Determines Min numeric unmber in file

	double almostMax=0;  // Example for trimmed mean.
	int count=0;
	
    fin.open("Blaster.in");
    if (fin.is_open()) 
	{

	    getline(fin, getTitle);  // Get the string of the title
	    cout << getTitle << endl; // Print String
	    cout << "=============================================================================" << endl; // Text

	    while (!fin.fail() && !fin.eof())
			{ // Calculate unknown amount of values in file
		        fin >> getMeasures;  // Grab largest numeric value

		    if (getMeasures > determinesMax) // Find Max value
		        {
				almostMax=determinesMax; // Example for trimmed mean.
				determinesMax = getMeasures;
				}
		
		    if (getMeasures < findMin) // Grab lowest numeric value
				{
			    findMin = getMeasures; // Find min Value
			  	}

			if (count==10)		// Print measures 
				{
				cout  << endl;
				if (getMeasures<10){cout << " ";}
				cout  << setprecision (2) << fixed << getMeasures  << "\t"; // Print measures
				count=0; 
				} 
			else
				{
				if (getMeasures<10){cout << " ";}
				cout  << setprecision (2) << fixed << getMeasures  << "\t"; // Print measures 
				}
		    }
	}
	
    else 
	{
        cout << "File Bad";
        return 1;
    }
    // output should be floating point (adding a 0 to the end of all values
    // Need to print all number in 10 columns of width 8. ???? 

    cout << "\nThe minimum measure is: "  << setprecision(2) << findMin << endl;
    cout << "The maximum measure is: "  << determinesMax << endl;
    cout << "The trimmed mean is: " << endl;  // calcTrimmedMean - Has to find a trimmed mean....Removing largest and smallest integer

	cout << "The second largest number should be " << almostMax << endl;
    fin.close();

    return 0;
}
Thank you for all your help, it's greatly appreciated!
Topic archived. No new replies allowed.