Apr 27, 2009 at 7:32pm UTC
It is my first time posting here, so please bear with me. I have been trying to make a simple text-editor using the Command prompt. It glitches at line 15. The program works if I don't make the user input the file name. I'm using Dev-C++.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string mystr;
string filename;
cout << "Enter the file name to create: " ;
getline(cin, filename);
cout << "Type anything to save to " << filename << ": " ;
getline(cin, mystr);
ofstream myfile;
myfile.open(filename);
if (myfile.is_open() == false )
{
cout << "Failed to open file" ;
}
else
{
cout << "File saved successfully" ;
}
myfile << mystr;
myfile.close();
char responce;
cin >> responce;
return 0;
}
Last edited on Apr 27, 2009 at 7:38pm UTC
Apr 27, 2009 at 7:47pm UTC
Make filename, filename.c_str().
Apr 27, 2009 at 7:56pm UTC
Thanks for the help. I have been working on this for a while.
Apr 28, 2009 at 9:01pm UTC
The output is:
In function `int main()':
expected primary-expression before "else"
expected `;' before "else"
Apr 28, 2009 at 9:43pm UTC
What code gave you that error?
(Post your changes.)
Apr 28, 2009 at 11:21pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string mystr;
string filename;
cout << "Enter the file name to create: " ;
getline(cin, filename);
cout << "Type anything to save to " << filename << "(Press 'Q' to quit): " ;
int n = 1;
while (n == 2)
{
getline(cin, mystr);
if (mystr!="Q" );
{
n = 1;
}
else
{
n = 2;
}
}
ofstream myfile;
filename.append(".txt" );
myfile.open(filename.c_str());
if (myfile.is_open() == false )
{
cout << "Failed to open file" ;
}
else
{
cout << "File saved successfully" ;
}
myfile << mystr;
myfile.close();
char responce;
cin >> responce;
return 0;
}
The output:
In function `int main()':
expected primary-expression before "else"
expected `;' before "else"
Last edited on Apr 28, 2009 at 11:22pm UTC
Apr 28, 2009 at 11:54pm UTC
myfile.open(filename.c_str()); // <--- this semicolon is your problem
@Warnis: Why is that semi-colon a problem? It should be there.
@lotios:
Line 18. Remove ; after if
Last edited on Apr 28, 2009 at 11:56pm UTC
Apr 29, 2009 at 10:09am UTC
It compiles, but doesn't loop.
Apr 29, 2009 at 12:42pm UTC
Well, that's because your while loop's condition is n == 2, which as per your code is obviously false since n = 1. So if you want to loop it then put n != 2
Last edited on Apr 29, 2009 at 12:43pm UTC
Apr 30, 2009 at 12:40am UTC
It still doesn't do what I want. Oh well, of to classes.