Can Someone Check my code please
Oct 4, 2013 at 12:05am UTC
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
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
int songNumber, songMinutes, songSeconds;
int totalMinutes = 0;
int totalSeconds = 0;
int minutesLeft = 0;
int secondsLeft = 0;
int totalTime = 0;
int usedTime = 0;
int currentSong = 0;
int currentSongMinutes = 0;
int currentSongSeconds = 0;
int seconds;
int usedMinutes = 0;
int usedSeconds = 0;
totalTime = (80 * 60);
ifstream inData;
inData.open("songs.dat" );
if (!inData.is_open())
{
cout << "Can't open the file. Try again." << endl;
return 1;
}
else
{
cout << setw(8) << "Song " << setw(28) << "Song Song" << setw(28) << "Total Total" << setw(25) << "Number" << setw(20)
<< "Minutes" << setw(10) << "Seconds" << setw(18) << "Minutes" << setw(10) << "Seconds" << setw(22) << "-------" << setw(20)
<< "-------" << setw(10) << "-------" << setw(18) << "-------" << setw(10) << "-------" << endl;
songNumber = 1;
while (inData.good())
{
inData >> currentSong;
currentSongMinutes = (currentSong / 60);
currentSongSeconds = (currentSong % 60);
songSeconds = currentSongSeconds;
songMinutes = currentSongMinutes;
seconds = (songMinutes * 60);
usedMinutes = 0;
usedSeconds = 0;
usedTime = usedTime + currentSong;
totalMinutes = (usedTime) / 60;
totalSeconds = (usedTime) % 60;
minutesLeft = (totalTime - usedTime) / 60;
secondsLeft = (totalTime - usedTime) % 60;
cout << fixed << showpoint << setprecision(2) << setw(7) << songNumber << setw(18) << songMinutes << setw(10)
<< songSeconds << setw(18) << totalMinutes << setw(10) << totalSeconds << setw(15) << endl;
songNumber++;
}
cout << "There are " << minutesLeft << " minutes and " << secondsLeft << " seconds of space left on the 80-minute CD." << endl << endl;
inData.close();
return 0;
}
}
Last edited on Oct 4, 2013 at 12:06am UTC
Oct 4, 2013 at 12:07am UTC
1 2 3 4 5 6 7 8 9 10 11
310
482
601
259
983
273
567
-12
535
45
300
The Input
Last edited on Oct 4, 2013 at 12:08am UTC
Oct 4, 2013 at 1:11am UTC
Can you highlight what's wrong? Is there an error, or is something not working right? If so, please post whatever is wrong/the error.
Oct 4, 2013 at 2:41am UTC
There are no errors.
Oct 4, 2013 at 4:08am UTC
There is at least one obvious error:
while (inData.good())
(in this case, because of it, you're seeing one more song that there are in the file, the output ends with:
10 0 45 67 23
11 5 0 72 23
12 5 0 77 23
_
Oct 4, 2013 at 1:54pm UTC
I see Cubbi's point. If you have no white-space after your last entry, then it would appear okay, because after the last read, EOF would have been encountered. If you do have an extra carriage return for example, the stream will still be good() after your last data entry, and your program's behaviour will not be as intended.
Perhaps, instead of checking inData.good() as the loop condition, and then modifying the stream by reading from it in your loop body, use your inData >> currentSong as the loop condition. Remember, >> will return inData, which can be used as a boolean, thereby accomplishing the good() check you're trying.
So:
1 2 3 4 5
// while (inData.good())
while (inData >> currentSong)
{
// inData >> currentSong;
....
Topic archived. No new replies allowed.