Thread: Homework Help Using While

  1. #1
    Registered User Trekkie's Avatar
    Join Date
    Oct 2005
    Posts
    20

    Homework Help Using While

    Hi,

    My assignment is to figure out the total time for a group of songs and see how they will fit on a CD. The data are on file songs.dat and looks like this:

    300
    250
    500
    570
    380
    425
    275
    305
    320
    450

    The time is entered as seconds. After all the data has been read, the application should print a message indicating the time remaining on the CD. The out put should be in the form of a table with colums and headings written on a file.

    The chart should look like this:

    Song Song Time Total Time
    Number Min Sec Min Sec
    1 5 10 5 10
    2 7 42 12 52
    5 4 19 17 11
    3 4 33 21 44
    4 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"

    Note that the output converts the input from seconds to minutes and seconds.

    This is what I have so far:
    Code:
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    
    using namespace std;
    
    int main()
    {
            int count;
            int song;
            int sec;
            int min;
            int totalSec;
            int totalMin;
    
            ifstream indata;
            indata.open("songs.dat");
    
            if (!indata) 
    {
    cout << "Can't open input file." <<endl;
    return 1;
    }
    indata >> min >> sec;
    
    cout << setw(4) << "Song" << setw(18)
    	 << "Song Time" << setw(18) << "Total Time" << endl;
    	 
    cout << fixed << setw(2)
    	 << "Number" << setw(10) << "Minutes" << setw(10) << "Seconds" << setw(10)
    	 << "Minutes" << setw(10) << "Seconds" <<endl;
    indata >> min >> sec;
    I don't know how to get the information into a chart. I know I need to use a while statement, but I'm not sure how. Can anyone help me??

    Thanks

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    A while loop is just like an if statement, except it keeps repeating if the condition is true.
    Code:
    int x = 0;
    while(x < 10) {
        cout << setw(20) << name[x] << " has $" << money[x] << endl;
        x ++;
    }
    See if that helps you at all.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User Trekkie's Avatar
    Join Date
    Oct 2005
    Posts
    20
    Yes, that does help a little bit. But I still don't know how to get the information in the same format as the chart.

    I've tried this:

    Code:
    while(indata)
    {
            song++;
            cout << min;
            totalMin = 0;
            count = 0;
    But this doesn't work. Can you give me any more clues?

    Thank you!

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    file >> data;
    cout << data;
    inside the loop?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Before you get to the while loop, may I ask why you're reading in what you are from the file?

    I don't see any minutes in that file.

    You have to read in the seconds and do calculations for the other variables.
    Sent from my iPadŽ

  6. #6
    Registered User Trekkie's Avatar
    Join Date
    Oct 2005
    Posts
    20
    Ok, after another 3 hours, this is what I have:

    Code:
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <cmath>
    
    using namespace std;
    
    int main()
    {
            int songTm_Min;
            int songTm_Sec;
            int total_Time;
            int totalTm_Min;
            int totalTm_Sec;
    		int current_Num;
    		int totalTm_Min = 0;
    		int song_Num;
    
          
            ifstream indata;
            indata.open("songs.dat");
    
            if (!indata) 
    {
    cout << "Can't open input file." <<endl;
    return 1;
    }
    
    inData >> current_Num;
     
      while (inData)
      {
       // Calculations
       songTm_Min = current_Num / 60; // Calculates the current song's time in Minutes
       songTm_Sec = current_Num % 60; // Calculates the current song's time left in Seconds
       total_Time = total_Time + current_Num; // Calculates the overall Total Time
       totalTm_Min = total_Time / 60; // Calculates the Total Time in Minutes
       totalTm_Sec = total_Time % 60; // Calculates the Total Time left in Seconds
     
       // Print out the results in a format
       cout << setw (4) << song_Num << setw(12) << songTm_Min << setw(10) << songTm_Sec << setw(14) << totalTm_Min << setw(10) << totalTm_Sec << endl;
       song_Num++;
     
       // Repeat Data-in
       inData >> current_Num;
      
       cout <<endl;
       cout << "There are 33 minutes and 54 seconds of space left on the 80 minute CD." <<endl;
      }
    Now I am coming up with a couple of errors. The main one being "inData undeclared identifier". Can someone explain to me what the "inData" does? Where do I declare this at? Didn't I declare it already when I wrote: inData >> current_Num;

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    You created:
    Code:
    ifstream indata;
    You're trying to use the identifier:
    Code:
    inData >>
    See the difference?
    Sent from my iPadŽ

  8. #8
    Registered User Trekkie's Avatar
    Join Date
    Oct 2005
    Posts
    20
    I'm still working on this assignment. I've made some changes, but now I'm getting an error that my file cannot be opened. Here is my code:

    Code:
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <cmath>
    
    using namespace std;
    
    int main()
    {
            int songTm_Min;
            int songTm_Sec;
            int total_Time;
            int totalTm_Min;
            int totalTm_Sec;
    		int current_Num;
    		int song_Num;
    		
    		      
            ifstream cdFile;
            cdFile.open("songs.dat");
    
    		int counter =1;
    		int total = 0;
    		int cdNow;
    if (cdFile) 
    {
    cout << "Can't open input file." <<endl;
    return 1;
            
    }
      cdFile >> cdNow;
     
      while (cdFile)
      {
    
       
       songTm_Min = current_Num / 60; // Calculates the current song's time in Minutes
       songTm_Sec = current_Num % 60; // Calculates the current song's time left in Seconds
       total_Time = total_Time + current_Num; // Calculates the overall Total Time
       totalTm_Min = total_Time / 60; // Calculates the Total Time in Minutes
       totalTm_Sec = total_Time % 60; // Calculates the Total Time left in Seconds
     
    	cout << setw(5) <<"Song"; 
    	cout << setw(10) <<"             Song Time";
    	cout << setw(10) <<"             Total Time\n\n";
    	cout << setw(5) <<"Number"; 
    	cout << setw(10) <<"        Minutes";
    	cout << setw(10) <<" Seconds";
    	cout << setw(10) <<"      Minutes";
    	cout << setw(10) <<"  Seconds\n";
    	cout <<"-----------------------------------------------------\n";
    
       cout << "There are 33 minutes and 54 seconds of space left on the 80 minute CD." <<endl;
      }
    }
    How come I can't open my file??

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> if (cdFile)

    That evaluates to true if the file opened successfully. Did you mean if (!cdFile)?

  10. #10
    Registered User Trekkie's Avatar
    Join Date
    Oct 2005
    Posts
    20
    THANK YOU!!!!!!!!!!!!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 11-03-2001, 04:39 PM
  2. Homework
    By kermi3 in forum C++ Programming
    Replies: 15
    Last Post: 09-26-2001, 03:16 PM
  3. Homework
    By kermi3 in forum Windows Programming
    Replies: 5
    Last Post: 09-15-2001, 11:48 AM
  4. Homework
    By kermi3 in forum C Programming
    Replies: 0
    Last Post: 09-10-2001, 01:26 PM