need help with ifstream

This is a discussion on need help with ifstream within the C++ Programming forums, part of the General Programming Boards category; I have made a program that measures the speed of a shot on goal in soccer. I need it to ...

  1. #1
    Registered User stillwell's Avatar
    Join Date
    Aug 2004
    Posts
    80

    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

  2. #2
    Registered User MathFan's Avatar
    Join Date
    Apr 2002
    Posts
    190
    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;
    }
    Last edited by MathFan; 06-19-2005 at 04:47 AM.
    The OS requirements were Windows Vista Ultimate or better, so we used Linux.

  3. #3
    Registered User stillwell's Avatar
    Join Date
    Aug 2004
    Posts
    80
    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

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    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).

    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.
    Last edited by 7stud; 06-19-2005 at 07:56 AM.

  5. #5
    Registered User MathFan's Avatar
    Join Date
    Apr 2002
    Posts
    190
    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)
    The OS requirements were Windows Vista Ultimate or better, so we used Linux.

  6. #6
    Registered User stillwell's Avatar
    Join Date
    Aug 2004
    Posts
    80
    Yay, now it works. Thanks for your help, MathFan

    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
    Last edited by stillwell; 06-19-2005 at 10:17 AM.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    4
    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)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple ifstream Question
    By Paul22000 in forum C++ Programming
    Replies: 8
    Last Post: 12-05-2008, 05:34 PM
  2. store data from ifstream and store in link list
    By peter_hii in forum C++ Programming
    Replies: 2
    Last Post: 10-26-2006, 09:50 AM
  3. ifstream
    By Wraithan in forum Tech Board
    Replies: 3
    Last Post: 09-24-2006, 02:26 AM
  4. ofstream and ifstream for searching and writing
    By johnnyd in forum C++ Programming
    Replies: 6
    Last Post: 03-17-2003, 08:34 AM
  5. Ifstream Problems
    By IlUomo in forum Windows Programming
    Replies: 1
    Last Post: 04-24-2002, 01:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21