|
|
|
#1 (permalink) | ||||||||||||||
|
Security Sleuth
|
Well, after long discussion and bugging of Mr. Hobo (as the usual) we couldn't get this going.
I need to ask the user to input the name of a file, then the app then uses what they entered for the name of the file to be written and saved. Tried everything from const to string and we it gives an error somewhere. Help please? Code:
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
int main() {
const char * filename; //variable to store file name?
ofstream the_file; //variable to refer to stream to write to file ?
cout<<"what do you want the name of your webpage to be? NOT the title!";
cin>>filename;
// open stream/open file of output?
the_file.open (&filename,ios::out);
return 0;
}
I need to ask multiple questions and wrap different tags around the users input for each question.
__________________
Quote:
Proud Member of the Linux Gaming CommunityI am your friend.
Last edited by Pooping^fish : 03-29-08 at 01:16 AM. |
||||||||||||||
|
|
|
|
|
#2 (permalink) | |||||||||||||
|
*cough* Stock *cough*
|
Whats this being done in? VB, C++, C#
__________________
|
|||||||||||||
|
|
|
|
|
#3 (permalink) | |||||||||||||
|
Off By 340 Undecillion
|
A few things wrong I see, assuming its C++.
First, where you declare the filename variable, you have declared it as a constant. When you declare a var as a constant, that means you cant change it anywhere in your program. Notice later you try to read into filename. Also you are probably going to want to make filename an array or a string. Here is what I'd write for your code. Code:
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char filename[51]; //variable to store file name //declares an array of characters to store the file name, 50 chars long.
ofstream the_file; //variable to refer to stream to write to file
cout <<"What do you want the name of your webpage to be? NOT the title!";
cin >> filename; //depending on what you want to allow to be as the file name, consider using cin.get?
//open stream/open file of output? //you don't need to have all that other junk in there.
the_file.open(filename);
return 0;
}
__________________
Congratulations! You have found the secret text! You get a cookie.
|
|||||||||||||
|
|
|
|
#4 (permalink) | ||||||||||||||
|
Security Sleuth
|
Sweet, thankyou. That worked.
I figured out I guess I dont exactly need to use a loop, perhaps a function and string to finish this. It will ask multiple questions requiring user input, and it will put text around (begginning and end) of the input. Im confused as exactly how to get it to take input from the user and put the specified text around that. Which one side will differ from another.
__________________
Quote:
Proud Member of the Linux Gaming CommunityI am your friend.
|
||||||||||||||
|
|
|
|
|
#5 (permalink) | |||||||||||||
|
Off By 340 Undecillion
|
Will the surrounding tags depend on the answers or the questions? If the tags depend on the question you could follow something like:
Write opening tag 1 to file ask question 1 write answer to question 1 to the file write closing tag 1. write opening tag 2 ask question 2 ..... If the tags depend on the answer you would have to find some way to temporarily store the answer, in a string, array, etc. example: Ask question 1. store answer 1 in an array analyze answer 1 to choose which tags to use write opening tag 1 to file write answer 1 to file write closing tag 1. ask question 2....
__________________
Congratulations! You have found the secret text! You get a cookie.
|
|||||||||||||
|
|
|
|
#6 (permalink) | ||||||||||||||
|
Security Sleuth
|
Code:
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char filename[51]; //variable to store file name //declares an array of characters to store the file name, 50 chars long.
cout <<"What do you want the name of your webpage to be? NOT the title!";
cin >> filename; //depending on what you want to allow to be as the file name, consider using cin.get?
string titlea = ("<title>");
string titleb = ("</title>");
string title = ("titlea, title, titleb");
cout <<"What do you want the title to be?";
cin >> title;
ofstream the_file; //variable to refer to stream to write to file
the_file.open(filename);
system("PAUSE");
return 0;
}
The file still writes, and the file name is written, but my title isnt included at all? I tried the_file title; and it just gave an error. Shouldnt I use this to write to the file?
__________________
Quote:
Proud Member of the Linux Gaming CommunityI am your friend.
|
||||||||||||||
|
|
|
|
|
#7 (permalink) | ||||||||||||||
|
Security Sleuth
|
Bartender-Yes the tags will depend on the question.
Could I use seperate functions for each or would that be too much of a hassle?
__________________
Quote:
Proud Member of the Linux Gaming CommunityI am your friend.
|
||||||||||||||
|
|
|
|
|
#8 (permalink) | ||||||||||||||
|
Off By 340 Undecillion
|
Quote:
If you want you can use a function, for small things like this I consider it a matter of style.
__________________
Congratulations! You have found the secret text! You get a cookie.
|
||||||||||||||
|
|
|
|
#9 (permalink) | ||||||||||||||
|
Security Sleuth
|
Ive tried this.
What you said kindof confused me. What do I use to have the variable title written to the file? No matter what I try nothing biut the filename gets written. It completely ignores the input for the title.
__________________
Quote:
Proud Member of the Linux Gaming CommunityI am your friend.
|
||||||||||||||
|
|
|
|
|
#10 (permalink) | |||||||||||||
|
Programmer
|
I'm probaly resurecting a dead horse but is this what you are after?
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
// Create the varible used to store the filename
char filename[64];
// Create the vaibles used to wrap a user input title around some tags
string TitleStartTag = "<title>";
string TitleEndTag = "</title>";
string Title;
// Get the filename from the user
cout << "Please enter the filename: ";
cin >> filename;
cout << endl << endl;
// Get the title from the user
cout << "Please enter the title: ";
cin >> Title;
// Create the filestream object
ofstream out_file;
// Open the file
out_file.open(filename);
// Write the title varibles to the file
out_file << TitleStartTag << Title << TitleEndTag << endl;
// Always must ensure the file is closed when were done with it
out_file.close();
system("PAUSE");
return 0;
}
![]()
__________________
3DMark06: 15393
Last edited by Gen : 05-10-08 at 10:27 PM. |
|||||||||||||
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|