String subscript out of range
Aug 5, 2014 at 1:50am UTC
I am trying to write a function that will read a command and then allow the script to continue, but I get an error that says the string subscript is out of range.
This is on the callstack:
ProjectAlpha.exe!commandCheck(std::basic_string<char,std::char_traits<char>,std::allocator<char> > command) Line 25 C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
int commandCheck(std::string command){//will be able to read up to a 10 character user command
int repeat = 0;
int cp = 0;//character postition
int cp2 = 0;
while (repeat == 0){
std::cin >> globalTempInput;
if ((globalTempInput[cp] == command[cp2]) && (globalTempInput[cp + 1] == command[cp2 + 1]) && //reads the command, then checks to see if it matches what the command is supposed to be
(globalTempInput[cp + 2] == command[cp2 + 2]) && (globalTempInput[cp + 3] == command[cp2 + 3]) &&
(globalTempInput[cp + 4] == command[cp2 + 4]) && (globalTempInput[cp + 5] == command[cp2 + 5]) &&
(globalTempInput[cp + 6] == command[cp2 + 6]) && (globalTempInput[cp + 7] == command[cp2 + 7]) &&
(globalTempInput[cp + 8] == command[cp2 + 8]) && (globalTempInput[cp + 9] == command[cp2 + 9]) &&
(globalTempInput[cp + 10] == command[cp2 + 10]) && (globalTempInput[cp + 11] == command[cp2 + 11]) &&
(globalTempInput[cp + 12] == command[cp2 + 12]) && (globalTempInput[cp + 13] == command[cp2 + 13]) &&
(globalTempInput[cp + 14] == command[cp2 + 14]) && (globalTempInput[cp + 15] == command[cp2 + 15]) &&
(globalTempInput[cp + 16] == command[cp2 + 16])
){
repeat = 1;
}
else {
std::cout << "INVALID COMMAND" << std::endl;
repeat = 0;
}
}
return 0;
}
Aug 5, 2014 at 2:08am UTC
Why not just do if (globalTempInput == command)
?
Aug 5, 2014 at 2:13am UTC
I guess I over thought it a lot, thanks!
Topic archived. No new replies allowed.