Hi guys, I'm writing a program but I ran into a run-time error. I know it sounds ambiguous but the issue only just came up when I wrote a few lines of code. The function is suppose to deal with cases where the first letters are upper case, but I get a run-time error before I even hit the first if statement where variable a is being tested. I tried outputting before the if statement and it shows on the console but output in the console doesnt occur. Why am I getting this run-time error? Thank you.
Code:
void piratify(string &s){
//Changes specific words
int caps=0;
int a=(int)(s.at(0));
if((a>=65)&&(a<=90)){
caps++;
}
if(s=="hello ")
s="ahoy ";
else if(s=="sir ")
s="matey ";
else if(s=="madam ")
s="proud beauty ";
else if(s=="officer ")
s="foul blaggart ";
else if(s=="stranger ")
s="scurvy dog ";
else if(s=="where ")
s="whar ";
else if(s=="is")
s="be ";
else if(s=="the ")
s="th' ";
else if(s=="my ")
s="me ";
else if(s=="your ")
s="yer ";
else if(s=="restaurant ")
s="gallery ";
else if(s=="hotel ")
s="fleabag inn ";
if(caps==1){
s.at(0)-=32;
}
}
Just in case anyone wants to see the whole code
Code:
#include<iostream>
#include<string>
#include<ctime>
#include<cstdlib>
using namespace std;
void cleanup(string &s);
void piratify(string &s);
void ioncannon(string s, string &m,string p, char a);
void pirate(string &s);
void arr(string &s);
void punctuation (string &s, char &a, string &p);
void checker (string s, string b);
int main()
{
string s;
srand(time(0));
cout<<"Enter string: ";
getline(cin,s);
cleanup(s);
pirate(s);
cout<<s;
cin.get();
cin.ignore();
}
void pirate(string &s){
//Calls everything, with the substrings as variables
int n=0,i=0,k=0;
string c,m,p;
char a;
while(n!=-1){
n=s.find(" ",i+1);
c=s.substr(k,n+1-k);
p=c;
punctuation(c,a,p);
piratify(c);
ioncannon(c,m,p,a);
k=n+1;
i=n;
}
s=m;
}
void cleanup(string &s)
{
//Cleans string for easy reading
string c,b;
for(int i=0;i<s.length();i++){
if(s.at(i)>=65&&s.at(i)<=90)
s.at(i)+=32;
}
s.resize(s.length()+1,' ');
}
void piratify(string &s){
//Changes specific words
int caps=0;
int a=(int)(s.at(0));
if((a>=65)&&(a<=90)){
caps++;
}
if(s=="hello ")
s="ahoy ";
else if(s=="sir ")
s="matey ";
else if(s=="madam ")
s="proud beauty ";
else if(s=="officer ")
s="foul blaggart ";
else if(s=="stranger ")
s="scurvy dog ";
else if(s=="where ")
s="whar ";
else if(s=="is")
s="be ";
else if(s=="the ")
s="th' ";
else if(s=="my ")
s="me ";
else if(s=="your ")
s="yer ";
else if(s=="restaurant ")
s="gallery ";
else if(s=="hotel ")
s="fleabag inn ";
if(caps==1){
s.at(0)-=32;
}
}
void ioncannon(string s, string &m,string p, char a){
//Adds everything back on
int n=p.find_first_of(a);
int k=s.length()-1;
if(a!=' '&&n!=-1){
s.at(k)=a;
s+=' ';
}
m+=s;
arr(m);
}
void punctuation (string &s, char &a, string &p){
//Punctuation checker
int find = s.find_first_of(",.?!");
if(find!=-1){
a = s.at(find);
s.resize(find+1);
p=s;
s.at(find)=' ';
}
}
void arr(string &s)
{
int n=0;
n=rand()%2;
if(n==1)
s+="Arrr! ";
}