std out of range?


hi i am fairly new to C++ and i am making a program that encrypts and decrypts messages with a cypher i made. how it works is i input a letter and it gives me the number of where it is placed in the string and vice versa for decrypting however where i am having my problem is every time i run my decrypt function and try to end it crashes and tells me std out of range

so if you can help me figure out what i did wrong and how to fix i would be very thankful
or a overall just better way to do it that would be good aswell
[code]
#include <iostream>
using namespace std;
int encrypt();
int decrypt();
int second();

int main(){
int whatTheyWant;

cout<<" -SHADOW'S-"<<endl;
cout<<" -KEY-"<<endl;
cout<<"input 1 to encrypt"<<endl;
cout<<"input 2 to decrypt"<<endl;
cin>>whatTheyWant;
switch(whatTheyWant){
case 1:
encrypt();
break;
case 2:
decrypt();
break;
}
}
int second(){

int WhatTheyWantSecond;

cout<<"input 1 to encrypt"<<endl;
cout<<"input 2 to decrypt"<<endl;
cin>>WhatTheyWantSecond;
switch(WhatTheyWantSecond){
case 1:
encrypt();
break;
case 2:
decrypt();
break;
}
}

int encrypt(){
string x (" ");
string one("qwertyuiopasdfghjklzxcvbnm ");
cout<<"input one letter at a time to encrypt, enter end when you are finished"<<endl;

do {
cin>> x;
cout<<": " << one.find(x)<<endl;
} while(x != "end");

second();
return 0;
}

int decrypt(){
int Choice;
string s1 = ("qwertyuiopasdfghjklzxcvbnm ");
cout << "input one number at a time to decrypt, when you are done enter -1"<<endl;
do {
cin>> Choice ;
cout <<":"<< s1.at(Choice)<<endl;
} while(Choice != -1);

second();
return 0;
}

[/code}

oh and i know that the qwerty layout isn't a cypher i have that there just for an example
Last edited on
It throws an exception because you are passing an invalid index (-1) to the string's at function.
oh ok i get it now why it gives me an error ,because it's not in the string so it doesn't know what to do with what i put in, so is there a way to end the function then
never mind i added a space in the front of the string to hold the 0 spot and q would be one now 0 terminates it and it works fine thank you for your help
Topic archived. No new replies allowed.