c++ divx calculator programming problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • xviddivxoggmp3
    essence of digital
    • Jun 2002
    • 150

    c++ divx calculator programming problem

    I'm trying to write a C++ program for shit and giggles.
    It only calculates DivX calculations for my first formula.
    If you know any C++ help out a newbie and tell me what is wrong. This is generic without a gui.
    It does compile, but I'm not sure what is wrong.
    The only thing I see that my text book says is wrong is the nested if statement, but in other languages I can write the if statement this way. I would like idea's on how to fix.

    ************************************************
    see attached .txt file for code
    ************************************************

    Attached Files
    no life just digital
    http://forum.digital-digest.com/
    http://forum.doom9.org/
    http://forums.divx.com/
    http://forum.vcdhelp.com/
    http://www.xvid.org/
  • khp
    The Other
    • Nov 2001
    • 2161

    #2
    It runs fine when I compile with MS VC6.

    But I would suggest using a switch instead of the nested if.

    like this

    Code:
     
    //divx 
    //program 1
    
    #include <iostream>
    
    using namespace std;
    
    int main()
    
    {
    	int     calc;
    	double	MovieLength,
    			kbps,
    			count;
    	
    	float	sum;
    
    	do
    		{
    			cout << "Enter (1) for DivX 3 or (2) for DivX 4/5: ";
    			cin >> calc;
    			
    			switch (calc)
    				{
    			case 1:
    				cout << "Input Movie Length in Minutes: ";
    				cin >> MovieLength;
    
    				cout << "Input kbps: ";
    				cin >> kbps;
    				
    				sum = ((MovieLength * 60) * kbps) / 8192;
    							
    				cout << "Size of Movie: " << sum << "MB ";
    				cout << "\nInput (1) to calculate another Movie: ";
    				cin >> count;
    				break;
    			case 2:
    				cout << "Input Movie Length in Minutes: ";
    				cin >> MovieLength;
    
    				cout << "Input kbps: ";
    				cin >> kbps;
    
    				sum = (((MovieLength * 60) * kbps) / 8192) * (float (1000) / float (1024));
    										
    				cout << "Size of Movie: " << sum << "MB ";
    				cout << "\nInput (1) to calculate another Movie: ";
    				cin >> count;
    				break;
    			default:
    				cout << "Input valid calculation request ";
    				cout << "\nInput (1) to calculate another Movie: ";
    				cin	>> count;
    			}
    		}
    	while (count == 1);
    
    return 0;
    }
    And of course I'am not a fan of do { } while loops. There is never any need for such a structure and it makes the code harder to read. Just use while {} insead.

    EDIT: the above code has been futher modified to fix the problems mentioned below
    Last edited by khp; 4 Sep 2002, 12:44 AM.
    Donate your idle CPU time for something usefull.
    http://folding.stanford.edu/

    Comment

    • khp
      The Other
      • Nov 2001
      • 2161

      #3
      And I suppose it should be

      Code:
      while (count == 1);
      instead of

      Code:
      while (count = 1);
      unless you really want to loop forever.
      Donate your idle CPU time for something usefull.
      http://folding.stanford.edu/

      Comment

      • khp
        The Other
        • Nov 2001
        • 2161

        #4
        And I suppose this line might also be giving you problems in the divx4/5 calculation

        Code:
        sum = ((MovieLength * 60) * kbps) / (8192 * (1000 / 1024));
        When you divide two integers you get an integer division, which means that (1000/1024) = 0 because it's rounded down. This, then causes a division by zero. At least in my compilation

        And shouldn't it have been (8192 * (1024 / 1000) or maybe (8192 / (1000/1024))

        I think this line will give you the correct result.

        Code:
        sum = (((MovieLength * 60) * kbps) / 8192) * (float (1000)/float (1024));
        Last edited by khp; 3 Sep 2002, 10:06 PM.
        Donate your idle CPU time for something usefull.
        http://folding.stanford.edu/

        Comment

        • khp
          The Other
          • Nov 2001
          • 2161

          #5
          If you are interrested, here is a bitrate calculator I wrote, it's written in perl.

          Code:
          package bitrate;  # ahhhh the bitrate calculator.
                            # just about the simpelst code in the script
          sub calc {        
              my $invocant = shift;  
              my $self = bless({}, ref $invocant || $invocant);
              ($size,$conversionpath,$ripped) = @_;  # formal arguments
              $logfile = $ripped->logfile;
              print $logfile "Calculating video bitrate for" . $size . " MB, length: " . $ripped->framecount . " frames\n"; 
              $size = $size * 1024 * 1024;              # we'll calculate this in bytes
              $videosize = $size - (63 * $framecount);  # avi overhead, not very precise, I know.
              use File::stat;
              print $logfile "looking for first audio track: " . $ripped->audio1 . "\n";
              if ($filestat = stat $conversionpath . $ripped->moviename . "\\" . $ripped->audio1) { #looking for audio1
          	$videosize = $videosize - $filestat->size;
          	print $logfile "Found audio1 file size: " . $filestat->size . "\n";
              }
              print $logfile "looking for second audio track: " . $ripped->audio2 . "\n";
              if ($filestat = stat $conversionpath . $ripped->moviename . "\\" . $ripped->audio2) { #looking for audio2
          	$videosize = $videosize - $filestat->size; 
          	print $logfile "Found audio2 file size: " . $filestat->size . "\n";
              }
              opendir converted, $conversionpath . $ripped->moviename or die "can't open conversion dir";
              @allfiles = readdir converted;      # search through all file in the conversion dir
              foreach (@allfiles) {
          	if (m/\.sub/) {
          	    $filestat = stat $conversionpath . $ripped->moviename . "\\" . $_;
          	    print $logfile "Found subs file: " . $_ . "\nsize: " . $filestat->size . "\n"; 
          	    $videosize = $videosize - $filestat->size;
          	}
          	if (m/endcredits\.avi/) {     # subtract size of endcredits
          	    $filestat = stat $conversionpath . $ripped->moviename . "\\" . $_;
          	    print $logfile "Found endcredits file: " . $_ . "\nsize: " . $filestat->size . "\n";
          	    $videosize = $videosize - $filestat->size;
          	}
          	if (m/begincredits\.avi/) {     # subtract size of endcredits
          	    $filestat = stat $conversionpath . $ripped->moviename . "\\" . $_;
          	    print $logfile "Found endcredits file: " . $_ . "\nsize: " . $filestat->size . "\n";
          	    $videosize = $videosize - $filestat->size;
          	}
              }
              $videosize = $videosize * 8;      # calculate video size in bits
              $bitrate = ($videosize / $ripped->framecount) * 25;  # calculate videobitrate in bits persecond
              print $logfile "Calculated video size: " . $videosize . "  bitrate: " . $bitrate . "\n";
              return $bitrate;
          }
          
          return 1;
          It's part of a collection of scripts, for automatic DVD->divx conversion. So you can't run it as is.

          This is turning into a monologue, am I saying anything usefull, or have I just gone mad.
          Last edited by khp; 3 Sep 2002, 11:13 PM.
          Donate your idle CPU time for something usefull.
          http://folding.stanford.edu/

          Comment

          • xviddivxoggmp3
            essence of digital
            • Jun 2002
            • 150

            #6
            thanks for the info.
            I didn't know you could put float in the equation. when i try to compile it with float in the equation it gives me the fallowing error

            Debug/divx calculator.exe : fatal error LNK1169: one or more multiply defined symbols found


            *************************************************

            sum = (((MovieLength * 60) * kbps) / 8192) * (float (1000)/float (1024));

            *************************************************
            I'm reading on the switch now. I'm used to case statements, but did not know they where called switch in c++

            will post my results later this evening after I recode it all.
            thanks again khp you rule.

            sorry I'm a newbie and clueless to perl


            Last edited by xviddivxoggmp3; 4 Sep 2002, 09:58 AM.
            no life just digital
            http://forum.digital-digest.com/
            http://forum.doom9.org/
            http://forums.divx.com/
            http://forum.vcdhelp.com/
            http://www.xvid.org/

            Comment

            • khp
              The Other
              • Nov 2001
              • 2161

              #7
              Originally posted by xviddivxoggmp3

              Debug/divx calculator.exe : fatal error LNK1169: one or more multiply defined symbols found
              OK, so your compiler doesn't like c-style casts. I guess I should have known better, but I've always been a c programmer at heart .
              Anyway it would probably be the best to keep to integer calculations as much as possible. As I try to do in my perl script, although perl happily converts to float at every chance it get.

              In your case this should also work.

              Code:
              sum = ((((MovieLength * 60) * kbps) / 8192) * 1000 ) / 1024;
              Donate your idle CPU time for something usefull.
              http://folding.stanford.edu/

              Comment

              Working...