i was trying to to make a program where i was learning how to use header file and i got into an issue
salt.h
#pragma once
#include<string>
class salting {
public:
char encrypt(std::string&);
char decrypt(std::string&);
};
salt.cpp
#include "salt.h"
#include<iostream>
char salting::encrypt(std::string& s)
{
size_t i = 0;
std::cout << s;
if (s[0] % 2 == 0) { //shows an error of exception unhandled std::out of range
for (char cl : s) {
if (i % 2 != 0 && i > 2) {
cl++;
s.at(i) = cl;
}
i++;
}
}
else {
for (char cl : s) {
if (i % 2 == 0 && i > 2) {
cl++;
s.at(i) = cl;
}
i++;
}
}
return 'c';
}
char salting::decrypt(std::string& s)
{
size_t i = 0;
if (s.at(0) % 2 == 0) { //shows an error of exception unhandled std::out of range
for (char cl : s) {
if (i % 2 != 0 && i > 2) {
cl--;
s.at(i) = cl;
}
i++;
}
}
else {
for (char cl : s) {
if (i % 2 == 0 && i > 2) {
cl--;
s.at(i) = cl;
}
i++;
}
}
return'c';
}
test.cpp
#include<iostream>
#include "salt.h"
using namespace std;
int main() {
salting l;
string s1 {};
cout << "enter the string to encrypt: ";
cin.ignore('\n',INT_MAX);
s1.clear();
getline(cin, s1);
l.encrypt(s1);
cout << endl << "the salted string is: " << s1;
l.decrypt(s1);
cout << endl << "the original string is: " << s1;
}
i dont know what happens as I am still learning programming the best i can state is that the getline function takes the input after which nothing happens but after i press enter 2-3 times it shows an: "Unhandled exception at 0x76804192 in test code.exe: Microsoft C++ exception: std::out_of_range at memory location 0x003AF704".Near the comment i put in salt.cpp after which if i press countinue it shows: "Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention." thanks in advance.