0

Please assume the user will always type in one, I have not gotten to the part of the code where the user will input text to decrypt. Anyways, whenever I run this code the program seems to just skip over the first word in user input and encrypt the rest of the text. I am completely dumbfounded as to why this is.

#include<iostream>
#include<string>
#include<fstream>
using namespace std;

int main()
{

cout<< "Do you want to encrypt or decrpyt your file?" << endl;
cout<< "Type 1 for encrytion"<<endl;
cout<< "Type 2 for decrption"<<endl;
string silence;
int choice;

cin>> choice;

if(choice== 1)
{
cout<< "Please enter a string to encrypt: ";
cin>> silence;
getline(cin,silence);



for(int i =0; i < silence.length();i++)
{
    if (isalpha(silence[i]))
     cout<< char(silence[i]+1);
    else cout<<silence[i];


}



}
Alan Watts
  • 33
  • 1
  • 7
  • 1
    You may find this interesting: ["Why does `std::getline` skip input after a formatted extraction?"](http://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction). – WhozCraig Apr 18 '14 at 15:58

2 Answers2

0

Don't redirecting cin into silence before calling getline with silence as a parm, that's how you're overwriting the first word.

Paul Evans
  • 26,111
  • 3
  • 30
  • 50
0
cin>>silence

puts the text that the user input into silence So there is no need to call getLine();

  • I need the getline function so that I can encrypt a line of code instead of just one word. Deleting cin>> silence seems to be the key, but when I run the program I am not asked to input any text at all. – Alan Watts Apr 18 '14 at 16:12