#include <iostream>
#include <string.h>
using namespace std;

int main()
{
char clear[200];
char cipher[200];
int x,i;
int opt;

cout<<"Encrypt (1) or Decrypt (2):";
cin>>opt;
if(opt==1) //my problem is its not giving me option to enter string
{

cout<<" Enter a string:";
cin.getline(clear,sizeof(clear));

x = strlen(clear);

for(i=0;i<=x-1;i++)
{
cipher = clear+3;
}
cipher[x] = '\0';
cout<<" Encrypted:" << cipher << endl;

Hey guys I'm having trouble with Encrypt and Decrypt option when i enter my option it is not leaving me enter my string, im new to programming and cant see were the problem is.i have also a decrypted part to this code also. but just posted encrypted part as my problem is at the beginning of the program thanks in advanced guys

Recommended Answers

All 4 Replies

You are mixing >> and getline() in the same program. >> leaves the terminating new line char in the input buffer. The new line char is the terminating char for getline() so it sees the new line char in the input buffer left by >> as the first char in the input buffer and stops any further input into the input buffer, making it look like it didn't do anything.

Either:
a) Never mix input methods in the same program or
b) Be sure the input buffer is cleared after each input method or before each subsequent input method is called.

You are mixing >> and getline() in the same program. >> leaves the terminating new line char in the input buffer. The new line char is the terminating char for getline() so it sees the new line char in the input buffer left by >> as the first char in the input buffer and stops any further input into the input buffer, making it look like it didn't do anything.

Either:
a) Never mix input methods in the same program or
b) Be sure the input buffer is cleared after each input method or before each subsequent input method is called.

Thanks for the input. how would i go about making sure the input buffer is cleared before each method is called.

look up the ignore() method of the istream class. Leaving the parameter list blank ignores the first value in the input buffer, which is often, though not always adequate. You can ignore anywhere from one to the maximum size of the input buffer with any given call. You can determine how many char you want to ignore. The syntax would be:

cin.ignore();

or

cin.ignore(1080);
which would ignore up to 1080 char or the new line char, whichever comes first

or

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

which would ignore up to the maximum size of the input buffer (whatever that is defined to be in the implementation of your compiler) or the new line char, whichever comes first. To use this syntax you need to include the ios and the limits header files. This last syntax may be the most "robust" way to do it, but it also is the most inconvenient. You get to to choose the relative convenience vs robustness you want your program to have.

look up the ignore() method of the istream class. Leaving the parameter list blank ignores the first value in the input buffer, which is often, though not always adequate. You can ignore anywhere from one to the maximum size of the input buffer with any given call. You can determine how many char you want to ignore. The syntax would be:

cin.ignore();

or

cin.ignore(1080);
which would ignore up to 1080 char or the new line char, whichever comes first

or

cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
Thanks for the help, got it working cheers:)

Thanks got it working. your a great help.

which would ignore up to the maximum size of the input buffer (whatever that is defined to be in the implementation of your compiler) or the new line char, whichever comes first. To use this syntax you need to include the ios and the limits header files. This last syntax may be the most "robust" way to do it, but it also is the most inconvenient. You get to to choose the relative convenience vs robustness you want your program to have.

Thanks got it working. you've been a great help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.