Actually, the program works fine in Devc++ but gives me the error if i run it in VisualStudio, does anybody know why this happens?
The program should check if it has to cout a string from the stati array with every first letter of each word capitalized and then it has to convert the string back to lowercase, statoScelto[] just checks if the string has to be printed.
#include <iostream>
#include <string>
#include <algorithm>
int main() {
std::string stati[4] = { "italia", "francia", "spagna", "bosnia erzegovina" };
bool statoScelto[4] = { true, false, false, true };
int i, k;
// here there is other code that eventually changes statoScelto[] values.
for (i = 0; i < 4; i++) {
if (statoScelto[i] == true) {
stati[i][0] = toupper(stati[i][0]);
for (k = 0; k < 16; k++) {
if (stati[i][k] == ' ') {
stati[i][k + 1] = toupper(stati[i][k + 1]);
}
}
std::cout << "\n" << stati[i];
for (k = 0; k < 16; k++) {
if (stati[i][k] == ' ') {
stati[i][k + 1] = tolower(stati[i][k + 1]);
}
}
stati[i][0] = tolower(stati[i][0]);
}
}
system("pause>0");
}
as it is now, the program should print:
Italia
Bosnia Erzegovina
but when it is run a string subscript out of range error would pop up. Does anybody understand what is wrong with it?