Music CD burning project

I'm trying to write a music cd burning program but I'm getting a line before each output line in error. Any suggestions?


#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{

int songNumber;
int totalMinutes;
int totalSeconds;
int current;
int newMinutes;
int newSeconds;
int newtotalMinutes = 0;
int newtotalSeconds = 0;
int timeUsed =0;
int minLeft =0;
int secLeft =0;
int totalTime;

ifstream inData;
ofstream outData;
inData.open("songs.dat");
outData.open("output.dat");


outData << "Song" << setw(20)
<< "Song Time" << setw(22)
<< "Total Time" << endl;
outData << "Number" << setw(12)
<< "Minutes" << setw(10)
<< "Seconds" << setw(12)
<< "Minutes" << setw(10)
<< "Seconds" << endl;
outData << "------" << setw(12)
<< "-------" << setw(10)
<< "-------" << setw(12)
<< "-------" << setw(10)
<< "-------" << endl;


totalMinutes = 0;
totalSeconds = 0;
newtotalMinutes = totalMinutes;
newtotalSeconds = totalSeconds;

songNumber = 1;

while (inData)
{
inData >> current;

if (!inData)
{
break;
}


newMinutes = (current/60);
newSeconds = (current % 60);
totalMinutes = newMinutes;
totalSeconds = newSeconds;
newtotalMinutes += totalMinutes;
newtotalSeconds += totalSeconds;

if(newtotalSeconds > 59)
{
newtotalMinutes += 1;
newtotalSeconds -= 60;
}



outData << songNumber << setw(11)
<< newMinutes << setw(11)
<< newSeconds << setw(11)
<< newtotalMinutes << setw(11)
<< newtotalSeconds << endl;
songNumber++;

}
totalTime = 80*60;
timeUsed = (newtotalMinutes * 60 + newtotalSeconds);
minLeft = (totalTime - timeUsed) / 60;
secLeft = (totalTime - timeUsed) % 60;
outData << "There are "<< minLeft << " minutes and " << secLeft
<< " seconds left on the CD."<<endl;

return 0;
}
Does anyone have any idea how to fix this problem?? ANY help would be greatly appreciated!
Do you want to have this: endl here << newtotalSeconds << endl; ?
That will draw a newline and flush the output buffer, so I guess that's not what you want.
Last edited on
Nope that is definitely not the problem. I don't get it!? I'm not sure where i'm going wrong.....
Can you use [code] tags please?
Your code compiles fine.

Could you provide contents of your songs.dat source file?
Here is the source 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>                                                                      
#include <fstream>                                                                             
#include <iomanip>                                                                           
#include <cmath>                                   
 
using namespace std; 
 
int main() 
{ 
         
        int songNumber; 
        int totalMinutes; 
        int totalSeconds; 
        int current; 
        int newMinutes; 
        int newSeconds; 
        int newtotalMinutes = 0; 
        int newtotalSeconds = 0; 
        int timeUsed =0; 
        int minLeft =0; 
        int secLeft =0; 
        int totalTime; 
         
        ifstream inData; 
        ofstream outData; 
        inData.open("songs.dat"); 
		outData.open("output.dat"); 
 

        outData << "Song" << setw(20) 
                        << "Song Time" << setw(22)  
                        << "Total Time" << endl; 
        outData << "Number" << setw(12)  
                        << "Minutes" << setw(10)  
                        << "Seconds" << setw(12) 
                        << "Minutes" << setw(10)  
                        << "Seconds" << endl; 
        outData << "------" << setw(12)  
                        << "-------" << setw(10)  
                        << "-------" << setw(12) 
                        << "-------" << setw(10)  
                        << "-------" << endl; 
 
        
        totalMinutes = 0; 
        totalSeconds = 0; 
        newtotalMinutes = totalMinutes; 
        newtotalSeconds = totalSeconds; 
 
        songNumber = 1; 
         
        while (inData) 
        { 
                inData >> current;   
				    
				if (!inData) 
                        { 
                                break; 
                        }
                 
                
                newMinutes = (current/60); 
                newSeconds = (current % 60); 
                totalMinutes = newMinutes; 
                totalSeconds = newSeconds; 
                newtotalMinutes += totalMinutes; 
                newtotalSeconds += totalSeconds; 
 
                if(newtotalSeconds > 59) 
                { 
                        newtotalMinutes += 1; 
                        newtotalSeconds -= 60; 
                } 
 
                 
                
                outData << songNumber << setw(11)  
                                << newMinutes << setw(11)  
                                << newSeconds << setw(11) 
                                << newtotalMinutes << setw(11)  
                                << newtotalSeconds << endl; 
                songNumber++; 
 
        }    
                totalTime = 80*60; 
                timeUsed  = (newtotalMinutes * 60 + newtotalSeconds); 
                minLeft   = (totalTime - timeUsed) / 60; 
                secLeft   = (totalTime - timeUsed) % 60; 
                outData << "There are "<< minLeft << " minutes and " << secLeft 
                                << " seconds left on the CD."<<endl; 

	return 0;
}


Here is the input file:

1	310
2	462
3	259
4	273
5	627
6	535
7	300


And here is the output file:
Song           Song Time            Total Time
Number     Minutes   Seconds     Minutes   Seconds
------     -------   -------     -------   -------
1          0          1          0          1
2          5         50          5         51
3          0          2          5         53
4          7         42         13         35
5          0          3         13         38
6          4         19         17         57
7          0          4         18          1
8          4         33         22         34
9          0          5         22         39
10         10         27         33          6
11          0          6         33         12
12          8         55         42          7
13          0          7         42         14
14          5          0         47         14
There are 32 minutes and 46 seconds left on the CD.


It should look like this:
Song         Song Time              Total Time
Number    Minutes  Seconds      Minutes   Seconds
------    -------  -------      -------   -------
1             5      10            5         10
2             7      42           12         52
3             4      19           17         11
4             4      33           21         44
5            10      27           32         11
6             8      55           41          6
7             5      0            46          6
There are 33 minutes and 54 seconds of space left on the 80-minute CD.


Hopefully this helps!
My guess is you're looking for data that is smaller than your data type, like if you were looking for only a single byte but were using a 2 byte data type but poking in both bytes of that data. Similar errors can be made when using unions.
Topic archived. No new replies allowed.