• Log in or sign up for an account here: https://www.sythe.org/login

Lets play find the error :D

Esuom

Guru
Banned
Joined
May 3, 2007
Sythe Gold
0
Messages
1,731
Location
EST Time Zone.
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;
}
 
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.
 
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...
 
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;
}
 
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
 
Runescape Gamblingrunescape gambling
Back
Top