-2

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);
    }

}
1
  • 1
    What to do you want to count? Also, which is the input you are giving? Commented Feb 6, 2016 at 19:07

2 Answers 2

0

The exception is caused by:

str.substr(i + 1, str.length());

as it tries to access str's elements up to i + str.length(). This fails for i > 0. See the original Q&A.

str.substr(i + 1, str.length() - i - 1); would correct it.

If you want to get the part of the string on the right of the index i, you can just leave out the second argument:

str.substr(i + 1);

See the reference.

And this is dangerous too:

alphacap[loc + 1]
alphacap[loc - 1]

What if loc is the index of the first/last element in alphacap?

Sign up to request clarification or add additional context in comments.

4 Comments

I fixed what you said the problem was and it's still giving me trouble. Why does it work when i use "cin >> strg"?
@Deck I can't say without knowing what the input is in both cases.
every input seems to work. Lets just say it's "Hello world"
@Deck Your code obviously can't handle spaces. It requires each character be a letter.
0

After getline(std::cin,strg) strg=="".
In loop:

for(int i = 0; i <= str.length()-1; i++)
comparison <= not work correct if str.length() ==0.
After changing to

for(int i = 0; i <= (int)str.length()-1; i++)
program not fail.

Preferable also change cin >> codc; to getline(cin,codc);

Now program not fail.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.