Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 04 Sep 2012
Offline Last Active Oct 19 2012 02:29 PM

Topics I've Started

entering a string of data to an ofstream file

01 October 2012 - 12:18 AM

I am trying to create a program that allows the user to do 2 things (create a new file in the current directory, or search a file and display the word count) The word count works fine but when I try to allow the user to create a file it starts going wrong. I first ask the user to input a filename, and then enter some initial data into a string variable. That string variable gets passed to the createFile() function which takes care of entering the data into the file.

When I run this program I enter the file data I wish to put in the created file, say for example " Hello File Data" and when I look into that file once its been created there is only the first word ("Hello") in the case of this example. Here is the code:

The main function:

[source lang="cpp"] cout << "Enter the file name: " << endl; cin >> filename; outFile.open(filename.c_str());//create the file if (!outFile) { //file could not be created cout<< "File could not be completed" << endl; break; } else // file was created cout << "FILE WAS COMPLETED NAMED: " << filename << endl;; //ask the user to put some data y/n cout << "Would you like to set some initial data in the file? y/n" << endl; cin >> choice; switch(choice) { case 'y': cout << "Enter the data: " << endl; cin >> filedata; user1.createFile(outFile, filedata); break; case 'n': cout << "Ok then dont create initial data... Your created file is blank" << endl; break; } [/source]

The create filefunction:
[source lang="cpp"]void User::createFile(ofstream &outfile, string filedata){ //output the file into outfile outfile << filedata; cout << "You have successfully saved the data you entered" << endl;}[/source]

Please help! Posted Image


I will attach the zip folder as well just in case...

Why even use virtual functions in a Parent class?

05 September 2012 - 12:44 AM

So I am learning about inheritence and currently I am on polymorphism... I got the whole inheriting functions from the parent class in order to not duplicate code however I am stuck on one thing: The virtual function...

So say I have a class named Automobile like so with a child class that is named Ferrari:

[source lang="cpp"]class Automobile{public: virtual void drivespeed();};class Ferrari: public Automobile{public: Ferrari(string name); void drivespeed();private: string mName;};[/source]

[source lang="cpp"]//implementation of classesvoid Automobile::drivespeed(){ //does nothing cout << "UNDEFINED" << endl;}Ferrari::Ferrari(string name) :mName(name){}void Ferrari::drivespeed(){ //drives fast cout << "DRIVES REALLY FAST" << endl;}[/source]

now in the main.cpp file in the main function i could write like so...

[source lang="cpp"]int main(int argc, char **argv){ Automobile *auto = new Ferrari("Spider"); //invoke drivespeed function auto->drivespeed(); return 0;}[/source]

all this does is creates an object of the ferrari type (upcasting it to the automobile type) and then lets me invoke methods of that class... So here is my question:

What difference does the virtual function declaration in the Automobile class make? Couldn't I skip the declaration in automobile function since it is really not even a function (it does nothing) and just declare the function as normal in the Ferrari class? and if i made more child classes of the parent class Automobile, i could do the same?

Please let me know


Wayne Prim

Why my program wont compile when I have Embed Manifest turned on

04 September 2012 - 01:49 PM

I have a question regarding the compilation of a simple c++ program. I just got a new computer and downloaded Microsoft Visual c++. I then created a Console application to test if everything is dandy. I made a simple hello world application with the precompiled stdafx.h files included and received this error:

LINK: fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt

it wouldn't let me even compile the program.... I looked into the web and found out one simple thing:

1. go into project | properties | Configuration Properties | Manifest Tool and then make sure Embed Manifest is turned to No instead of Yes.

I did this and it worked perfectly... so my question is this:

Why does my project compile now that this simple project property is changed? What does this do? Will I run into problems if I continue to leave it turned to No?

Please let me know if you need any more information on this matter.

Thanks in advance!

Wayne Prim