7 Replies - 317 Views - Last Post: 05 November 2017 - 07:00 PM Rate Topic: -----

#1 sauerplayer  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 11
  • Joined: 27-October 17

My code fully compiles and then shows nothing

Posted 04 November 2017 - 07:12 PM

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

#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

#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

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



Is This A Good Question/Topic? 0
  • +

Replies To: My code fully compiles and then shows nothing

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5898
  • View blog
  • Posts: 20,138
  • Joined: 05-May 12

Re: My code fully compiles and then shows nothing

Posted 04 November 2017 - 07:29 PM

From what I can see scanning the code, it looks like the code should run. It may not give the output that you are expecting, but it will produce some output and therefore, technically, run.

It looks like the reason for the wrong output is your misunderstanding of two things:
1. scope
2. how the string functions work
Was This Post Helpful? 0
  • +
  • -

#3 sauerplayer  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 11
  • Joined: 27-October 17

Re: My code fully compiles and then shows nothing

Posted 04 November 2017 - 08:13 PM

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.
Was This Post Helpful? 0
  • +
  • -

#4 ndc85430  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 594
  • Posts: 2,491
  • Joined: 13-June 14

Re: My code fully compiles and then shows nothing

Posted 04 November 2017 - 10:22 PM

So, what do you think it means? Do you know what the valid indices in a string are?
Was This Post Helpful? 0
  • +
  • -

#5 GazinAtCode  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 70
  • Joined: 26-September 16

Re: My code fully compiles and then shows nothing

Posted 05 November 2017 - 01:04 AM

You seem to be using substr incorrectly. The second parameter specifies the length of the substring, so miPalabra.substr( 0, 0 ) effectively does nothing. There are other problems, too, but let's try to fix your code one step at a time.

This is unrelated to your errors, but the #endif directive should technically be the closing line in your Letras.h file.
Was This Post Helpful? 0
  • +
  • -

#6 Radius Nightly  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 91
  • Joined: 07-May 15

Re: My code fully compiles and then shows nothing

Posted 05 November 2017 - 06:37 PM

If its Bloodshed i think you should use some another command for pause.
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5898
  • View blog
  • Posts: 20,138
  • Joined: 05-May 12

Re: My code fully compiles and then shows nothing

Posted 05 November 2017 - 06:52 PM

sauerplayer: Like your other thread, this program is another situation where you don't really need a separate class to accomplish the task of printing spaces between the letters of a word.
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5898
  • View blog
  • Posts: 20,138
  • Joined: 05-May 12

Re: My code fully compiles and then shows nothing

Posted 05 November 2017 - 07:00 PM

And regarding the code itself, it looks like you are getting similar advise here and here regarding your incorrect use of substr(), but they haven't yet noticed the scoping issues.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1