Passing a user supplied variable to a function

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
Make filename, filename.c_str().
Thanks for the help. I have been working on this for a while.
It turns out that I still need help. I'm trying to get it to loop until the user presses "Q".

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; 
} 
Try using the function 'strcmp' instead of the arithmetic comparison operator.

http://www.cplusplus.com/reference/clibrary/cstring/strcmp/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//Not a complete program
string str0, str1, str2;

str0 = "hello";
str1 = "hello";
str2 = "hell0";

if(strcmp(str0, str1) == 0)
{
    cout << "Strings are equal." << endl; 
}

if(strcmp(str0, str2) != 0)
{
    cout << "Strings are not equal." << endl; 
}
The output is:

In function `int main()':
expected primary-expression before "else"
expected `;' before "else"
What code gave you that error?

(Post your changes.)
1
2
3
4
5
6
7
ofstream myfile;
filename.append(".txt"); 
myfile.open(filename.c_str()); // <--- this semicolon is your problem
if (myfile.is_open() == false) 
{ 
cout << "Failed to open file"; 
} 
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
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
It compiles, but doesn't loop.
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
It still doesn't do what I want. Oh well, of to classes.
Topic archived. No new replies allowed.