-2

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?

akaManen
  • 27
  • 6
  • 1
    Lots of magic numbers in this code, who knows if they are correct. Please post a [mre]. – Paul Sanders Apr 22 '21 at 17:51
  • 2
    A note on the [mre]: They can be hard to make, but they are worth it. MRE is a powerful debugging technique and you rarely have to finish the job before the reduced noise around the bug allows you to see and fix the bug. If you make a MRE early in the question-asking process, the process usually comes to a quick end. – user4581301 Apr 22 '21 at 17:56
  • 1
    @user4581301 I think the question is complete now, could you please tell me if you understand the problem or have any ideas? – akaManen Apr 22 '21 at 18:51
  • @akaManen I've voted to re-open the question, but the second point in Bleh's answer is correct. the `16` should be `stati[i].size()` or you should use a [range-based `for` loop](https://en.cppreference.com/w/cpp/language/range-for) so the number of iterations becomes controlled by the system for you. Code you don't have to write has no bugs. Or if it does, at least they aren't YOUR bugs. – user4581301 Apr 22 '21 at 19:05
  • @user4581301 Replacing 16 with stati[i].size() fixed everything, if you write it in an answer i'll accept it. Thank you! – akaManen Apr 22 '21 at 19:14

1 Answers1

1

Couple of things wrong with your code :

  1. you need to place double quotes around the string characters you've defined . For example: std::string colour[4] = { "Blue", "Red", "Orange", "Yellow" };

  2. The size 16 makes sense for the last word , where size is 16, but for all the other words, you're still trying to access the positions which do not exist. maybe set a variable to the size of each word to solve this problem .

you could also put the if (stati[i][k] == ' ') condition before the loop, so it enters in only that case

bleh
  • 32
  • 3
  • https://www.geeksforgeeks.org/array-strings-c-3-different-ways-create/ is 5 ways to correctly define a string array – bleh Apr 22 '21 at 18:26