Program Skipping getline Statement

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;
Last edited on
1
2
3
4
5
getline( cin, filename );

// change to

getline (cin, filename, '\n');
I tried that and the same thing happened...thanks though!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>
using namespace 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.
Last edited on
Ok, so here is my program.
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 <iomanip>
#include <string>
#include <fstream>
using namespace 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;
}
Last edited on
1
2
cout << "Please enter a shift value: "; 
cin >> shiftValue;


This part of your encrypt function will leave junk in the input buffer. You'll need to extract the other stuff that could be after the number you got:

std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

Put that after you get integers using the formatted extraction operator (>>).

Also, your touppercase function is really, really slow. >_> I'd take a look at what you are doing there.
Last edited on
Thanks! I can look at that. Do you know how to get it to properly write to a file?
When I add this:
 
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');


It pops up with 5 errors:

(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
(71) : error C2780: 'const _Ty &std::max(const _Ty &,const _Ty &,_Pr)' : expects 3 arguments - 0 provided
c:\program files\microsoft visual studio 9.0\vc\include\xutility(3390) : see declaration of 'std::max'
(71) : error C2780: 'const _Ty &std::max(const _Ty &,const _Ty &)' : expects 2 arguments - 0 provided

How do I fix those?
(71) : error C2780: 'const _Ty &std::max(const _Ty &,const _Ty &,_Pr)' : expects 3 arguments - 0 provided
c:\program files\microsoft visual studio 9.0\vc\include\xutility(3390) : see declaration of 'std::max'
(71) : error C2780: 'const _Ty &std::max(const _Ty &,const _Ty &)' : expects 2 arguments - 0 provided


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?


1
2
3
4
5
6
7
...
ofstream fout;
fout.open ("sometextfile.txt");
// error checking goes here
fout << "Stuff.\n";
fout.close ();
...


Also, easier way to convert characters to upper case:

1
2
3
4
5
...
string szInput = "TeSt123";
for (int i = 0; i < szInput.length(); i++)
    szInput[i] = toupper (szInput[i]);
...
Topic archived. No new replies allowed.