Lets play find the error :D

Discussion in 'Programming General' started by Esuom, Mar 1, 2009.

Lets play find the error :D
  1. Unread #1 - Mar 1, 2009 at 9:52 PM
  2. Esuom
    Joined:
    May 3, 2007
    Posts:
    1,731
    Referrals:
    2
    Sythe Gold:
    0

    Esuom Guru
    Banned

    Lets play find the error :D

    Okay, I was just trying to write up a simple program to do some monotonous work that I cbf to do by hand. It compiles/runs but it doesn't save the file...


    Code:
    //I know. Probably something really obviously. Im learning :P
    #include <iostream>
    #include <fstream>
    #include <iostream.h>
    //Probably a bunch of unnecessary  includes, I know :P
    
    using namespace std;
    
    int main () {
      int a;
      cin >> a;
      ofstream myfile (a + ".php");
      myfile.open (a + ".php"); //Probably my error?
      myfile << "<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0\" width=\"640\" height=\"480\">" << endl;
      myfile << " <param name=\"movie\" value=\"" << a << ".swf\" />" << endl;
      myfile << "<param name=\"quality\" value\"high\" />" << endl;
      myfile << "<embed src=\"./" << a << ".swf\" quality=\"high\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" type=\"application/x-shockwave-flash\" width=\"640\" height=\"480\"></embed>" << endl;
      myfile << "</object>" << endl;
      myfile.close();
      return 0;
    }
    
    
    
     
  3. Unread #2 - Mar 1, 2009 at 11:21 PM
  4. omagawd
    Joined:
    Sep 11, 2007
    Posts:
    381
    Referrals:
    0
    Sythe Gold:
    0

    omagawd Forum Addict
    $5 USD Donor

    Lets play find the error :D

    Lol I highly doubt anyone will get this, especially now a days when coding programs tell you errors in your code, most people can't to it manually.
     
  5. Unread #3 - Mar 1, 2009 at 11:30 PM
  6. cp
    Joined:
    Jan 30, 2007
    Posts:
    3,278
    Referrals:
    6
    Sythe Gold:
    0

    cp an cat
    Banned

    Lets play find the error :D

    iostream.h is outdated... Just use iostream.

    Aside from that, everything looks fine.

    And I think it'll compile fine even with iostream.h if you ignore warnings...
     
  7. Unread #4 - Mar 2, 2009 at 12:49 AM
  8. Nullware
    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0

    Nullware Guru

    Lets play find the error :D

    Here's the working code. It was only a few small things that I changed.

    1. iostream.h has been mostly abandonned as cp said, it doesn't even exist on the linux machine I am using to compile so I removed it.

    2. You shouldn't use a constructor when you initialize myfile because you were going to use its open function to give it a filename anyway and you can't do both. Fix: ofstream myfile;

    3. You can't concantenate integers and character arrays using the + operator (int + ".php"). I included the string library and made it so the user is inputting a string as you can do (string + ".php")

    4. The open function can't take a string class but it can take an array of characters so c_str() converts the full filename string into an array of characters.

    Code:
    //I know. Probably something really obviously. Im learning :P
    #include <iostream>
    #include <fstream>
    #include <string>
    //Probably a bunch of unnecessary  includes, I know :P
    
    using namespace std;
    
    int main () {
      string a;
      cin >> a;
      ofstream myfile;
      myfile.open ((a + ".php").c_str()); //Probably my error?
      myfile << "<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0\" width=\"640\" height=\"480\">" << endl;
      myfile << " <param name=\"movie\" value=\"" << a << ".swf\" />" << endl;
      myfile << "<param name=\"quality\" value\"high\" />" << endl;
      myfile << "<embed src=\"./" << a << ".swf\" quality=\"high\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" type=\"application/x-shockwave-flash\" width=\"640\" height=\"480\"></embed>" << endl;
      myfile << "</object>" << endl;
      myfile.close();
      return 0;
    }
     
  9. Unread #5 - Mar 2, 2009 at 9:25 AM
  10. Esuom
    Joined:
    May 3, 2007
    Posts:
    1,731
    Referrals:
    2
    Sythe Gold:
    0

    Esuom Guru
    Banned

    Lets play find the error :D

    Ah, it works now. Thanks Nullware, I am glad that you gave the explanation too, rather then just throwing the source at me.


    Stick around for more questions in the future :p
     
< Hi a bit of help | Bypass CAPTCHA by matching images >

Users viewing this thread
1 guest


 
 
Adblock breaks this site