My code fully compiles and then shows nothing

it is a simple program that separates two 5 letter words, with a space after each letter, and as the title says, after it successfully compiles, it doesn't run.

Letras.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef LETRAS_H_INCLUDED
#define LETRAS_H_INCLUDED
#endif // LETRAS_H_INCLUDED

#include<string>

class Letras

{
    public:

        explicit Letras();

        void establecerLetras( std::string );

        std::string obtenerLetras() const;

        void mostrarMensaje() const;

    private:

        std::string miPalabra;
};


Letras.cpp

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include"Letras.h"
#include<iostream>
#include<string>

Letras::Letras()

{
    establecerLetras( miPalabra );
}

void Letras::establecerLetras( std::string miPalabra )

{
    if( miPalabra.size() <= 5 )

    {
        miPalabra.substr( 0, 0 );

        obtenerLetras();

        std::cout << " ";

        miPalabra.substr( 1, 1 );

        obtenerLetras();

        std::cout << " ";

        miPalabra.substr( 2, 2 );

        obtenerLetras();

        std::cout << " ";

        miPalabra.substr( 3, 3 );

        obtenerLetras();

        std::cout << " ";

        miPalabra.substr( 4, 4 );

        obtenerLetras();

        std::cout << " ";

    } else

    {
        miPalabra.substr( 0, 4 );
    }
}

std::string Letras::obtenerLetras() const

{
    return miPalabra;
}

void Letras::mostrarMensaje() const

{
    std::cout << " La palabra completa es : " << obtenerLetras() << "!" << std::endl;
}


main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include"Letras.h"
#include<iostream>

int main()

{
    Letras letra1;
    Letras letra2;

    std::cout << " La palabra 1 se separa en : ";

    letra1.establecerLetras( "cosas" );

    std::cout << " \n\nLa palabra 2 se separa en : ";

    letra2.establecerLetras( "voces" );

    std::cout << std::endl;

    return 0;
}


This is the error I get at when I run the program:

terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::substr: __pos (which is 1) > this->size()
(which is 0) Aborted (core dumped) process returned 134 (0x86) execution time : 0.203 s
press ENTER to continue.
Last edited on
Well, the program starts at main(), line 7
 
    Letras letra1;

That defines an object of type Letras and the constructor is called.
1
2
3
4
Letras::Letras()
{
    establecerLetras( miPalabra );
}


That constructor calls function
establecerLetras()


Here is the beginning of that function.
11
12
13
14
15
16
17
18
19
20
21
22
23
void Letras::establecerLetras( std::string miPalabra )

{
    if( miPalabra.size() <= 5 )

    {
        miPalabra.substr( 0, 0 );

        obtenerLetras();

        std::cout << " ";

        miPalabra.substr( 1, 1 );

This is right at the start of execution, the string miPalabra is empty, its size is zero. At line 23 it tries to obtain a substring starting at position 1. Because position 1 does not exist (the string is empty), an exception is generated and the program terminates.


I'm not sure exactly what the program is supposed to do. Could you give an example of input and required output please.
Actually I think I now have a fair idea of how the program should work.
I would write main() something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
{
    Letras letra1("cosas");
    Letras letra2("voces");

    std::cout << " La palabra 1 se separa en : ";

    letra1.establecerLetras();

    std::cout << " \n\nLa palabra 2 se separa en : ";

    letra2.establecerLetras();

    std::cout << std::endl;

    return 0;
}


Then you need a constructor which accepts a string as a parameter, and initialises the member variable miPalabra with that string.

The function to print the string with spaces, maybe just like this:
1
2
3
4
void Letras::establecerLetras() const
{
	for (char ch : miPalabra)  std::cout << ch << ' ';
}


Or use a traditional for-loop with a count from 0 to count<miPalabra.size(), accessing each character in the string. There's no need for substrings, and it should be fairly simple, just 2 or 3 lines of code rather than twenty or thirty.
Last edited on
Topic archived. No new replies allowed.