My code fully compiles and then shows nothing
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: My code fully compiles and then shows nothing

  1. #1
    Join Date
    Oct 2016
    Posts
    16

    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

    Code:
    #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

    Code:
    #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

    Code:
    #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;
    }

  2. #2
    Join Date
    Oct 2016
    Posts
    16

    Re: My code fully compiles and then shows nothing

    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.

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,837

    Re: My code fully compiles and then shows nothing

    Code:
    miPalabra.substr( 1, 1 );
    I don't know what you expected this (and similar) to do, but it doesn't. This returns a string from miPalabra from the 1st position for a length of 1. It doesn't change miPalabra. As the returned string is not used, this statement (and the others like it) have no effect.

    Code:
    if ( miPalabra.size() <= 5 )
    You are checking if the string has less than 5 characters, but are then accessing character positions 0, 1, 2, 3, 4. In this case the string must have at least 5 characters otherwise you are accessing the string beyond its end which is out of range which is the error you are obtaining.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.4.4)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)