New to programming, need help with totaling integers

This is a discussion on New to programming, need help with totaling integers within the C++ Programming forums, part of the General Programming Boards category; I am writing a program that will take in data from a file called "songs.dat" and uses that data to ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    15

    New to programming, need help with totaling integers

    I am writing a program that will take in data from a file called "songs.dat" and uses that data to output the song length in total seconds (the integers on "song.dat"), the song length in minutes and seconds, and the total minutes and seconds of all songs up until that point. My problem is getting the total length of all of the songs to work, at the moment it is just saying that the total of all of the songs is whatever the length in minutes and seconds the current song is. The function "int totalTime (int time)" is supposed to be calculating the total time in seconds (ie if song.dat reads 60 60 120, the total time in seconds is 240) and the functions totalTime_min and totalTime_sec are supposed to be taking the total value and calculating minutes and seconds out of that value, but something in there is not working the way that it should be. I have toyed around with it for an hour and am hoping that someone here can help me out.

    sample output:

    http://imageshack.us/a/img28/3487/sc...7oct131807.jpg


    Code:
    #include <iostream>
    #include <fstream>      // file I/O
    #include <cstdlib>      // exit
    #include <iomanip>      // setw
    using namespace std;
    
    void print_heading ();
    
    void output (int time);
    
    int songTime_min (int time);
    
    int songTime_sec (int time);
    
    int totalTime (int time);
    
    int totalTime_min (int time);
    
    int totalTime_min (int time);
    
    void open_files (ifstream& filein);
    
    int main ()
    {
    
      ifstream file_stream;
      int num;
    
      open_files (file_stream);
    
      print_heading ();
    
      while (file_stream >> num)
      {
        output (num);
      }
    
    }
    
    int songTime_min (int time)
    {
    
      return (time /60);
    
    }
    
    int songTime_sec (int time)
    {
      return (time % 60);
    
    }
    
    int totalTime (int time)
    {
    
      int total = 0;
    
      total = total += time;
    
      return (total);
    
    }
    
    int totalTime_min (int total)
    {
    
      return (total / 60);
    
    }
    
    int totalTime_sec (int time)
    {
    
      int total = 0;
    
      total = total += time;
    
      return (total % 60);
    
    }
    
    void print_heading ()
    {
    
      cout << endl << endl;
      cout << "Song            Song  Time           Total  Time\n";
      cout << "length        Minute  Seconds      Minutes  Seconds\n";
      cout << "---------------------------------------------------\n";
    
    }
    
    void output (int time)
    {
    
      cout.setf(ios::left);
    
      cout << setw (5) << time;
    
      cout.unsetf(ios::left);
    
      cout << setw(13) << songTime_min (time);
    
      cout << setw(7) << songTime_sec (time);
    
      cout << setw(15) << totalTime_min (time);
      cout << setw(7) << totalTime_sec (time) << endl;
    
    }
    
    void open_files (ifstream& filein)
    {
    
      filein.open ("songs.dat");
    
      if (filein.fail())
      {
        cout << "File fail to open \n";
        exit(1);
      }
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,825
    Your total keeps going out of scope.

    You need a total which preserves its value from one call to output to the next.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Just began programming and having trouble with integers
    By sfkc1588 in forum C Programming
    Replies: 7
    Last Post: 09-24-2011, 05:12 PM
  2. Still have a totaling issue after modifying code
    By scmurphy64 in forum C Programming
    Replies: 5
    Last Post: 09-30-2009, 06:54 PM
  3. Getting an error when totaling table
    By scmurphy64 in forum C Programming
    Replies: 3
    Last Post: 09-30-2009, 02:35 PM
  4. Turbo C programming - integers in C
    By unregistered in forum C Programming
    Replies: 4
    Last Post: 02-03-2002, 01:44 AM
  5. Totaling up numbers in a for loop?
    By musayume in forum C Programming
    Replies: 3
    Last Post: 11-21-2001, 04:23 PM

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