# need help with ifstream

• 06-19-2005
stillwell
need help with ifstream
I have made a program that measures the speed of a shot on goal in soccer. I need it to read from a .txt file and write to another. My problem is that I need 2 simultanious readings from the file, and I have no idea how to do that.

Code:

```double skudhastighed (double s, double d) { double speed, distance, counterspeed = 0, counter = 0, hastighed, add;       speed = s;       distance = d;       counterspeed = speed;             while (counterspeed < (3600 - speed))                   {                   counterspeed = counterspeed + speed;                   counter++;                   }             counterspeed = 3600 - counterspeed;       add = counterspeed / speed;             distance = distance / 1000;       counter = counter + add;       hastighed = distance * counter;     return hastighed; }     int main() { double s, d; ifstream reading( "test.txt"); reading >> s, d; cout << setprecision(2) << fixed << skudhastighed (s, d); ofstream result( "results.txt", ios::app ); result << setprecision(2) << fixed << skudhastighed (s, d) << " Km/t" << endl; result.close(); cin >> s;     return 0; }```
and this is the text of the test.txt document
Code:

```1.11, 55.55 1.33, 44.66 1.76, 66.11```
• 06-19-2005
MathFan
What do you mean by two simultaneous readings?

What I spotted up til now:

1.
Code:

` reading >> s, d;`
That can't be right; Instead try:
Code:

`reading>>s>>d;`
2.
You mustn't have commas in your test file.

3.
Code:

`cin >> s;`
What are you trying to do here? If you want to wait for user to hit a key and then exit, do the following:
Code:

`cin.get();`
4.
I assume that you have all the necessary includes in your original code. If not here they are:
Code:

```#include <iostream> #include <fstream> #include <iomanip> using namespace std;```

5.
Yopu forgot to close reading file.
Code:

`reading.close();`
6.
If you want to read in ALL the values from the test.txt file, do the following:

Code:

```int main() { double s, d; ifstream reading( "test.txt"); ofstream result( "results.txt", ios::app ); //do you really need the ios::app? while(reading >> s>>d) { cout << setprecision(2) << fixed << skudhastighed (s, d); result << setprecision(2) << fixed << skudhastighed (s, d) << " Km/t" << endl; } reading.close(); result.close(); cin.get();         return 0; }```
• 06-19-2005
stillwell
1. That's my problem. I need to read in 2 numbers for every cycle from test.txt. Need to read in s and d.

2. Ok, but how do I write it then? With a space, like this:

Code:

`1.11 55.55`
3. Thanks, that is exactly what I'm trying to do.

4. Yea, I have the necessary includes

5. Thanks.

6. Doesn't seem to work. I want the program to read in 2 numbers from one line in test.txt, and then put the 2 numbers into s and d respectively.

Thanks for the help :)
• 06-19-2005
7stud
Quote:

k, but how do I write it then? With a space, like this:

1.11 55.55
The operator>> is programmed to stop reading in data when it encounters any whitespace(spaces, tabs, newlines).

Quote:

6. Doesn't seem to work.
What doesn't seem to work? If your code looks like this:
Code:

```int main() {         xxxaj;ldrjfs;dlfjsa;ldfjas;ldfj;sdaljf         asdl;fjas;dfjasld;jfsa;ldf         as;dfjlasd;lfjas;lj               return 0; }```
I wouldn't expect it to work, and as far as far as anyone knows, that is what your code looks like.
• 06-19-2005
MathFan
Quote:

Originally Posted by stillwell
6. Doesn't seem to work. I want the program to read in 2 numbers from one line in test.txt, and then put the 2 numbers into s and d respectively.

Hmmm.... It works when I try it... It reads the three lines into s and d and then outputs three values, as it is expected. Here is what the result.txt looks like on my computer:

Code:

```180.11 Km/t 120.84 Km/t 135.16 Km/t```
You should probably remove the ios::app. It appends the lines to the file (unless that is what you want). If you take it away, you will overwrite the file each time you open it.

Oh, ooops... My mistake, this line
Code:

`cout << setprecision(2) << fixed << skudhastighed (s, d);`
should look like this:
Code:

`cout << setprecision(2) << fixed << skudhastighed (s, d)<<endl;`
(just forgot the endl)
• 06-19-2005
stillwell
Yay, now it works. Thanks for your help, MathFan :D

Quote:

You should probably remove the ios::app. It appends the lines to the file (unless that is what you want). If you take it away, you will overwrite the file each time you open it.
I know. The effect is wanted :)
• 06-20-2005
ubern00ber
Code:

`counterspeed = counterspeed + speed;`
Don't bother writing that, just write
Code:

`counterspeed += speed;`
(-= to subtract)
this wont make any difference in your code, but it takes less time to write(Be as lazy as you can)