Jump to content

Coding Help

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
hetra

hetra

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 297 posts
Hello All,

I am finally making my first program since I started on C++. It is simple, it basically extracts the first name and last name from the user and then exports it to a text file.

Here's the catch...I don't know how (or if it's possible) to export it to a text file.

Here's the code so far:

//A simple program for finding names.

#include <iostream>
using namespace std;


// Declares two string type variables to store the first name and second name



string firstName

string secondName

//Requests the user to input first name, then last name, then dumps the two into the two variables declard above.

cout <<"Hello there! Please enter your first name:"cin >>firstName

cout <<"Now your second name:"cin >>secondName

//Waits for the user to terminate.
system ("PAUSE");
return 0;




I need to export those two variables to a text file.



Any info would be great.

Thanks

Edited by Hunter100, 13 February 2010 - 12:18 AM.


#2
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
Unless you very specifically need formatted data, I very much recommend that you use getline() instead of cin >>. Like so:
cout << "Hello there! Please enter your first name:";
getline(cin, firstName);
cout << "Now your second name:";
getline(cin, secondName);
Also, remember that you need to use a semicolon [noparse](;)[/noparse] to end each command, something you were NOT doing with the cout/cin combination there. cout and cin are very different objects and should not be operated upon in the same expression.

Anyway, writing to files is easy, all you need is an ofstream object.
#include <fstream>

int main()
{
    using namespace std;
    ofstream outstream("filename.txt");
    outstream << "Text to write\n";
Just treat the [noparse]std::ofstream[/noparse] basically the same way you would cout, and you should be fine.
Wow I changed my sig!

#3
hetra

hetra

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 297 posts
Right so this code should work?

//A simple program for finding names.


#include <iostream>

#include <fstream>

int main ()


{


	using namespace std;

	ofstream outstream("loada.txt");

    	outstream << "Text to write\n";




// Declares two string type variables to store the first name and second name




string firstName


string secondName


//Requests the user to input first name, then last name, then dumps the two into the two variables declared above.

cout <<"Hello there! Please enter your first name:";

getline (cin, firstname);


cout <<"Now your second name:";

getline (cin, secondName);


//Waits for the user to terminate.

system ("PAUSE");

return 0;

Edited by Hunter100, 13 February 2010 - 12:14 AM.


#4
crippled

crippled

    Newbie

  • Members
  • PipPip
  • 12 posts
You just copied the basic syntax that ZekeDragon gave as an example. So no, that code will not put the names in a file.

This will create the ofstream object,
ofstream outstream("loada.txt");

Then, after you getline the data being read, put it into the text file with,
outstream << variableName;


#5
mmo-dev

mmo-dev

    Learning Programmer

  • Members
  • PipPipPip
  • 87 posts
ok so u can ether use the above code mentioned or look around on visual basic, vb, active server pages (ASP),java, javscript,c, c++, c__ , vbscript,active server pages, ASP, vbscript,SQL, database, informix, oracle, SQL Server,Perl, CGI,Delphi, PHP,Free source code for the taking. Over five million lines of progra for some examples u never know u might find something better to enhance it with

#6
solidsnake

solidsnake

    Newbie

  • Members
  • Pip
  • 5 posts

Quote

ofstream myfile ("example.txt", ios::app);
if (myfile.is_open())
{
myfile <<"\n"<<firstName<<" | "<< secondName;
myfile.close();
}
else cout << "Unable to open file";
paste this after user entering values for those two variables. If there is no file it will create it. Every next time it will write into next line...If you want him to overwrite it every time just erase append command (ios::app)