I can’t tell what’s wrong with my code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string test;
cin>>test;
if (test[4] == ' ')
{
test[4] = '+';
}
cout<<test<<endl;
system("PAUSE");
return 0;
}
I’m basically asking the user to give me a string (the string will be “star trek”) and then, I want the string to be replace with a ‘+’. For some reason, I’m getting this whenever I try it. However, when I run this, I get a dialog box that pops up and says “Debug Assertion Failed” and “Expression: String subscript out of range”.
Use
getline(cin,test)as it won’t skip whitespace. Also, please use cin.get() instead ofSystem("Pause").Something like this would be better.