|
||||||
| Forums FAQ | Members List | Search | Today's Posts | Mark Forums Read |
| Programming/Scripting Questions about programming, bash scripts, perl, php, cron jobs, ruby, python, etc. |
![]() |
|
|
LinkBack | Thread Tools | Display Modes |
|
||||
|
Ok so I changed my approach and it compiles now, but my problem is with my output, sense I am dealing with time in this program I can't have anything greater than 60 seconds. Also I have no clue with is up with the third column in my output.
Code:
/*Adam Jimerson
10/09/09
Songs
*/
#include <iostream> //Needed for input and output stream
#include <fstream> //Needed for ifstream
#include <iomanip> //Needed for setprecision
#include <cstdlib> //Needed for Exit
using namespace std;
ifstream inFile; //Create the file handle for the file to be read
int main()
{
inFile.open ( "songs.dat" ); //Open the file to be read
if ( ! inFile ) { //Test to see if songs.dat can be read
inFile.close();
cerr << "Can't open songs.dat." << endl; //If not print an error stating such
exit(1);
}
float SongTime; //Get the song play time from songs.dat
int SongNumber = 1; //Count the Song Number from the list
float TotalSongTime;
cout << "Song Song Time Total Time" << endl;
cout << "Number Minutes Seconds Minutes Seconds" << endl;
cout << "------- ------- ------- ------- -------" << endl;
while ( inFile >> SongTime ) {
TotalSongTime = TotalSongTime + SongTime;
SongTime = SongTime / 60;
cout << SongNumber << " " << setprecision(3) << SongTime << " " << setprecision(3) << TotalSongTime << endl;
SongNumber ++;
}
inFile.close();
return 0;
}
Code:
Song Song Time Total Time Number Minutes Seconds Minutes Seconds ------- ------- ------- ------- ------- 1 5.42 325 2 2.42 470 3 2 590 4 4.67 870 5 6.67 1.27e+03 6 5.25 1.58e+03 7 9.67 2.16e+03 8 9.33 2.72e+03 9 14.8 3.62e+03 10 2.82 3.78e+03 11 3.87 4.02e+03 12 5.85 4.37e+03 13 1.93 4.48e+03 14 1.6 4.58e+03
__________________
"We must plan for freedom, and not only for security, if for no other reason than only freedom can make security more secure." Karl Popper |
|
||||
|
This is going to give you minutes, plus the remainder as a fraction. (Which is what the output is showing.) You should do something like
Code:
SongTimeMinutes = SongTime / 60; SongTimeSeconds = SongTime - (SongTimeMinutes*60); Quote:
Can't help you much more beyond that. College classes want you to use the "cout" and "infile" stuff, but I've never used them in real life. (I've never met a C++ programmer who uses them in the real world, either, though I know there has to be at least a few.) They're too clunky and error prone, and I want better control of the I/O. Even in my most advanced C++ programs, I use fopen(), fread(), fwrite(), and (of course!) good ol' printf() for output. More control, I can get exactly what I want, and no surprises. But that's just my opinion. |
|
||||
|
I remember that if you want to omit the special notation 1.Ex
you need to pass an argument earlier, something like: cout.setf(ios_base::fixed); So it would look like this: /*Adam Jimerson 10/09/09 Songs */ #include <iostream> //Needed for input and output stream #include <fstream> //Needed for ifstream #include <iomanip> //Needed for setprecision #include <cstdlib> //Needed for Exit using namespace std; ifstream inFile; //Create the file handle for the file to be read int main() { cout.setf(ios_base::fixed); inFile.open ( "songs.dat" ); //Open the file to be read if ( ! inFile ) { //Test to see if songs.dat can be read inFile.close(); cerr << "Can't open songs.dat." << endl; //If not print an error stating such exit(1); } float SongTime; //Get the song play time from songs.dat int SongNumber = 1; //Count the Song Number from the list float TotalSongTime; cout << "Song Song Time Total Time" << endl; cout << "Number Minutes Seconds Minutes Seconds" << endl; cout << "------- ------- ------- ------- -------" << endl; while ( inFile >> SongTime ) { TotalSongTime = TotalSongTime + SongTime; SongTime = SongTime / 60; cout << SongNumber << " " << setprecision(3) << SongTime << " " << setprecision(3) << TotalSongTime << endl; SongNumber ++; } inFile.close(); return 0; }
__________________
How does a linux geek make love?? - unzip; strip; touch; finger; mount; fsck; more; yes; umount; sleep; |
|
||||
|
I meant to post this the other night but I managed to get this to work correctly
Here is my working code: Code:
/*Adam Jimerson
10/09/09
Songs
*/
#include <iostream> //Needed for input and output stream
#include <fstream> //Needed for ifstream
#include <cstdlib> //Needed for Exit
using namespace std;
ifstream inFile; //Create the file handle for the file to be read
int main()
{
inFile.open ( "songs.dat" ); //Open the file to be read
if ( ! inFile ) { //Test to see if songs.dat can be read
inFile.close();
cerr << "Can't open songs.dat, does the files exist?" << endl; //If not print an error stating such
exit(1);
}
int SongTime; //Get the song play time from songs.dat
int SongNumber = 1; //Count the Song Number from the list
int TotalSongTime; //Use for calculating the total CD run time
int seconds; //Use to calculate the number of seconds for each track
int minutes; //Use to calculate the number of minutes for each track
int Totalseconds; //Use to calculate the number of seconds for the whole CD
int Totalminutes; //Use to calculate the number of minutes for the whole CD
int FreeMinutes; //Use to calculate the number of minutes remaining on the CD
int FreeSeconds; //Use to calculate the number of seconds remaining on the CD
cout << "Song Song Time Total Time" << endl;
cout << "Number Minutes Seconds Minutes Seconds" << endl;
cout << "------- ------- ------- ------- -------" << endl; //Print out the layout
while ( inFile >> SongTime ) {
TotalSongTime = TotalSongTime + SongTime; //Calculate the total CD run time
seconds = SongTime % 60; //Calculate the number of seconds in the track
minutes = SongTime / 60;
minutes = minutes % 60; //Calculate the number of minutes in the track
Totalseconds = TotalSongTime % 60; //Calculate the number of seconds for the whole CD
Totalminutes = TotalSongTime / 60; //Calculate the number of minutes for the whole CD
cout << SongNumber << " " << minutes << ":" << seconds << " " << Totalminutes << ":" << Totalseconds << endl; /*Display the
information to the user, following the layout set above*/
SongNumber ++; //Incrament the song count
}
FreeMinutes = 80 - Totalminutes; //Find the number of minutes left on the CD
FreeSeconds = 60 - Totalseconds; //Find the number of seconds left on the CD
cout << "There are " << FreeMinutes << " minutes and " << FreeSeconds << " seconds left on the 80-minutes CD." << endl; //Display the information to the user
inFile.close(); //Close the songs.dat file
return 0;
}
Code:
Song Song Time Total Time Number Minutes Seconds Minutes Seconds ------- ------- ------- ------- ------- 1 5:25 5:25 2 2:25 7:50 3 2:0 9:50 4 4:40 14:30 5 6:40 21:10 6 5:15 26:25 7 9:40 36:5 8 9:20 45:25 9 14:50 60:15 10 2:49 63:4 11 3:52 66:56 12 5:51 72:47 13 1:56 74:43 14 1:36 76:19 There are 4 minutes and 41 seconds left on the 80-minutes CD.
__________________
"We must plan for freedom, and not only for security, if for no other reason than only freedom can make security more secure." Karl Popper |
|
|||
|
He's going to count off for error checking?
![]() Tell him he gets 5 point off from me ![]() Dave |
|
||||
|
He doesn't like students doing more than what the assignment asks for, he took points off because I used a function in one of my assignments before we had covered them. At this point I don't care if he takes points off or not I'm doing this to learn and if he doesn't like me to learn how to program in C++ he can fail me
__________________
"We must plan for freedom, and not only for security, if for no other reason than only freedom can make security more secure." Karl Popper |
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|