I am working on an encryption code and am just about done. Now I just need to ask the user what to name the file that they want to write the encryption to. However, the program does not allow the user to input a name, even though the code is written to do so. I have posted the section of my code that deals with this. If anyone can see something wrong, please let me know. Thanks in advance for your help!
1 2 3 4 5 6 7 8 9
ofstream f;
string filename;
cout << "What would you like to name the encrypted file? ";
getline( cin, filename );
f.open(filename.c_str());
f << cipher[i];
cout<< "File has been encrypted."<<endl;
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
string szInput;
cout << "Enter a string, and I will print it back to you: ";
getline (cin, szInput, '\n');
cout << szInput << endl;
cin.get();
return 0;
}
Then you need to post your entire program, instead of a chunk of code, because there's likely something else happening before you call the getline function that's affecting your console input.
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
usingnamespace std;
{
ofstream f;
string filename;
cout << "What would you like to name the encrypted file? ";
getline(cin, filename, '\n');
f.open(filename.c_str());
f << cipher[i];
cout<< "File has been encrypted."<<endl;
}
int main()
{
openfile();
encrypt();
return 0;
}
When a function definition contains arguments, you need to pass arguments to the function call unless initial values are set in the function prototype. Sooo:
1 2 3 4 5 6 7 8 9
...
my_function (one); // won't work in most cases
my_function (); // wont work in most cases
my_function (one, two); // function expects 2 arguments, and you've provided 2 arguments, everyone's happy
...
void my_function (argument one, argument two)
{
...
}
(71) : error C2039: 'numeric_limits' : is not a member of 'std'
(71) : error C2065: 'numeric_limits' : undeclared identifier
(71) : error C2275: 'std::streamsize' : illegal use of this type as an expression
Fix that by using cin.ignore (1000, '\n'); instead of something that looks more complicated than it needs to be.
Do you know how to get it to properly write to a file?