I've got this simple code I've been trying to do for HOURS and I can't figure it out. I aim for the program to be a test creator, not with integers, but with actualy sentences, a program that could make English tests, with sentences, and I want the test to be saved, so I put some code to write on a text file. If I make the code that asks what they want to call the test it works like it should, except it wouldn't be saved, but if it isn't in a comment the question is skipped and it moves onto the answer, below is the code.
if you compile and run this application beware that if you input a file name that has already been created it may overwrite it. You've been warned.
#include <iostream>
#include <cmath>
#include <fstream>
#include <cstdlib>
#include <string>
usingnamespace std;
int main()
{
system("TITLE Test Maker");
char filename1[99];
string q1;
string a1;
cout << "What do you want to name the test? If you name it over an already created file" << endl;
cout << "it WILL OVERWRITE it." << endl;
cin >> filename1;
cout << "Write in your questions then answers, when you're done, exit the program." << endl;
cout << "Question 1: " << endl;
cout << "What is your question?";
getline (cin, q1);
cout << "What is the answer?";
getline (cin, a1);
ofstream myfile;
myfile.open(filename1);
myfile << q1, "\n" a1;
myfile.close();
system("PAUSE");
return 0;
}
I've gotten it to save now... but I haven't achieved the user being able to enter a custom name, I used "test1.txt" and it worked, but I can't do custom, any help?
#include <iostream>
#include <cmath>
#include <fstream>
#include <cstdlib>
#include <string>
#include <sstream>
usingnamespace std;
int main()
{
system("TITLE Test Maker");
char filename1[99];
string q1;
string a1;
/*cout << "What do you want to name the test? If you name it over an already created file" << endl;
cout << "it WILL OVERWRITE it." << endl;
cin >> filename1;*/
cout << "Write in your questions then answers, when you're done, exit the program." << endl;
cout << "Question 1: " << endl;
cout << "What is your question?";
getline (cin, q1);
cout << "What is the answer?";
getline (cin, a1);
ofstream myfile;
myfile.open("test1.txt");
myfile << q1 << endl << a1;
myfile.close();
system("PAUSE");
return 0;
}
It worked =)))))). Thank you! But how do I get it to be a text file or another file besides the deafult type that makes you pick what you want to open it in?
Liek a "txt" file. It, by default, is a "file", that's what it says in the properies. I don't want it to be that because it then makes you decide what you want to open it with. If you don't understand would you please compile and just run it, make 1 file, no content is required and then look in the folder you ran the program in, the "file" will be in that folder.
after line 18, you have the name of the file in the var. filename1 without any extension so it will be opened with that name (wihtout extention) at your OS ( probally windows) and as default it will ask what program to use to open that file. the proposed solution, by adding .txt extention to your file name, should aim to change the filename1 var. to extend it in order to include .txt .
so the solution would be to add some kinf of manipulation at the string after line 18. at C the strcat() did the trick i'm not sure about C++, still pretty noob :)