file input

1)number of integer inputs from a file and store them in a vector
1
2
3
4
5
6
ifstream inputFile("xxx.txt");
if (inputFile) {        
    double value;
    while ( inputFile >> value ) {
        fall.ph(value);
    }

2) I have to find harmonic mean to first and last number
 
harmonic_mean= (2)/((1/double(a))+(1/double (b))) ;

3)I have to sort them in desending order
1
2
3
4
5
6
7
8
9
10
11
12
for (int k = 0; k < w.size(); k++)
		{
			for (int i = 0; i < w.size() - 1; i++){
				if (w[i] > w[i + 1])
				{
					temp = w[i];
					w[i] = w[i + 1];
					w[i + 1] = temp;
					finished = false;
				}
			}
		}

4)I have input the file name in the console like input "xx.txt" and it will create a new file "xrw" where it will display hmean and desending order

getline.xrw
xrw.c_str()


whenever I put them all together they never work
Last edited on
Hello mary9734,

For the most part I see little wrong with the code bits you have posted. What I do notice is:

1. I am guessing that "fall" is an instance of a class or struct and that "ph(value)" is a member function to set the value of a member variable of the class or struct. The problem I see here is that "fall" is only one instance of the class or struct and each time through the loop you are overwriting the variable, so by the end of the file "fall" will contain the last value read. I do not think that is what you want. After "fall.ph(value);" you might want to put that class or struct into a vector, array or list to store it for later use. This way the each element of the vector will be a complete class or struct that you can access.

2. Will work as is, but I would suggest using "static_cast<double> (a)" as it is more C++ compliant.

3. The code looks fine although I have no idea what "w" is or how it is defined or what is in "w", so I do not know if it will work.

4. Hard to comment here without knowing what you did.

Do not be afraid to post all the code. it makes it easier to figure out what is going wrong.

Hope that helps for now,

Andy

P.S. An example of the input file you use would be helpful. Five to ten lines if the file is big, Just enough to test with.
Last edited on
Here my full code.

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
#include<iostream>
#include<fstream>
#include<string>

using namespace std;


int main(){
string line;
double x;

getline.myfile
myfile.c_str()
if (myfile.is_open()){
        while(myfile.good()){
            myfile>>x;
score= (2)/((1/double(a))+(1/double (b))) ;
   int a[50],n=10,i,j,ting;
 for(i=1;i<n;++i)
    {
        for(j=0;j<(n-i);++j)
            if(a[j]>a[j+1])
            {
                ting=a[j];
                a[j]=a[j+1];
                a[j+1]=ting;
            }
    }

    for(i=0;i<n;++i)
        cout<<" "<<a[i];
}
int c[50], n=[5], o, j, wing;
for (i=1; i>n; i++){
    for (c[j]<c[j+2])
    if (c[j]<a[j+1]){
        wing=c[j];
        c[j]=c[j+1]
        c[j+1]=wing;
    }
    }
for (i=1;i<n;++i)
}}
cout<< "Harmonic mean: "<<score<<"\n";
    cout<< "Sorted Numbers:"<<snap<<"\n";
        }
    myfile.close();
    }

else cout<<"unable to open file";

return 0;
}
Topic archived. No new replies allowed.