Thread: I need some help...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    18

    I need some help...

    I would like to know how to add up the integers after the id number.

    For instance:
    12345 = id number
    19
    21
    22
    23
    -1
    54321
    23
    15
    -1

    my code so far is
    Code:
    #include<iostream>
    #include<fstream>
    
    //void AverageSpeed(double, double);
    
    using namespace std;
    
    const int sentinel = -1;
    
    int main(){
    int count;
    int time;
    
    int time1;
    int time2;
    int time3;
    int time4;
      
    ifstream input;
    ofstream out;
    
    int id;
    
    double velocity;
    int distance = 1;
    double average;
    double sum;
    
    
    int timesum[4];
    
    
    input.open("race1.dat");
    out.open("raceresults.txt");
    
    cout << "Vehicle Id          Average Speed (Mph)          Status" << endl;
    cout << "*******************************************************" << endl;
    
    out  << "Vehicle Id          Average Speed (Mph)          Status" << endl;
    out  << "*******************************************************" << endl;
    
    input >> id;
    while (!input.eof())
    {
      cout << id << endl;
      out << id << endl;
      
    input >> time;
    	count = 0;
       while (time != -1)
       {	
         cout << time << endl;	
         
         count ++;
    
    	input >> time;
       }
       input >> id;
    }
    
    
    
    
    
    input.close();
    out.close();
    
    return 0;
    }
    
    
    /***********************************/
    //void AverageSpeed(double velocity, double time)
    //{
     //velocity =  
      
    //average = time/4;

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,359
    Use += to add the current value of time to the current sum. Don't forget to set/reset sum to 0 when you start with a new id.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    34,310
    Doesn't anybody bother to
    - read the FAQ
    - indent their code
    - preview before they post?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed