Results 1 to 7 of 7

Thread: C++ program won't compile

  1. #1
    Join Date
    Jun 2008
    Location
    Cleveland, TN USA
    Posts
    412

    Default C++ program won't compile

    I am trying to debug a problem with one of my assignment for my Intro
    to C++ Programming which reads a list of time from a file, in seconds,
    then convert them to minutes and seconds along with printing out the
    total run time of the CD

    the code that I have is

    ,----[ 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 <string> //Needed for string manipulation
    |
    |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
    | cout << "Where is songs.dat? " << endl;
    //Poll user asking for its location
    | string songdata;
    | cin >> songdata; //Read in and store the location
    | inFile.open ( songdata ); //Open the file to be
    read
    | }
    | int SongTime; //Get the song play time from songs.dat
    | int SongNumber = 1; //Count the Song Number from the
    list
    | int TotalSongTime;
    | //int ConvertSongTime; //Use this if needed to convert time
    from seconds only to minutes

    | 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 ( 2 ) << SongTime << " " <<
    setprecision ( 2 ) << TotalSongTime << endl;
    | SongNumber ++;
    | }
    | return 0;
    |}
    `----

    The songs.dat file is
    ,----[ ]
    | 325
    |145
    |120
    |280
    |400
    |315
    |580
    |560
    |890
    |169
    |232
    |351
    |116
    |96
    `----

    The error that I get is on Line 26 about "No matching function for
    call to 'std::basic_ifstream<char, std::char_traits<char>
    >:pen(std::string&)'

    --
    "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

  2. #2
    Join Date
    Jun 2008
    Location
    Cleveland, TN USA
    Posts
    412

    Default Re: C++ program won't compile

    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;
    }
    The output I get with the above is:

    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

  3. #3
    Join Date
    Oct 2008
    Location
    Birmingham. AL
    Posts
    858

    Default Re: C++ program won't compile

    Quote Originally Posted by vendion View Post
    SongTime = SongTime / 60;
    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);
    ... and print them separately. As for the third column,

    << setprecision(3)
    It's doing exactly what you told it to: it has set the precision to a fixed 3 places. Once you go past 999 total time, it starts displaying as an exponent.

    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.

  4. #4

    Default Re: C++ program won't compile

    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??

    - rtfm; unzip; strip; touch; finger; mount; fsck; more; yes; umount; zip; sleep;

  5. #5
    Join Date
    Jun 2008
    Location
    Cleveland, TN USA
    Posts
    412

    Default Re: C++ program won't compile

    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;
    }
    This is the output now:

    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.
    Might not be the best way to do it, but it works and my teacher is already going to count off for me going the extra step and testing to see if the input file was opened or not
    "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

  6. #6
    Join Date
    May 2009
    Location
    North Carolina USA
    Posts
    76

    Default Re: C++ program won't compile

    He's going to count off for error checking?
    Tell him he gets 5 point off from me

    Dave

  7. #7
    Join Date
    Jun 2008
    Location
    Cleveland, TN USA
    Posts
    412

    Default Re: C++ program won't compile

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •