I have tried countless times to solve this and nothing on the internet is helping. I've figured that the problem is with the "getline(cin,strg);" but i dont know what, the program runs perfectly if I just use "cin >> strg;", however, i want to account for spaces and whatnot. What is the problem?
#include <iostream>
#include <stdlib.h>
using namespace std;
string alphabet = "abcdefghijklmnopqrstuvwxyza";
string alphacap = "AbcdEfghIjklmnOpqrstUvwxyzA";
int loc;
string code(string str) {
for(int i = 0; i <= str.length()-1; i++){
if(alphabet.find(str[i]) != str.npos){
loc = alphabet.find(str[i]);
str = str.substr(0,i) + alphacap[loc+1] + str.substr(i+1,str.length());
}
}
return str;
}
string decode(string str) {
for(int i = 0; i <= str.length()-1; i++){
if(alphabet.find(str[i]) != str.npos){
loc = alphabet.find(str[i]);
str = str.substr(0,i) + alphacap[loc-1] + str.substr(i+1,str.length());
}
}
return str;
}
int main() {
string codc;
do{
system("cls");
cout << "Code or Decode? ";
cin >> codc;
}while(codc != "code" and codc != "Code" and codc != "decode" and codc != "Decode");
string str;
if(codc == "code" or codc == "Code"){
string strg;
getline(std::cin,strg);
cout << code(strg);
}else{
string strg;
getline(cin,strg);
cout << decode(strg);
}
}